hostrado-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97d60d24b15a1f0905bb1774b43268e1e95ad9ed2164a5580b3ad7f551d3ef15
4
- data.tar.gz: 76a961feee87564fb6de6976e9ba40ffd76369136e81fe7bcee320c03a3d7ec3
3
+ metadata.gz: 1906cbb0365285ed18160448bc0d53b79e5a936b83c3b3dafe779249ef1c588c
4
+ data.tar.gz: 964f40b12697384c614d80e314ea8224d9e8a1aad08bad8405ad5050b8736f83
5
5
  SHA512:
6
- metadata.gz: c585c64eb2061340274a6a0c57d19188ab9f2299e11e87666f07bfaaa633b35b863e5bf96ea318efea751171688fd621558ccde433c5700d5e8eab47f3c1ba93
7
- data.tar.gz: 5077677b20a32c1eea94914028db7a5f6e95510777513c57861e07eaae517661f6a1a087b8f1adcbd4898bc750fd2e7e4736cabcda199baf666d7a23ef47e518
6
+ metadata.gz: d4a0ec4c8ea77bf2dee83e9dc7bf4eb93d44eeb49af32cf7d0f39013b33365231ee0b796a47a60effb3dbce16029508a43963cf7c449128c677299273ebc40fd
7
+ data.tar.gz: ba4711ef50d3f0d52a27cae919cdaf3ab9ecd25de779b1171ce8e460c20d309afbe643083639783910b677fdae7681f603073d2927b34af8ed0ac7f3f434fe74
@@ -1,3 +1,8 @@
1
+ ## Release 0.0.2 (2019-02-11)
2
+
3
+ * Add HostradoAPI::V1::Connection.new to establish a new API server connection
4
+ * Add domain module to work with the /domains API endpoint
5
+
1
6
  ## Release 0.0.1 (2018-06-14)
2
7
 
3
8
  * Initialize ruby gem
data/README.md CHANGED
@@ -1,2 +1,48 @@
1
- # hostrado-api-ruby
2
- Hostrado Ruby API.
1
+ # Hostrado API for Ruby
2
+
3
+ This is the official Hostrado API Ruby Gem to work with Hostrado's API endpoint.
4
+
5
+ ## Getting Started
6
+
7
+ ### Install the Library
8
+
9
+ Just install the library on your system by running:
10
+ ```
11
+ gem install hostrado-api
12
+ ```
13
+
14
+ ### Generate an API Token
15
+
16
+ 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.
17
+
18
+ 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.
19
+
20
+ ### Basic Usage of this Library
21
+
22
+ 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.
23
+
24
+ Let's make a simple API call to list your domains you're owning on hostrado.com:
25
+ ```ruby
26
+ require 'hostrado-api'
27
+
28
+ hostrado_api_token = 'REPLACE_WITH_YOUR_API_TOKEN'
29
+ api = HostradoAPI::V1::Connection.new(hostrado_api_token)
30
+
31
+ api.domains.list.each do |domain|
32
+ puts "ID: #{domain.id}, Name: #{domain.name}, TLD: #{domain.tld}, Expires On: #{domain.expires_on}"
33
+ end
34
+ ```
35
+
36
+ The output can look like this:
37
+ ```
38
+ ID: 3, Name: hostrado.com, TLD: com, Expires On: 2020-01-28
39
+ ID: 4, Name: hostrado.net, TLD: net, Expires On: 2019-09-07
40
+ ```
41
+
42
+ 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).
43
+
44
+ Please also checkout our [examples](https://github.com/hostrado/hostrado-api-ruby/tree/master/examples).
45
+
46
+ ## Contributions
47
+
48
+ 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 ;-)
@@ -0,0 +1,15 @@
1
+ module HostradoAPI
2
+ class Domain
3
+ attr_reader :id
4
+ attr_reader :name
5
+ attr_reader :tld
6
+ attr_reader :expires_on
7
+
8
+ def initialize(domain_data = {})
9
+ @id = domain_data['id'].to_i
10
+ @name = domain_data['name']
11
+ @tld = domain_data['tld']
12
+ @expires_on = Date.parse(domain_data['expires_on'])
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module HostradoAPI
2
+ class HostradoAPIError < Exception; end
3
+ class RemoteInternalServerError < HostradoAPIError; end
4
+ class ConnectionError < HostradoAPIError; end
5
+ class ResponseFormatError < HostradoAPIError; end
6
+ class InvalidAccessTokenError < HostradoAPIError; end
7
+ end
@@ -1,3 +1,11 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'date'
4
+
5
+ require_relative 'exceptions'
6
+ require_relative 'domain'
7
+ require_relative 'v1'
8
+
1
9
  module HostradoAPI
