dnsimple 2.0.0.alpha2 → 2.0.0.alpha3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.markdown +6 -2
  3. data/README.markdown +32 -30
  4. data/dnsimple.gemspec +1 -2
  5. data/lib/dnsimple.rb +2 -13
  6. data/lib/dnsimple/client.rb +164 -53
  7. data/lib/dnsimple/client/certificates_service.rb +98 -0
  8. data/lib/dnsimple/client/client_service.rb +8 -0
  9. data/lib/dnsimple/client/contacts_service.rb +82 -0
  10. data/lib/dnsimple/client/domains_service.rb +333 -0
  11. data/lib/dnsimple/client/name_servers_service.rb +69 -0
  12. data/lib/dnsimple/client/registrars_service.rb +105 -0
  13. data/lib/dnsimple/client/services_service.rb +95 -0
  14. data/lib/dnsimple/client/templates_service.rb +180 -0
  15. data/lib/dnsimple/client/users_service.rb +37 -0
  16. data/lib/dnsimple/compatibility.rb +46 -0
  17. data/lib/dnsimple/default.rb +86 -0
  18. data/lib/dnsimple/error.rb +7 -11
  19. data/lib/dnsimple/extra.rb +54 -0
  20. data/lib/dnsimple/struct.rb +29 -0
  21. data/lib/dnsimple/struct/certificate.rb +56 -0
  22. data/lib/dnsimple/struct/contact.rb +61 -0
  23. data/lib/dnsimple/struct/domain.rb +40 -0
  24. data/lib/dnsimple/struct/email_forward.rb +14 -0
  25. data/lib/dnsimple/struct/extended_attribute.rb +39 -0
  26. data/lib/dnsimple/struct/membership.rb +22 -0
  27. data/lib/dnsimple/struct/price.rb +16 -0
  28. data/lib/dnsimple/struct/record.rb +22 -0
  29. data/lib/dnsimple/struct/service.rb +19 -0
  30. data/lib/dnsimple/struct/template.rb +19 -0
  31. data/lib/dnsimple/struct/template_record.rb +24 -0
  32. data/lib/dnsimple/struct/transfer_order.rb +10 -0
  33. data/lib/dnsimple/struct/user.rb +17 -0
  34. data/lib/dnsimple/struct/whois_privacy.rb +19 -0
  35. data/lib/dnsimple/version.rb +1 -1
  36. data/spec/dnsimple/client/certificates_service_spec.rb +196 -0
  37. data/spec/dnsimple/client/contacts_service_spec.rb +179 -0
  38. data/spec/dnsimple/client/domains_service_spec.rb +662 -0
  39. data/spec/dnsimple/client/name_servers_service_spec.rb +131 -0
  40. data/spec/dnsimple/client/registrars_service_spec.rb +160 -0
  41. data/spec/dnsimple/client/services_service_spec.rb +162 -0
  42. data/spec/dnsimple/client/templates_service_spec.rb +371 -0
  43. data/spec/dnsimple/client/users_service_spec.rb +70 -0
  44. data/spec/dnsimple/client_spec.rb +108 -99
  45. data/spec/dnsimple/compatibility_spec.rb +57 -0
  46. data/spec/files/2fa/exchange-token.http +8 -11
  47. data/spec/files/badgateway.http +14 -0
  48. data/spec/files/certificates/configure/success.http +19 -0
  49. data/spec/files/certificates/index/success.http +1 -1
  50. data/spec/files/certificates/notfound.http +19 -0
  51. data/spec/files/certificates/purchase/success.http +19 -0
  52. data/spec/files/certificates/show/success.http +1 -1
  53. data/spec/files/certificates/submit/success.http +19 -0
  54. data/spec/files/contacts/create/badrequest-missingcontact.http +19 -0
  55. data/spec/files/contacts/create/badrequest-validationerror.http +19 -0
  56. data/spec/files/contacts/create/created.http +22 -0
  57. data/spec/files/contacts/delete/success-204.http +18 -0
  58. data/spec/files/{extended_attributes/com.http → contacts/delete/success.http} +5 -5
  59. data/spec/files/{domains/auto_renewal_enable → contacts/index}/success.http +6 -6
  60. data/spec/files/contacts/notfound.http +19 -0
  61. data/spec/files/contacts/update/success.http +21 -0
  62. data/spec/files/domains/create/created.http +21 -0
  63. data/spec/files/domains/index/success.http +11 -9
  64. data/spec/files/domains/notfound.http +11 -9
  65. data/spec/files/domains/show/success.http +12 -10
  66. data/spec/files/domains_autorenewal/disable/success.http +21 -0
  67. data/spec/files/domains_autorenewal/enable/success.http +21 -0
  68. data/spec/files/domains_autorenewal/notfound-domain.http +19 -0
  69. data/spec/files/domains_forwards/create/created.http +22 -0
  70. data/spec/files/domains_forwards/delete/success.http +17 -0
  71. data/spec/files/domains_forwards/get/success.http +21 -0
  72. data/spec/files/domains_forwards/list/success.http +21 -0
  73. data/spec/files/domains_forwards/notfound-domain.http +19 -0
  74. data/spec/files/domains_forwards/notfound.http +19 -0
  75. data/spec/files/domains_records/create/created.http +21 -0
  76. data/spec/files/domains_records/delete/success-204.http +18 -0
  77. data/spec/files/{contacts/show/notfound.http → domains_records/delete/success.http} +9 -7
  78. data/spec/files/{records → domains_records}/index/success.http +0 -0
  79. data/spec/files/{records/show → domains_records}/notfound.http +1 -1
  80. data/spec/files/{records → domains_records}/show/success.http +0 -0
  81. data/spec/files/domains_records/update/success.http +21 -0
  82. data/spec/files/domains_sharing/create/success.http +21 -0
  83. data/spec/files/domains_sharing/delete/success.http +17 -0
  84. data/spec/files/domains_sharing/list/success.http +21 -0
  85. data/spec/files/domains_sharing/notfound-domain.http +19 -0
  86. data/spec/files/domains_sharing/notfound.http +19 -0
  87. data/spec/files/domains_whois_privacy/disable/success.http +21 -0
  88. data/spec/files/domains_whois_privacy/enable/success.http +22 -0
  89. data/spec/files/domains_zones/get/success.http +21 -0
  90. data/spec/files/domains_zones/notfound-domain.http +19 -0
  91. data/spec/files/{domains/auto_renewal_disable/notfound.http → nameservers/change/success.http} +23 -21
  92. data/spec/files/nameservers/deregister/success.http +17 -0
  93. data/spec/files/{domains/auto_renewal_enable/notfound.http → nameservers/list/success.http} +23 -21
  94. data/spec/files/nameservers/notfound-domain.http +19 -0
  95. data/spec/files/nameservers/register/badrequest-valueerror.http +19 -0
  96. data/spec/files/nameservers/register/success.http +21 -0
  97. data/spec/files/registrars/check/available.http +19 -0
  98. data/spec/files/registrars/check/registered.http +21 -0
  99. data/spec/files/registrars/register/badrequest-missingdomain.http +19 -0
  100. data/spec/files/registrars/register/badrequest-missingregistrant.http +19 -0
  101. data/spec/files/registrars/register/success.http +21 -0
  102. data/spec/files/registrars/renew/badrequest-missingrenewal.http +19 -0
  103. data/spec/files/registrars/renew/badrequest-unable.http +19 -0
  104. data/spec/files/registrars/renew/success.http +21 -0
  105. data/spec/files/registrars/transfer/success.http +21 -0
  106. data/spec/files/registrars_extended_attributes/list/success.http +21 -0
  107. data/spec/files/registrars_prices/list/success.http +21 -0
  108. data/spec/files/services/applied/success.http +21 -0
  109. data/spec/files/services/apply/success.http +21 -0
  110. data/spec/files/services/available/success.http +21 -0
  111. data/spec/files/services/index/success.http +21 -0
  112. data/spec/files/services/notfound.http +19 -0
  113. data/spec/files/services/show/success.http +21 -0
  114. data/spec/files/services/unapply/success.http +21 -0
  115. data/spec/files/subscription/show/success.http +21 -0
  116. data/spec/files/templates/apply/success.http +21 -0
  117. data/spec/files/templates/create/created.http +22 -0
  118. data/spec/files/templates/delete/success-204.http +21 -0
  119. data/spec/files/templates/delete/success.http +21 -0
  120. data/spec/files/templates/index/success.http +21 -0
  121. data/spec/files/templates/notfound.http +19 -0
  122. data/spec/files/templates/show/success.http +12 -10
  123. data/spec/files/templates/update/success.http +21 -0
  124. data/spec/files/templates_records/create/created.http +22 -0
  125. data/spec/files/templates_records/delete/success-204.http +19 -0
  126. data/spec/files/templates_records/delete/success.http +21 -0
  127. data/spec/files/templates_records/index/success.http +21 -0
  128. data/spec/files/templates_records/notfound.http +19 -0
  129. data/spec/files/templates_records/show/success.http +21 -0
  130. data/spec/files/templates_records/update/success.http +21 -0
  131. data/spec/files/users/user/success.http +21 -0
  132. data/spec/spec_helper.rb +0 -2
  133. metadata +206 -73
  134. data/lib/dnsimple/base.rb +0 -10
  135. data/lib/dnsimple/certificate.rb +0 -143
  136. data/lib/dnsimple/contact.rb +0 -157
  137. data/lib/dnsimple/domain.rb +0 -252
  138. data/lib/dnsimple/extended_attribute.rb +0 -52
  139. data/lib/dnsimple/record.rb +0 -94
  140. data/lib/dnsimple/service.rb +0 -42
  141. data/lib/dnsimple/template.rb +0 -65
  142. data/lib/dnsimple/template_record.rb +0 -80
  143. data/lib/dnsimple/transfer_order.rb +0 -34
  144. data/lib/dnsimple/user.rb +0 -50
  145. data/spec/dnsimple/certificate_spec.rb +0 -59
  146. data/spec/dnsimple/contact_spec.rb +0 -45
  147. data/spec/dnsimple/domain_spec.rb +0 -241
  148. data/spec/dnsimple/extended_attributes_spec.rb +0 -54
  149. data/spec/dnsimple/record_spec.rb +0 -51
  150. data/spec/dnsimple/template_spec.rb +0 -31
  151. data/spec/dnsimple/user_spec.rb +0 -70
  152. data/spec/files/account/user/success.http +0 -19
  153. data/spec/files/certificates/show/notfound.http +0 -17
  154. data/spec/files/domains/auto_renewal_disable/success.http +0 -23
  155. data/spec/files/domains/create/success.http +0 -19
  156. data/spec/files/extended_attributes/ca.http +0 -19
  157. data/spec/files/extended_attributes/success.http +0 -19
  158. data/spec/files/templates/show/notfound.http +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e16688144a4991e78dc2f86e78dbc558c011aa4
