hostrado-api
Advanced tools
| module HostradoAPI | ||
| class Domain | ||
| attr_reader :id | ||
| attr_reader :name | ||
| attr_reader :tld | ||
| attr_reader :expires_on | ||
| def initialize(domain_data = {}) | ||
| @id = domain_data['id'].to_i | ||
| @name = domain_data['name'] | ||
| @tld = domain_data['tld'] | ||
| @expires_on = Date.parse(domain_data['expires_on']) | ||
| end | ||
| end | ||
| end |
| module HostradoAPI | ||
| class HostradoAPIError < Exception; end | ||
| class RemoteInternalServerError < HostradoAPIError; end | ||
| class ConnectionError < HostradoAPIError; end | ||
| class ResponseFormatError < HostradoAPIError; end | ||
| class InvalidAccessTokenError < HostradoAPIError; end | ||
| end |
| require_relative 'v1/domains' | ||
| require_relative 'v1/connection' |
| module HostradoAPI | ||
| module V1 | ||
| class Connection | ||
| attr_reader :token | ||
| attr_reader :api_server | ||
| attr_accessor :domains | ||
| def initialize(token, api_server = nil) | ||
| @token = token | ||
| @api_server = api_server ? api_server : 'https://api.hostrado.com/v1' | ||
| @domains = HostradoAPI::V1::Domains.new(self) | ||
| end | ||
| def send_request(method, path, params = {}) | ||
| timeout = params[:timeout] ? params[:timeout].to_i : 10 | ||
| params.delete(:timeout) | ||
| uri = URI("#{@api_server}/#{path}") | ||
| request_params = format_params(params) unless format_params(params).empty? | ||
| begin | ||
| request = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), open_timeout: timeout) | ||
| response = request.send_request(method.to_s.upcase, uri.path.empty? ? '/' : uri.path, request_params, { 'X-Hostrado-Token': @token }) | ||
| raise RemoteInternalServerError.new(response.body) if response.code == '500' | ||
| response_body = JSON.parse(response.body) | ||
| raise InvalidAccessTokenError.new(response_body['message']) if response.code == '401' && response_body['error'] == 'invalid_api_token' | ||
| raise HostradoAPIError.new(response.body) unless response.code == '200' | ||
| [response_body, response] | ||
| rescue Errno::ECONNREFUSED, SocketError, Net::OpenTimeout => err | ||
| raise ConnectionError.new(err.to_s) | ||
| rescue JSON::ParserError => err | ||
| raise ResponseFormatError.new("API server sent a non-JSON string: #{response.body}") | ||
| end | ||
| end | ||
| private | ||
| def format_params(params) | ||
| values = params.values | ||
| formatted_params = "" | ||
| params.keys.each_with_index do |param, index| | ||
| formatted_params += "&" if index != 0 | ||
| if values[index].is_a?(Array) | ||
| values[index].each do |array_value| | ||
| formatted_params += '&' if formatted_params[-1] != '&' && formatted_params.length > 0 | ||
| formatted_params += "#{CGI.escape(param.to_s)}[]=#{CGI.escape(array_value.to_s)}" | ||
| end | ||
| else | ||
| formatted_params += "#{CGI.escape(param.to_s)}=#{CGI.escape(values[index].to_s)}" | ||
| end | ||
| end | ||
| formatted_params | ||
| end | ||
| end | ||
| end | ||
| end |
| module HostradoAPI | ||
| module V1 | ||
| class Domains | ||
| def initialize(connection) | ||
| @connection = connection | ||
| end | ||
| def list | ||
| domain_list, _ = @connection.send_request(:get, '/domains') | ||
| domain_list['domains'].map { |domain| HostradoAPI::Domain.new(domain) } | ||
| end | ||
| end | ||
| end | ||
| end |
+5
-0
@@ -0,3 +1,8 @@ | ||
| ## Release 0.0.2 (2019-02-11) | ||
| * Add HostradoAPI::V1::Connection.new to establish a new API server connection | ||
| * Add domain module to work with the /domains API endpoint | ||
| ## Release 0.0.1 (2018-06-14) | ||
| * Initialize ruby gem |
@@ -0,3 +1,11 @@ | ||
| require 'net/http' | ||
| require 'json' | ||
| require 'date' | ||
| require_relative 'exceptions' | ||
| require_relative 'domain' | ||
| require_relative 'v1' | ||
| module HostradoAPI | ||
| VERSION = "0.0.1" | ||
| end |
+48
-2
@@ -1,2 +0,48 @@ | ||
| # hostrado-api-ruby | ||
| Hostrado Ruby API. | ||
| # Hostrado API for Ruby | ||
| This is the official Hostrado API Ruby Gem to work with Hostrado's API endpoint. | ||
| ## Getting Started | ||
| ### Install the Library | ||
| Just install the library on your system by running: | ||
| ``` | ||
| gem install hostrado-api | ||
| ``` | ||
| ### Generate an API Token | ||
| You will now need an API key that can be generated for every Hostrado account on https://www.hostrado.com. Log in to the `Customer Center` and navigate to the API Tokens page. | ||
| The instructions in the customer center should make it clear what to do next. In case you need assistance, please contact our support and we will assist you on this. | ||
| ### Basic Usage of this Library | ||
| Now that you have installed the library and obtained an API token, you can start using the library. Please check our [Wiki page](https://github.com/hostrado/hostrado-api-ruby/wiki) for all available methods in this gem. | ||
| Let's make a simple API call to list your domains you're owning on hostrado.com: | ||
| ```ruby | ||
| require 'hostrado-api' | ||
| hostrado_api_token = 'REPLACE_WITH_YOUR_API_TOKEN' | ||
| api = HostradoAPI::V1::Connection.new(hostrado_api_token) | ||
| api.domains.list.each do |domain| | ||
| puts "ID: #{domain.id}, Name: #{domain.name}, TLD: #{domain.tld}, Expires On: #{domain.expires_on}" | ||
| end | ||
| ``` | ||
| The output can look like this: | ||
| ``` | ||
| ID: 3, Name: hostrado.com, TLD: com, Expires On: 2020-01-28 | ||
| ID: 4, Name: hostrado.net, TLD: net, Expires On: 2019-09-07 | ||
| ``` | ||
| See how it easy it is? Have fun playing around and don't hesitate reaching out to us by opening a new [GitHub issue](https://github.com/hostrado/hostrado-api-ruby/issues) or using our [Forums](https://forum.hostrado.com). | ||
| Please also checkout our [examples](https://github.com/hostrado/hostrado-api-ruby/tree/master/examples). | ||
| ## Contributions | ||
| Contributions are highly appreciated! Just fork this repository, do your changes and open a new pull request. We also very appreciate bug reports via the [issue tracking tool](https://github.com/hostrado/hostrado-api-ruby/issues) here on GitHub ;-) |