2
10
  VERSION = "0.0.1"
3
11
  end
@@ -0,0 +1,2 @@
1
+ require_relative 'v1/domains'
2
+ require_relative 'v1/connection'
@@ -0,0 +1,65 @@
1
+ module HostradoAPI
2
+ module V1
3
+ class Connection
4
+ attr_reader :token
5
+ attr_reader :api_server
6
+ attr_accessor :domains
7
+
8
+ def initialize(token, api_server = nil)
9
+ @token = token
10
+ @api_server = api_server ? api_server : 'https://api.hostrado.com/v1'
11
+
12
+ @domains = HostradoAPI::V1::Domains.new(self)
13
+ end
14
+
15
+ def send_request(method, path, params = {})
16
+ timeout = params[:timeout] ? params[:timeout].to_i : 10
17
+ params.delete(:timeout)
18
+
19
+ uri = URI("#{@api_server}/#{path}")
20
+
21
+ request_params = format_params(params) unless format_params(params).empty?
22
+
23
+ begin
24
+ request = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), open_timeout: timeout)
25
+ response = request.send_request(method.to_s.upcase, uri.path.empty? ? '/' : uri.path, request_params, { 'X-Hostrado-Token': @token })
26
+
27
+ raise RemoteInternalServerError.new(response.body) if response.code == '500'
28
+
29
+ response_body = JSON.parse(response.body)
30
+
31
+ raise InvalidAccessTokenError.new(response_body['message']) if response.code == '401' && response_body['error'] == 'invalid_api_token'
32
+ raise HostradoAPIError.new(response.body) unless response.code == '200'
33
+
34
+ [response_body, response]
35
+ rescue Errno::ECONNREFUSED, SocketError, Net::OpenTimeout => err
36
+ raise ConnectionError.new(err.to_s)
37
+ rescue JSON::ParserError => err
38
+ raise ResponseFormatError.new("API server sent a non-JSON string: #{response.body}")
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def format_params(params)
45
+ values = params.values
46
+ formatted_params = ""
47
+
48
+ params.keys.each_with_index do |param, index|
49
+ formatted_params += "&" if index != 0
50
+
51
+ if values[index].is_a?(Array)
52
+ values[index].each do |array_value|
53
+ formatted_params += '&' if formatted_params[-1] != '&' && formatted_params.length > 0
54
+ formatted_params += "#{CGI.escape(param.to_s)}[]=#{CGI.escape(array_value.to_s)}"
55
+ end
56
+ else
57
+ formatted_params += "#{CGI.escape(param.to_s)}=#{CGI.escape(values[index].to_s)}"
58
+ end
59
+ end
60
+
61
+ formatted_params
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ module HostradoAPI
2
+ module V1
3
+ class Domains
4
+ def initialize(connection)
5
+ @connection = connection
6
+ end
7
+
8
+ def list
9
+ domain_list, _ = @connection.send_request(:get, '/domains')
10
+ domain_list['domains'].map { |domain| HostradoAPI::Domain.new(domain) }
11
+ end
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -1,15 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hostrado-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Schnitzer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-14 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: date
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.1.0
13
53
  description: A library to communicate with the Hostrado API server.
14
54
  email: webmaster@mschnitzer.de
15
55
  executables: []
@@ -18,7 +58,12 @@ extra_rdoc_files: []
18
58
  files:
19
59
  - CHANGELOG.md
20
60
  - README.md
61
+ - lib/domain.rb
62
+ - lib/exceptions.rb
21
63
  - lib/hostrado-api.rb
64
+ - lib/v1.rb
65
+ - lib/v1/connection.rb
66
+ - lib/v1/domains.rb
22
67
  homepage: https://github.com/hostrado/hostrado-api-ruby
23
68
  licenses:
24
69
  - MIT