4
- data.tar.gz: f7dc21999ebef6b8bfee73a6a9d0afefad8ca89e
3
+ metadata.gz: 59bd049e932b2627935ec1c20b461c044a469db7
4
+ data.tar.gz: 339e7ddd3c26886983121c50b9f0600d4e91d458
5
5
  SHA512:
6
- metadata.gz: 461f978e357d7a1b6895253d8c1488f7d8b720fd00b1ef80008b83071ca6d4dc8b5aafcefe927b90aacc708deda5de9fbed6f491de67ef56d0bad4d44adc087c
7
- data.tar.gz: aef1ebfd28622173d53fc183a5834f5b42390fc3c7d4aff75686456b9d578fe5059430aab72436c55df836284bde6bde638538d524572bde220fe14e706531ee
6
+ metadata.gz: 18d6712c612148898493bacc510a5686233407b670125cc8da40a5152b7b19dca74217b316ffd7f81a6e62ebf678f502213dcaf3887a999a96ee0fafec171337
7
+ data.tar.gz: b8342199df7bee17d7483f09b9b32a22e076e694a69a12c43e06718f3392f6197bd4f06f838f67382cc911b776daa250ccc98152d7f6ab9f37ee2fcdd7e81327
data/CHANGELOG.markdown CHANGED
@@ -2,19 +2,23 @@
2
2
 
