dnsmadeeasy-rest-api 1.0.3 → 1.0.4

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
  SHA1:
3
- metadata.gz: ad96706553c3063b3459a6db5a040c964b5e4804
4
- data.tar.gz: 03e58dfc227d4100e15d052a190531106188dfca
3
+ metadata.gz: 3987a44ee6657d03fe2746d75007b772672c1899
4
+ data.tar.gz: 9b94abad19f6ebc718369b1be6ac7aabf68e738b
5
5
  SHA512:
6
- metadata.gz: 1f950d3f6577f89843f5055847981e43b853a8af04a7d65e11db501ddece19ab1745ca0ec915e1f91367c519c19f6367ff1c8056697a4f76e0a94e4a8b587b29
7
- data.tar.gz: 88e2629ed703584d9307e25962c409c28d27b8b2720ed8e0859bccd7a58d46c7412a77dbf5238be63c21b8572c7ca407b57a5d7be086f55dc48bf9f44ca12c87
6
+ metadata.gz: 17ba812ec3bad74eacfbd157a8b8b8697b1b2d7ebf60379d8141a6b51d1bbf240440788daf2c344196ed64c6fe9d461943231f69178f1bb502427f722a80d8b4
7
+ data.tar.gz: 6f2873e2ad365e6a0f3125f053762887019834fea444d0db5e1f78178f510ca39bf1e6ff79ca79897f974e7c96e7e36f86a07fa729517bed8120e8e99b046d3a
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ Metrics/LineLength:
2
+ Max: 300
3
+
4
+ Style/SpaceBeforeFirstArg:
5
+ Exclude:
6
+ - '**/metadata.rb'
data/README.md CHANGED
@@ -93,6 +93,10 @@ api.delete_record('hello.example.com', 123)
93
93
  # To delete multiple records:
94
94
 
95
95
  api.delete_records('hello.example.com', [123, 143])