3
3
  #### 2.0.0.alpha
4
4
 
5
+ - NEW: Add support changing name servers (GH-52). Thanks @rosscooperman
6
+
5
7
  - CHANGED: Drop 1.8.7 support.
6
8
 
7
9
  - CHANGED: This package no longer provides a CLI. The CLI has been extracted to [aetrion/dnsimple-ruby-cli](https://github.com/aetrion/dnsimple-ruby-cli)
8
10
 
9
- - CHANGED: Renamed the Gem from "dnsimple-ruby" to "dnsimple". (GH-23)
11
+ - CHANGED: Renamed the Gem from "dnsimple-ruby" to "dnsimple" (GH-23).
10
12
 
11
13
  - CHANGED: Renamed the namespace from DNSimple to Dnsimple.
12
14
 
15
+ - CHANGED: Redesigned client API.
16
+
13
17
  - REMOVED: The library no longer provides built-in support for loading the credentials from a config file.
14
18
 
15
19
  #### Release 1.7.1
16
20
 
17
- - FIXED: Updated Certificate to match the serialized attributes (GH-53)
21
+ - FIXED: Updated Certificate to match the serialized attributes (GH-53).
18
22
 
19
23
  #### Release 1.7.0
20
24
 
data/README.markdown CHANGED
@@ -11,48 +11,50 @@ We provide a full API and an easy-to-use web interface so you can get your domai
11
11
 
12
12
  ## Installation
13
13
 
14
- $ gem install dnsimple
14
+ ```
15
+ $ gem install dnsimple
16
+ ```
17
+
15
18
 
19
+ ## Getting Started
16
20
 
17
- ## DNSimple Client
21
+ This library is a Ruby client you can use to interact with the [DNSimple API](http://developer.dnsimple.com/).
18
22
 
19
- This library provides a Ruby DNSimple client you can use to interact with the [DNSimple API](http://developer.dnsimple.com/). Here's a short example.
23
+ Here's a short example.
20
24
 
21
25
  ```ruby
22
26
  require 'dnsimple'
23
27
 
24
- DNSimple::Client.username = 'YOUR_USERNAME'
25
- DNSimple::Client.password = 'YOUR_PASSWORD'
28
+ client = Dnsimple::Client.new(username: 'YOUR_USERNAME', api_token: 'YOUR_TOKEN')
26
29
 
27
- user = DNSimple::User.me
28
- puts "#{user.domain_count} domains"
30
+ # Fetch your user details
31
+ user = client.users.user
32
+ puts "My email is #{user.email}"
29
33
 
30
- puts "Domains..."
31
- DNSimple::Domain.all.each do |domain|
32
- puts " #{domain.name}"
34
+ # Get a list of your domains
35
+ domains = client.domains.list
36
+ domains.each do |domain|
37
+ puts "Domain: %s (id: %d)" % [domain.name, domain.id]
33
38
  end
34
39
 
35
- domain = DNSimple::Domain.find("example.com")
36
- domain.apply("template") # applies a standard or custom template to the domain
37
-
38
- domain = DNSimple::Domain.create("newdomain.com")
39
- puts "Added #{domain.name}"
40
- domain.delete # removes from DNSimple
40
+ # Create a domain
41
+ domain = client.domains.create("example.com")
42
+ puts "Domain: %s (id: %d)" % [domain.name, domain.id]
41
43
  ```
42
44
 
43
- For the full API documentation visit http://rubydoc.info/gems/dnsimple
45
+ For the full library documentation visit http://rubydoc.info/gems/dnsimple
46
+
44
47
 
45
- ### Authentication
48
+ ## Authentication
46
49
 
47
50
  This client supports both the HTTP Basic and API Token authentication mechanism.
48
51
 
49
52
  #### HTTP Basic
50
53
 
51
54
  ```ruby
52
- DNSimple::Client.username = 'YOUR_USERNAME'
53
- DNSimple::Client.password = 'YOUR_PASSWORD'
54
-
55
- user = DNSimple::User.me
55
+ client = Dnsimple::Client.new(username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD')
56
+ client.users.user
57
+ # => Dnsimple::Struct::User
56
58
  ```
57
59
 
58
60
  #### HTTP Basic with two-factor authentication enabled
@@ -61,20 +63,20 @@ See the [2FA API documentation](http://developer.dnsimple.com/authentication/#tw
61
63
 
62
64
  ```ruby
63
65
  # Request the 2FA exchange token
64
- DNSimple::Client.username = 'YOUR_USERNAME'
65
- DNSimple::Client.password = 'YOUR_PASSWORD'
66
- token = DNSimple::User.two_factor_exchange_token('otp-token')
66
+ client = Dnsimple::Client.new(username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD')
67
+ token = client.users.exchange_token('otp-token')
67
68
 
68
69
  # Authenticate with the exchange token
69
- DNSimple::Client.exchange_token = token
70
- user = DNSimple::User.me
70
+ client.exchange_token = token
71
+ client.users.user
72
+ # => Dnsimple::Struct::User
71
73
  ```
72
74
 
73
75
  #### API Token
74
76
 
75
77
  ```ruby
76
- DNSimple::Client.username = 'YOUR_USERNAME'
77
- DNSimple::Client.api_token = 'API_TOKEN'
78
+ client = Dnsimple::Client.new(username: 'YOUR_USERNAME', api_token: 'YOUR_TOKEN')
78
79
 
79
- user = DNSimple::User.me
80
+ client.users.user
81
+ # => Dnsimple::Struct::User
80
82
  ```
data/dnsimple.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ['anthony.eden@dnsimple.com', 'simone.carletti@dnsimple.com']
10
10
  s.homepage = 'http://github.com/aetrion/dnsimple-ruby'
11
11
  s.summary = 'A Ruby client for the DNSimple API'
12
- s.description = 'A Ruby client for the DNSimple API that also includes a command-line client.'
12
+ s.description = 'A Ruby client for the DNSimple API.'
13
13
 
14
14
  s.required_ruby_version = ">= 1.9.3"
15
15
 
@@ -21,7 +21,6 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'httparty'
22
22
 
23
23
  s.add_development_dependency 'rake'
24
- s.add_development_dependency 'mocha'
25
24
  s.add_development_dependency 'rspec'
26
25
  s.add_development_dependency 'yard'
27
26
  s.add_development_dependency 'webmock'
data/lib/dnsimple.rb CHANGED
@@ -2,8 +2,6 @@ require 'httparty'
2
2
 
3
3
  module Dnsimple
4
4
 
5
- BLANK_REGEX = /\S+/
6
-
7
5
  # Echoes a deprecation warning message.
8
6
  #
9
7
  # @param [String] message The message to display.
@@ -18,16 +16,7 @@ module Dnsimple
18
16
 
19
17
  end
20
18
 
21
- require 'dnsimple/base'
19
+ require 'dnsimple/version'
20
+ require 'dnsimple/default'
22
21
  require 'dnsimple/client'
23
22
  require 'dnsimple/error'
24
- require 'dnsimple/user'
25
- require 'dnsimple/contact'
26
- require 'dnsimple/domain'
27
- require 'dnsimple/record'
28
- require 'dnsimple/template'
29
- require 'dnsimple/template_record'
30
- require 'dnsimple/transfer_order'
31
- require 'dnsimple/extended_attribute'
32
- require 'dnsimple/service'
33
- require 'dnsimple/certificate'
@@ -1,96 +1,207 @@
1
- require 'dnsimple/version'
2
- require 'yaml'
1
+ require 'dnsimple/compatibility'
2
+ require 'dnsimple/extra'
3
+ require 'dnsimple/struct'
4
+ require 'dnsimple/client/client_service'
5
+ require 'dnsimple/client/certificates_service'
6
+ require 'dnsimple/client/contacts_service'
7
+ require 'dnsimple/client/domains_service'
8
+ require 'dnsimple/client/name_servers_service'
9
+ require 'dnsimple/client/registrars_service'
10
+ require 'dnsimple/client/services_service'
11
+ require 'dnsimple/client/templates_service'
12
+ require 'dnsimple/client/users_service'
3
13
 
4
14
  module Dnsimple
15
+
16
+ # Client for the DNSimple API
17
+ #
18
+ # @see http://developer.dnsimple.com
5
19
  class Client
20
+ include Dnsimple::Compatibility
6
21
 
7
- DEFAULT_BASE_URI = "https://api.dnsimple.com/"
8
22
  HEADER_2FA_STRICT = "X-DNSimple-2FA-Strict"
9
23
  HEADER_API_TOKEN = "X-DNSimple-Token"
10
24
  HEADER_DOMAIN_API_TOKEN = "X-DNSimple-Domain-Token"
11
25
  HEADER_OTP_TOKEN = "X-DNSimple-OTP"
12
26
  HEADER_EXCHANGE_TOKEN = "X-DNSimple-OTP-Token"
13
27
 
14
- class << self
15
- # @return [Boolean] if the debug mode is enabled.
16
- # Defaults to false.
17
- attr_accessor :debug
18
28
 
19
- attr_accessor :username, :password, :exchange_token, :api_token, :domain_api_token
29
+ # @!attribute api_endpoint
30
+ # @return [String] Base URL for API requests. (default: https://api.dnsimple.com/)
31
+ # @!attribute username
32
+ # @return [String] DNSimple username for Basic Authentication
33
+ # @!attribute password
34
+ # @see http://developer.dnsimple.com/authentication/
35
+ # @return [String] DNSimple password for Basic Authentication
36
+ # @!attribute exchange_token
37
+ # @see http://developer.dnsimple.com/authentication/
38
+ # @return [String] Exchange Token for Basic Authentication with 2FA
39
+ # @!attribute api_token
40
+ # @see http://developer.dnsimple.com/authentication/
41
+ # @return [String] API access token for authentication
42
+ # @!attribute domain_api_token
43
+ # @see http://developer.dnsimple.com/authentication/
44
+ # @return [String] Domain API access token for authentication
45
+ # @!attribute user_agent
46
+ # @return [String] Configure User-Agent header for requests.
47
+ # @!attribute proxy
48
+ # @return [String,nil] Configure address:port values for proxy server
49
+
50
+ attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
51
+ :user_agent, :proxy
52
+
53
+
54
+ def initialize(options = {})
55
+ defaults = Dnsimple::Default.options
56
+
57
+ Dnsimple::Default.keys.each do |key|
58
+ instance_variable_set(:"@#{key}", options[key] || defaults[key])
59
+ end
60
+
61
+ @services = {}
62
+ end
63
+
64
+
65
+ # Make a HTTP GET request.
66
+ #
67
+ # @param [String] url The path, relative to {#api_endpoint}
68
+ # @param [Hash] options Query and header params for request
69
+ # @return [HTTParty::Response]
70
+ def get(path, options = {})
71
+ request :get, path, options
20
72
  end
21
73
 
22
- # Gets the qualified API base uri.
74
+ # Make a HTTP POST request.
23
75
  #
24
- # @return [String] The qualified API base uri.
25
- def self.base_uri
26
- @base_uri ||= DEFAULT_BASE_URI.chomp("/")
76
+ # @param [String] url The path, relative to {#api_endpoint}
77
+ # @param [Hash] options Body and header params for request
78
+ # @return [HTTParty::Response]
79
+ def post(path, options = {})
80
+ request :post, path, options
27
81
  end
28
82
 
29
- # Sets the qualified API base uri.
83
+ # Make a HTTP PUT request.
30
84
  #
31
- # @param [String] value The qualified API base uri.
32
- def self.base_uri=(value)
33
- @base_uri = value.to_s.chomp("/")
85
+ # @param [String] url The path, relative to {#api_endpoint}
86
+ # @param [Hash] options Body and header params for request
87
+ # @return [HTTParty::Response]
88
+ def put(path, options = {})
89
+ request :put, path, options
34
90
  end
35
91
 
36
- def self.http_proxy
37
- @http_proxy
92
+ # Make a HTTP DELETE request.
93
+ #
94
+ # @param [String] url The path, relative to {#api_endpoint}
95
+ # @param [Hash] options Query and header params for request
96
+ # @return [HTTParty::Response]
97
+ def delete(path, options = {})
98
+ request :delete, path, options
38
99
  end
39
100
 
40
- def self.base_options
41
- options = {
42
- :format => :json,
43
- :headers => { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{VERSION}" },
44
- }
45
101
 
46
- if http_proxy
47
- options.merge!(
48
- :http_proxyaddr => http_proxy[:addr],
49
- :http_proxyport => http_proxy[:port]
50
- )
102
+ # Make a HTTP request.
103
+ #
104
+ # @param [String] method The HTTP method
105
+ # @param [String] url The path, relative to {#api_endpoint}
106
+ # @param [Hash] options Query and header params for request
107
+ # @return [HTTParty::Response]
108
+ def request(method, path, data, options = {})
109
+ if data.is_a?(Hash)
110
+ options[:query] = data.delete(:query) if data.key?(:query)
111
+ options[:headers] = data.delete(:headers) if data.key?(:headers)
112
+ end
113
+ if !data.empty?
114
+ options[:body] = data
51
115
  end
52
116
 
53
- if exchange_token
54
- options[:basic_auth] = { :username => exchange_token, :password => "x-2fa-basic" }
55
- elsif password
56
- options[:basic_auth] = { :username => username, :password => password }
57
- elsif domain_api_token
58
- options[:headers][HEADER_DOMAIN_API_TOKEN] = domain_api_token
59
- elsif api_token
60
- options[:headers][HEADER_API_TOKEN] = "#{username}:#{api_token}"
117
+ response = HTTParty.send(method, api_endpoint + path, Extra.deep_merge!(base_options, options))
118
+
119
+ case response.code
120
+ when 200..299
121
+ response
122
+ when 401
123
+ raise (response.headers[HEADER_OTP_TOKEN] == "required" ? TwoFactorAuthenticationRequired : AuthenticationFailed), response["message"]
124
+ when 404
125
+ raise RecordNotFound.new(response)
61
126
  else
62
- raise Error, 'A password or API token is required for all API requests.'
127
+ raise RequestError.new(response)
63
128
  end
129
+ end
64
130
 
65
- options
131
+
132
+ # @return [Dnsimple::Client::CertificatesService] The certificate-related API proxy.
133
+ def certificates
134
+ @services[:certificates] ||= Client::CertificatesService.new(self)
66
135
  end
67
136
 
68
- def self.get(path, options = {})
69
- request :get, path, options
137
+ # @return [Dnsimple::Client::ContactsService] The contact-related API proxy.
138
+ def contacts
139
+ @services[:contacts] ||= Client::ContactsService.new(self)
70
140
  end
71
141
 
72
- def self.post(path, options = {})
73
- request :post, path, options
142
+ # @return [Dnsimple::Client::DomainsService] The domain-related API proxy.
143
+ def domains
144
+ @services[:domains] ||= Client::DomainsService.new(self)
74
145
  end
75
146
 
76
- def self.put(path, options = {})
77
- request :put, path, options
147
+ # @return [Dnsimple::Client::NameServersService] The name server-related API proxy.
148
+ def name_servers
149
+ @services[:name_servers] ||= Client::NameServersService.new(self)
78
150
  end
79
151
 
80
- def self.delete(path, options = {})
81
- request :delete, path, options
152
+ # @return [Dnsimple::Client::RegistrarsService] The registrar-related API proxy.
153
+ def registrars
154
+ @services[:registrars] ||= Client::RegistrarsService.new(self)
155
+ end
156
+
157
+ # @return [Dnsimple::Client::ServicesService] The service-related API proxy.
158
+ def services
159
+ @services[:services] ||= Client::ServicesService.new(self)
160
+ end
161
+
162
+ # @return [Dnsimple::Client::TemplatesService] The template-related API proxy.
163
+ def templates
164
+ @services[:templates] ||= Client::TemplatesService.new(self)
82
165
  end
83
166
 
84
- def self.request(method, path, options)
85
- response = HTTParty.send(method, "#{base_uri}#{path}", base_options.merge(options))
167
+ # @return [Dnsimple::Client::UsersService] The user-related API proxy.
168
+ def users
169
+ @services[:users] ||= Client::UsersService.new(self)
170
+ end
171
+
172
+
173
+ # @return [String] Base URL for API requests.
174
+ def api_endpoint
175
+ File.join(@api_endpoint, "")
176
+ end
86
177
 
87
- if response.code == 401 && response.headers[HEADER_OTP_TOKEN] == "required"
88
- raise TwoFactorAuthenticationRequired, response["message"]
89
- elsif response.code == 401
90
- raise AuthenticationFailed, response["message"]
178
+
179
+ private
180
+
181
+ def base_options
182
+ options = {
183
+ format: :json,
184
+ headers: { 'Accept' => 'application/json', 'User-Agent' => user_agent },
185
+ }
186
+
187
+ if proxy
188
+ address, port = proxy.split(":")
189
+ options.merge!(http_proxyaddr: address, http_proxyport: port)
91
190
  end
92
191
 
93
- response
192
+ if exchange_token
193
+ options[:basic_auth] = { username: exchange_token, password: "x-2fa-basic" }
194
+ elsif password
195
+ options[:basic_auth] = { username: username, password: password }
196
+ elsif domain_api_token
197
+ options[:headers][HEADER_DOMAIN_API_TOKEN] = domain_api_token
198
+ elsif api_token
199
+ options[:headers][HEADER_API_TOKEN] = "#{username}:#{api_token}"
200
+ else
201
+ raise Error, 'A password or API token is required for all API requests.'
202
+ end
203
+
204
+ options
94
205
  end
95
206
 
96
207
  end
@@ -0,0 +1,98 @@
1
+ module Dnsimple
2
+ class Client
3
+ class CertificatesService < ClientService
4
+
5
+ # Lists the certificates for a domain.
6
+ #
7
+ # @see http://developer.dnsimple.com/domains/certificates/#list
8
+ #
9
+ # @param [#to_s] domain The domain id or domain name.
10
+ # @param [Hash] options
11
+ #
12
+ # @return [Array<Struct::Certificate>]
13
+ # @raise [RecordNotFound]
14
+ # @raise [RequestError] When the request fails.
15
+ def list(domain, options = {})
16
+ response = client.get("v1/domains/#{domain}/certificates", options)
17
+
18
+ response.map { |r| Struct::Certificate.new(r["certificate"]) }
19
+ end
20
+
21
+ # Gets a certificate for a domain.
22
+ #
23
+ # @see http://developer.dnsimple.com/domains/certificates/#get
24
+ #
25
+ # @param [#to_s] domain The domain id or domain name.
26
+ # @param [Fixnum] certificate_id The certificate ID.
27
+ #
28
+ # @return [Struct::Certificate]
29
+ # @raise [RecordNotFound]
30
+ # @raise [RequestError] When the request fails.
31
+ def find(domain, certificate_id)
32
+ response = client.get("v1/domains/#{domain}/certificates/#{certificate_id}")
33
+
34
+ Struct::Certificate.new(response["certificate"])
35
+ end
36
+
37
+ # Purchases a certificate under the given domain with the given name.
38
+ #
39
+ # The name will be appended to the domain name, and thus should only be the subdomain part.
40
+ #
41
+ # Invoking this method DNSimple will immediately charge
42
+ # your credit card on file at DNSimple for the full certificate price.
43
+ #
44
+ # For wildcard certificates an asterisk must appear in the name.
45
+ #
46
+ # @example Purchase a single-hostname certificate
47
+ # Dnsimple::Certificate.purchase(domain, 'www', contact)
48
+ #
49
+ # @example Purchase a wildcard certificate
50
+ # Dnsimple::Certificate.purchase(domain, '*', contact)
51
+ #
52
+ # @param [#to_s] domain The domain id or domain name.
53
+ # @param [String] name The certificate name.
54
+ # @param [Fixnum] contact_id The ID of the contact associated to the certificate.
55
+ #
56
+ # @return [Struct::Certificate]
57
+ # @raise [RecordNotFound]
58
+ # @raise [RequestError] When the request fails.
59
+ def purchase(domain, name, contact_id, options = {})
60
+ options = Extra.deep_merge(options, { certificate: { name: name, contact_id: contact_id }})
61
+ response = client.post("v1/domains/#{domain}/certificates", options)
62
+
63
+ Struct::Certificate.new(response["certificate"])
64
+ end
65
+
66
+ # Configures a certificate.
67
+ #
68
+ # @param [#to_s] domain The domain id or domain name.
69
+ # @param [Fixnum] certificate_id The certificate ID.
70
+ #
71
+ # @return [Struct::Certificate]
72
+ # @raise [RecordNotFound]
73
+ # @raise [RequestError] When the request fails.
74
+ def configure(domain, certificate_id)
75
+ response = client.put("v1/domains/#{domain}/certificates/#{certificate_id}/configure")
76
+
77
+ Struct::Certificate.new(response["certificate"])
78
+ end
79
+
80
+ # Submits a certificate for approval.
81
+ #
82
+ # @param [#to_s] domain The domain id or domain name.
83
+ # @param [Fixnum] certificate_id The certificate ID.
84
+ # @param [Fixnum] email The approver email.
85
+ #
86
+ # @return [Struct::Certificate]
87
+ # @raise [RecordNotFound]
88
+ # @raise [RequestError] When the request fails.
89
+ def submit(domain, certificate_id, email)
90
+ options = { certificate: { approver_email: email }}
91
+ response = client.put("v1/domains/#{domain}/certificates/#{certificate_id}/submit", options)
92
+
93
+ Struct::Certificate.new(response["certificate"])
94
+ end
95
+
96
+ end
97
+ end
98
+ end