96
+
97
+ # To delete all records in the domain:
98
+
99
+ api.delete_all_records('hello.example.com')
96
100
  ```
97
101
 
98
102
  To create a record:
data/Rakefile CHANGED
@@ -1,2 +1,22 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
2
4
 
5
+ namespace :style do
6
+ desc 'Run Ruby style checks'
7
+ RuboCop::RakeTask.new(:ruby) do |t|
8
+ t.options = ['-D']
9
+ end
10
+ end
11
+
12
+ desc 'Run all style checks'
13
+ task style: ['style:ruby']
14
+
15
+ desc 'Run CRSpec unit tests'
16
+ RSpec::Core::RakeTask.new(:unit) do |t|
17
+ t.rspec_opts = '--color --format documentation'
18
+ end
19
+
20
+ # The default rake task should just run it all
21
+ desc 'Run all tests'
22
+ task default: %w(style unit)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dnsmadeeasy-rest-api'
3
- s.version = '1.0.3'
3
+ s.version = '1.0.4'
4
4
  s.authors = ['Arnoud Vermeer', 'Paul Henry', 'James Hart']
5
5
  s.email = ['a.vermeer@freshway.biz', 'ops@wanelo.com']
6
6
  s.license = 'Apache'
@@ -12,4 +12,6 @@ Gem::Specification.new do |s|
12
12
 
13
13
  s.add_development_dependency 'rspec'
14
14
  s.add_development_dependency 'webmock'
15
+ s.add_development_dependency 'bundler'
16
+ s.add_development_dependency 'rubocop'
15
17
  end
@@ -4,16 +4,17 @@ require 'json'
4
4
  require 'uri'
5
5
  require 'net/http'
6
6
 
7
+ # A class to interact with the DNSMadeEasy REST API v2.0
7
8
  class DnsMadeEasy
8
-
9
9
  attr_accessor :base_uri
10
10
 
11
- def initialize (api_key, api_secret, sandbox = false)
12
- raise "api_key is undefined" unless api_key
13
- raise "api_secret is undefined" unless api_secret
11
+ def initialize(api_key, api_secret, sandbox = false, options = {})
12
+ fail 'api_key is undefined' unless api_key
13
+ fail 'api_secret is undefined' unless api_secret
14
14
 
15
15
  @api_key = api_key
16
16
  @api_secret = api_secret
17
+ @options = options
17
18
 
18
19
  if sandbox
19
20
  self.base_uri = 'https://api.sandbox.dnsmadeeasy.com/V2.0'
@@ -58,80 +59,90 @@ class DnsMadeEasy
58
59
  get "/dns/managed/#{get_id_by_domain(domain_name)}/records"
59
60
  end
60
61
 
62
+ def find(domain_name, name, type)
63
+ records = records_for(domain_name)
64
+ records['data'].detect { |r| r['name'] == name && r['type'] == type }
65
+ end
66
+
61
67
  def find_record_id(domain_name, name, type)
62
68
  records = records_for(domain_name)
63
69
 
64
- records['data'].select { |r| r['name'] == name && r['type'] == type }
65
- .map { |r| r['id'] }
70
+ records['data'].select { |r| r['name'] == name && r['type'] == type }.map { |r| r['id'] }
71
+ end
72
+
73
+ def delete_record(domain_name, record_id)
74
+ delete "/dns/managed/#{get_id_by_domain(domain_name)}/records/#{record_id}/"
66
75
  end
67
76
 
68
77
  def delete_records(domain_name, ids = [])
69
- id = get_id_by_domain(domain_name)
78
+ return if ids.empty?
79
+ domain_id = get_id_by_domain(domain_name)
70
80
 
71
- delete "/dns/managed/#{id}/records/", ids
81
+ delete "/dns/managed/#{domain_id}/records?ids=#{ids.join(',')}"
72
82
  end
73
83
 
74
- def delete_record(domain_name, record_id)
75
- delete "/dns/managed/#{get_id_by_domain(domain_name)}/records/#{record_id}/"
84
+ def delete_all_records(domain_name)
85
+ domain_id = get_id_by_domain(domain_name)
86
+ delete "/dns/managed/#{domain_id}/records"
76
87
  end
77
88
 
78
89
  def create_record(domain_name, name, type, value, options = {})
79
- body = {"name" => name, "type" => type, "value" => value, "ttl" => 3600, "gtdLocation" => "DEFAULT"}
90
+ body = { 'name' => name, 'type' => type, 'value' => value, 'ttl' => 3600, 'gtdLocation' => 'DEFAULT' }
80
91
  post "/dns/managed/#{get_id_by_domain(domain_name)}/records/", body.merge(options)
81
92
  end
82
93
 
83
94
  def create_a_record(domain_name, name, value, options = {})
84
- # todo: match IPv4 for value
85
- create_record domain_name, name, "A", value, options
95
+ # TODO: match IPv4 for value
96
+ create_record domain_name, name, 'A', value, options
86
97
  end
87
98
 
88
99
  def create_aaaa_record(domain_name, name, value, options = {})
89
- # todo: match IPv6 for value
90
- create_record domain_name, name, "AAAA", value, options
100
+ # TODO: match IPv6 for value
101
+ create_record domain_name, name, 'AAAA', value, options
91
102
  end
92
103
 
93
104
  def create_ptr_record(domain_name, name, value, options = {})
94
- # todo: match PTR value
95
- create_record domain_name, name, "PTR", value, options
105
+ # TODO: match PTR value
106
+ create_record domain_name, name, 'PTR', value, options
96
107
  end
97
108
 
98
109
  def create_txt_record(domain_name, name, value, options = {})
99
- # todo: match TXT value
100
- create_record domain_name, name, "TXT", value, options
110
+ # TODO: match TXT value
111
+ create_record domain_name, name, 'TXT', value, options
101
112
  end
102
113
 
103
114
  def create_cname_record(domain_name, name, value, options = {})
104
- # todo: match CNAME value
105
- create_record domain_name, name, "CNAME", value, options
115
+ # TODO: match CNAME value
116
+ create_record domain_name, name, 'CNAME', value, options
106
117
  end
107
118
 
108
119
  def create_ns_record(domain_name, name, value, options = {})
109
- # todo: match domainname for value
110
- create_record domain_name, name, "NS", value, options
120
+ # TODO: match domainname for value
121
+ create_record domain_name, name, 'NS', value, options
111
122
  end
112
123
 
113
124
  def create_spf_record(domain_name, name, value, options = {})
114
- create_record domain_name, name, "SPF", value, options
125
+ create_record domain_name, name, 'SPF', value, options
115
126
  end
116
127
 
117
128
  def create_mx_record(domain_name, name, priority, value, options = {})
118
- options.merge!({"mxLevel" => priority})
129
+ options.merge!('mxLevel' => priority)
119
130
 
120
- create_record domain_name, name, "MX", value, options
131
+ create_record domain_name, name, 'MX', value, options
121
132
  end
122
133
 
123
134
  def create_srv_record(domain_name, name, priority, weight, port, value, options = {})
124
- options.merge!({"priority" => priority, "weight" => weight, "port" => port})
125
- create_record domain_name, name, "SRV", value, options
135
+ options.merge!('priority' => priority, 'weight' => weight, 'port' => port)
136
+ create_record domain_name, name, 'SRV', value, options
126
137
  end
127
138
 
128
- def create_httpred_record(domain_name, name, value, redirectType = "STANDARD - 302", description = "", keywords = "", title = "", options = {})
129
- options.merge!({"redirectType" => redirectType, "description" => description, "keywords" => keywords, "title" => title})
130
- create_record domain_name, name, "HTTPRED", value, options
139
+ def create_httpred_record(domain_name, name, value, redirectType = 'STANDARD - 302', description = '', keywords = '', title = '', options = {})
140
+ options.merge!('redirectType' => redirectType, 'description' => description, 'keywords' => keywords, 'title' => title)
141
+ create_record domain_name, name, 'HTTPRED', value, options
131
142
  end
132
143
 
133
144
  def update_record(domain, record_id, name, type, value, options = {})
134
- body = { "name" => name, "type" => type, "value" => value, "ttl" => 3600, "gtdLocation" => "DEFAULT", "id" => record_id}
145
+ body = { 'name' => name, 'type' => type, 'value' => value, 'ttl' => 3600, 'gtdLocation' => 'DEFAULT', 'id' => record_id }
135
146
  put "/dns/managed/#{get_id_by_domain(domain)}/records/#{record_id}/", body.merge(options)
136
147
  end
137
148
 
@@ -139,13 +150,13 @@ class DnsMadeEasy
139
150
 
140
151
  def get(path)
141
152
  request(path) do |uri|
142
- Net::HTTP::Get.new(uri.path)
153
+ Net::HTTP::Get.new(uri)
143
154
  end
144
155
  end
145
156
 
146
157
  def delete(path, body = nil)
147
158
  request(path) do |uri|
148
- req = Net::HTTP::Delete.new(uri.path)
159
+ req = Net::HTTP::Delete.new(uri)
149
160
  req.body = body.to_json if body
150
161
  req
151
162
  end
@@ -153,7 +164,7 @@ class DnsMadeEasy
153
164
 
154
165
  def put(path, body = nil)
155
166
  request(path) do |uri|
156
- req = Net::HTTP::Put.new(uri.path)
167
+ req = Net::HTTP::Put.new(uri)
157
168
  req.body = body.to_json if body
158
169
  req
159
170
  end
@@ -161,7 +172,7 @@ class DnsMadeEasy
161
172
 
162
173
  def post(path, body)
163
174
  request(path) do |uri|
164
- req = Net::HTTP::Post.new(uri.path)
175
+ req = Net::HTTP::Post.new(uri)
165
176
  req.body = body.to_json
166
177
  req
167
178
  end
@@ -172,6 +183,8 @@ class DnsMadeEasy
172
183
 
173
184
  http = Net::HTTP.new(uri.host, uri.port)
174
185
  http.use_ssl = true
186
+ http.open_timeout = @options[:open_timeout] if @options.key?(:open_timeout)
187
+ http.read_timeout = @options[:read_timeout] if @options.key?(:read_timeout)
175
188
 
176
189
  request = yield(uri)
177
190
 
@@ -180,7 +193,9 @@ class DnsMadeEasy
180
193
  end
181
194
 
182
195
  response = http.request(request)
183
- unparsed_json = response.body == "" ? "{}" : response.body
196
+ response.value # raise Net::HTTPServerException unless response was 2xx
197
+
198
+ unparsed_json = response.body.to_s.empty? ? '{}' : response.body
184
199
 
185
200
  JSON.parse(unparsed_json)
186
201
  end
@@ -111,6 +111,25 @@ describe DnsMadeEasy do
111
111
  end
112
112
  end
113
113
 
114
+ describe '#find' do
115
+ let(:records_for_response) do
116
+ {
117
+ 'data' => [
118
+ { 'name' => 'demo', 'type' => 'A', 'id' => 123},
119
+ { 'name' => 'demo', 'type' => 'A', 'id' => 143}
120
+ ]
121
+ }
122
+ end
123
+
124
+ before do
125
+ subject.stub(:records_for).with('something.wanelo.com').and_return(records_for_response)
126
+ end
127
+
128
+ it 'finds the first record that matches name and type' do
129
+ expect(subject.find('something.wanelo.com', 'demo', 'A')).to eq({ 'name' => 'demo', 'type' => 'A', 'id' => 123})
130
+ end
131
+ end
132
+
114
133
  describe '#find_record_id' do
115
134
  let(:records_for_response) do
116
135
  {
@@ -132,17 +151,47 @@ describe DnsMadeEasy do
132
151
 
133
152
  describe '#delete_records' do
134
153
  let(:response) { "{}" }
154
+ let(:domain) { 'something.wanelo.com' }
155
+ let(:domain_id) { 123 }
135
156
 
136
- before do
137
- subject.stub(:get_id_by_domain).with('something.wanelo.com').and_return(123)
157
+ context 'with an array of record ids' do
158
+ let(:ids) { [147, 159] }
159
+
160
+ before do
161
+ subject.stub(:get_id_by_domain).with(domain).and_return(domain_id)
162
+
163
+ stub_request(:delete, "https://api.dnsmadeeasy.com/V2.0/dns/managed/#{domain_id}/records?ids=#{ids.join(',')}").
164
+ with(headers: request_headers).
165
+ to_return(:status => 200, :body => response, :headers => {})
166
+ end
167
+
168
+ it 'deletes a list of records from a given domain' do
169
+ expect(subject.delete_records('something.wanelo.com', ids)).to eq({})
170
+ end
138
171
  end
139
172
 
140
- it 'deletes a list of records from a given domain' do
141
- stub_request(:delete, "https://api.dnsmadeeasy.com/V2.0/dns/managed/123/records/").
142
- with(headers: request_headers, body: "[147,159]").
173
+ context 'with an empty array' do
174
+ it 'returns early without deleting anything' do
175
+ expect(subject.delete_records('something.wanelo.com', [])).to eq(nil)
176
+ end
177
+ end
178
+ end
179
+
180
+ describe '#delete_all_records' do
181
+ let(:response) { "{}" }
182
+ let(:domain) { 'something.wanelo.com' }
183
+ let(:domain_id) { 123 }
184
+
185
+ before do
186
+ subject.stub(:get_id_by_domain).with(domain).and_return(domain_id)
187
+
188
+ stub_request(:delete, "https://api.dnsmadeeasy.com/V2.0/dns/managed/#{domain_id}/records").
189
+ with(headers: request_headers).
143
190
  to_return(:status => 200, :body => response, :headers => {})
191
+ end
144
192
 
145
- expect(subject.delete_records('something.wanelo.com', [147, 159])).to eq({})
193
+ it 'deletes all records from the domain' do
194
+ expect(subject.delete_all_records('something.wanelo.com')).to eq({})
146
195
  end
147
196
  end
148
197
 
@@ -242,14 +291,39 @@ describe DnsMadeEasy do
242
291
  to_return(:status => 200, :body => response, :headers => {})
243
292
 
244
293
 
245
- expect(subject.update_record('something.wanelo.com', 21, 'mail', 'A', '1.1.1.1', options = {})).to eq({})
294
+ expect(subject.update_record('something.wanelo.com', 21, 'mail', 'A', '1.1.1.1', {})).to eq({})
246
295
  end
247
296
  end
248
297
 
249
298
  describe "#request" do
250
- it "handles an empty string response" do
251
- Net::HTTP.any_instance.stub(:request).and_return(double(body: ""))
252
- expect(subject.send(:request, "/some_path") { {} }).to eq({})
299
+ before do
300
+ stub_request(:get, "https://api.dnsmadeeasy.com/V2.0/some_path").
301
+ with(headers: request_headers).
302
+ to_return(status: status, body: body, headers: {})
303
+ end
304
+
305
+ let(:response) do
306
+ subject.send(:request, "/some_path") { |uri|
307
+ Net::HTTP::Get.new(uri.path)
308
+ }
309
+ end
310
+
311
+ context "with a 2xx, empty-string response" do
312
+ let(:status) { 200 }
313
+ let(:body) { "" }
314
+
315
+ it "handles gracefully" do
316
+ expect(response).to eq({})
317
+ end
318
+ end
319
+
320
+ context "with a non-2xx response" do
321
+ let(:status) { 400 }
322
+ let(:body) { "<xml> JSON.parse won't like this very much </xml>" }
323
+
324
+ it "raises a Net::HTTPServerException instead of a JSON::ParserError" do
325
+ expect { response }.to raise_exception(Net::HTTPServerException)
326
+ end
253
327
  end
254
328
  end
255
329
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dnsmadeeasy-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnoud Vermeer
@@ -10,34 +10,62 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-08-14 00:00:00.000000000 Z
13
+ date: 2016-06-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ! '>='
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - ! '>='
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: webmock
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ! '>='
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ! '>='
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: bundler
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rubocop
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
41
69
  - !ruby/object:Gem::Version
42
70
  version: '0'
43
71
  description: DNS Made Easy V2.0 REST API client for Ruby. With tests and no dependencies.
@@ -48,9 +76,9 @@ executables: []
48
76
  extensions: []
49
77
  extra_rdoc_files: []
50
78
  files:
51
- - .gitignore
79
+ - ".gitignore"
80
+ - ".rubocop.yml"
52
81
  - Gemfile
53
- - Gemfile.lock
54
82
  - LICENSE.txt
55
83
  - README.md
56
84
  - Rakefile
@@ -68,17 +96,17 @@ require_paths:
68
96
  - lib
69
97
  required_ruby_version: !ruby/object:Gem::Requirement
70
98
  requirements:
71
- - - ! '>='
99
+ - - ">="
72
100
  - !ruby/object:Gem::Version
73
101
  version: '0'
74
102
  required_rubygems_version: !ruby/object:Gem::Requirement
75
103
  requirements:
76
- - - ! '>='
104
+ - - ">="
77
105
  - !ruby/object:Gem::Version
78
106
  version: '0'
79
107
  requirements: []
80
108
  rubyforge_project:
81
- rubygems_version: 2.2.2
109
+ rubygems_version: 2.4.5.1
82
110
  signing_key:
83
111
  specification_version: 4
84
112
  summary: DNS Made Easy V2.0 REST API client for Ruby
data/Gemfile.lock DELETED
@@ -1,32 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- dnsmadeeasy-rest-api (1.0.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- addressable (2.3.6)
10
- crack (0.4.2)
11
- safe_yaml (~> 1.0.0)
12
- diff-lcs (1.2.5)
13
- rspec (2.14.1)
14
- rspec-core (~> 2.14.0)
15
- rspec-expectations (~> 2.14.0)
16
- rspec-mocks (~> 2.14.0)
17
- rspec-core (2.14.8)
18
- rspec-expectations (2.14.5)
19
- diff-lcs (>= 1.1.3, < 2.0)
20
- rspec-mocks (2.14.6)
21
- safe_yaml (1.0.3)
22
- webmock (1.18.0)
23
- addressable (>= 2.3.6)
24
- crack (>= 0.3.2)
25
-
26
- PLATFORMS
27
- ruby
28
-
29
- DEPENDENCIES
30
- dnsmadeeasy-rest-api!
31
- rspec
32
- webmock