glare 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5c89e74d7b744713d246968f87784dd5c649e43
4
- data.tar.gz: 4799fd973bad72704aef4e8885e764398170ca41
3
+ metadata.gz: a5f191a543603fccd5d3f0b0c8de96a5f91cd657
4
+ data.tar.gz: 25c831074962b9946ccde5958252820987120087
5
5
  SHA512:
6
- metadata.gz: 39722068385af12ec85cfd955ca49b1c99bff206b675575b52f3655e0d63247c4bf4ee4708419964dcff3bccb3a69f0cf9b964f8467935b31df57200f766f419
7
- data.tar.gz: 671ad8b1f9b4cdbf9314d9da5755e03efd533cb362190da230aef4feb7535aa00df79c79f500cc8c8a2b61135d1572a7665ffc05658edeb9730f17c778e40500
6
+ metadata.gz: 505288e7e9ec1ea1ab83d81034962e7eeb77b29ecf29a6c681448da5a806a483287b3f4c0e7ebe4246a31b9c781493fe48957254d7f4f2001d89d527d5636519
7
+ data.tar.gz: 053d5b8a1b9a195eae1ccc02c99879f084e54b3351b00c50e5ab966b561a8d59c2c46ed1417120b5e41cbddee3e8d6f376bf499ed0bf5ae42e1bc64b5742d311
data/README.md CHANGED
@@ -27,10 +27,52 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
+ In order to configure credentials used to interact with Cloudflare API you will need to setup the following environment variables:
31
+
32
+ - `CF_EMAIL`: Email used to create a Cloudflare account
33
+ - `CF_AUTH_KEY`: Auth key of the given user
34
+
35
+ Additionally, you can set other environment variables:
36
+
37
+ - `CF_DEBUG`: Set to `1` to enable HTTP requests' debug
38
+
30
39
  ### Create/update DNS record
31
40
 
41
+ ```ruby
42
+ require 'glare'
43
+
44
+ Glare.register('example.domain.com', 'destination.com' ,'CNAME')
45
+ ```
46
+
47
+ Where:
48
+ - `example.domain.com`: Name of the record to create
49
+ - `destination.com`: Name(s) of the values of the record
50
+ - `CNAME`: Type of the DNS record
51
+
32
52
  ### Delete DNS record
33
53
 
54
+ ```ruby
55
+ require 'glare'
56
+
57
+ Glare.deregister('example.domain.com', 'CNAME')
58
+ ```
59
+
60
+ Where:
61
+ - `example.domain.com`: Name of the record to destroy
62
+ - `CNAME`: Type of the DNS record
63
+
64
+ ### Resolve DNS record
65
+
66
+ ```ruby
67
+ require 'glare'
68
+
69
+ Glare.resolve('example.domain.com', 'CNAME')
70
+ ```
71
+
72
+ Where:
73
+ - `example.domain.com`: Name of the record to resolve
74
+ - `CNAME`: Type of the DNS record
75
+
34
76
  ## Development
35
77
 
36
78
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['josacar@users.noreply.github.com', 'olopez@users.noreply.github.com']
11
11
 
12
12
  spec.summary = 'API client for CloudFlare v4 API'
13
- spec.homepage = 'https://github.com/peertransfer/cf'
13
+ spec.homepage = 'https://github.com/peertransfer/glare'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  files = Dir['lib/**/*.rb']
@@ -3,9 +3,9 @@ require 'glare/version'
3
3
  require 'glare/credentials'
4
4
  require 'glare/client'
5
5
  require 'glare/domain'
6
- require 'glare/result'
6
+ require 'glare/api_response'
7
7
  require 'glare/dns_record'
8
- require 'glare/dns_records'
8
+ require 'glare/cf_dns_records'
9
9
 
10
10
  module Glare
11
11
  class << self
@@ -0,0 +1,18 @@
1
+ module Glare
2
+ class ApiResponse
3
+ def initialize(response)
4
+ @response = response
5
+ end
6
+
7
+ def result
8
+ content['result']
9
+ end
10
+
11
+ private
12
+
13
+ def content
14
+ @response.content
15
+ end
16
+ end
17
+ private_constant :ApiResponse
18
+ end
@@ -0,0 +1,68 @@
1
+ module Glare
2
+ class CfDnsRecord
3
+ def initialize(id:, name:, type:, content:)
4
+ @id = id
5
+ @name = name
6
+ @type = type
7
+ @content = content
8
+ end
9
+ attr_reader :id, :name, :type, :content
10
+ end
11
+
12
+ class CfDnsRecords
13
+ class << self
14
+ def from_result(api_result)
15
+ response = ApiResponse.new(api_result)
16
+ result = response.result
17
+
18
+ records = result.map do |item|
19
+ CfDnsRecord.new(
20
+ id: item['id'],
21
+ name: item['name'],
22
+ type: item['type'],
23
+ content: item['content']
24
+ )
25
+ end
26
+
27
+ new(records)
28
+ end
29
+
30
+ def empty
31
+ new([])
32
+ end
33
+ end
34
+
35
+ def initialize(records)
36
+ @records = records
37
+ end
38
+
39
+ def to_update(desired_records)
40
+ @records.reject do |record|
41
+ desired_records.any? { |r| r.content == record.content }
42
+ end
43
+ end
44
+
45
+ def count
46
+ @records.count
47
+ end
48
+
49
+ def contents
50
+ @records.map(&:content)
51
+ end
52
+
53
+ def each
54
+ @records.each { |record| yield(record) }
55
+ end
56
+
57
+ def to_delete(target_number)
58
+ records_to_delete = count - target_number
59
+ return CfDnsRecords.empty if records_to_delete < 0
60
+
61
+ @records.last(records_to_delete)
62
+ end
63
+
64
+ def to_create(desired_records)
65
+ desired_records.drop(count)
66
+ end
67
+ end
68
+ end
@@ -6,7 +6,6 @@ module Glare
6
6
 
7
7
  def initialize(email, auth_key)
8
8
  @headers = {
9
- 'Content-Type' => 'application/json',
10
9
  'X-Auth-Email' => email,
11
10
  'X-Auth-Key' => auth_key
12
11
  }
@@ -1,92 +1,8 @@
1
+ require 'glare/domain/zone'
2
+ require 'glare/domain/record'
3
+
1
4
  module Glare
2
5
  class Domain
3
- class Zone
4
- def initialize(client, fqdn)
5
- @client = client
6
- @fqdn = fqdn
7
- end
8
-
9
- def records(type)
10
- records = record_search(type)
11
- DnsRecords.new(records)
12
- end
13
-
14
- def id
15
- return @id if @id
16
- zone_search = @client.get('/zones', name: registered_domain)
17
- @id = Result.new(zone_search).first_result_id
18
- end
19
-
20
- private
21
-
22
- def registered_domain
23
- PublicSuffix.parse(@fqdn).domain
24
- end
25
-
26
- def record_search(type)
27
- @client.get("/zones/#{id}/dns_records", name: @fqdn, type: type)
28
- end
29
- end
30
-
31
- class Record
32
- class << self
33
- def register(client, zone, dns_records)
34
- @client = client
35
- existing_records = zone.records(dns_records.first.type)
36
- zone_id = zone.id
37
-
38
- update(zone_id, dns_records, existing_records)
39
- end
40
-
41
- def deregister(client, zone, dns_records)
42
- @client = client
43
- zone_id = zone.id
44
-
45
- delete(zone_id, dns_records)
46
- end
47
-
48
- private
49
-
50
- def delete(zone_id, dns_records)
51
- dns_records.each do |record|
52
- @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
53
- end
54
- end
55
-
56
- def update(zone_id, dns_records, existing_records)
57
- update_current_records(zone_id, dns_records, existing_records)
58
- delete_uneeded_records(zone_id, dns_records, existing_records)
59
- create_new_records(zone_id, dns_records, existing_records)
60
- end
61
-
62
- def update_current_records(zone_id, dns_records, existing_records)
63
- records_to_update = existing_records.records_to_update(dns_records)
64
- updates = records_to_update.zip(dns_records)
65
- updates.each do |existing_record, dns_record|
66
- @client.put("/zones/#{zone_id}/dns_records/#{existing_record.id}", dns_record.to_h)
67
- end
68
- end
69
-
70
- def delete_uneeded_records(zone_id, dns_records, existing_records)
71
- records_to_delete = existing_records.records_to_delete(dns_records.count)
72
- records_to_delete.each do |record|
73
- @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
74
- end
75
- end
76
-
77
- def create_new_records(zone_id, dns_records, existing_records)
78
- records_to_create = existing_records.records_to_create(dns_records)
79
- create(zone_id, records_to_create)
80
- end
81
-
82
- def create(zone_id, dns_records)
83
- dns_records.each do |dns_record|
84
- @client.post("/zones/#{zone_id}/dns_records", dns_record.to_h)
85
- end
86
- end
87
- end
88
- end
89
-
90
6
  def initialize(client)
91
7
  @client = client
92
8
  end
@@ -102,8 +18,8 @@ module Glare
102
18
 
103
19
  def resolve(fqdn, type)
104
20
  zone = Zone.new(@client, fqdn)
105
- result = zone.records(type)
106
- result.contents
21
+ records = zone.records(type)
22
+ records.contents
107
23
  end
108
24
 
109
25
  def deregister(fqdn, type)
@@ -0,0 +1,37 @@
1
+ require 'glare/domain/cf_zones'
2
+
3
+ module Glare
4
+ class Domain
5
+ class CfZones
6
+ def self.from_result(api_response)
7
+ response = ApiResponse.new(api_response)
8
+ result = response.result
9
+
10
+ zones = result.map do |item|
11
+ CfZone.new(
12
+ id: item['id'],
13
+ name: item['name']
14
+ )
15
+ end
16
+
17
+ new(zones)
18
+ end
19
+
20
+ def initialize(zones)
21
+ @zones = zones
22
+ end
23
+
24
+ def first
25
+ @zones.first
26
+ end
27
+ end
28
+
29
+ class CfZone
30
+ def initialize(id:, name:)
31
+ @id = id
32
+ @name = name
33
+ end
34
+ attr_reader :id, :name
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,62 @@
1
+ module Glare
2
+ class Domain
3
+ class Record
4
+ class << self
5
+ def register(client, zone, dns_records)
6
+ @client = client
7
+ existing_records = zone.records(dns_records.first.type)
8
+ zone_id = zone.id
9
+
10
+ update(zone_id, dns_records, existing_records)
11
+ end
12
+
13
+ def deregister(client, zone, dns_records)
14
+ @client = client
15
+ zone_id = zone.id
16
+
17
+ delete(zone_id, dns_records)
18
+ end
19
+
20
+ private
21
+
22
+ def delete(zone_id, dns_records)
23
+ dns_records.each do |record|
24
+ @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
25
+ end
26
+ end
27
+
28
+ def update(zone_id, dns_records, existing_records)
29
+ update_current_records(zone_id, dns_records, existing_records)
30
+ delete_uneeded_records(zone_id, dns_records, existing_records)
31
+ create_new_records(zone_id, dns_records, existing_records)
32
+ end
33
+
34
+ def update_current_records(zone_id, dns_records, existing_records)
35
+ records_to_update = existing_records.to_update(dns_records)
36
+ updates = records_to_update.zip(dns_records)
37
+ updates.each do |existing_record, dns_record|
38
+ @client.put("/zones/#{zone_id}/dns_records/#{existing_record.id}", dns_record.to_h)
39
+ end
40
+ end
41
+
42
+ def delete_uneeded_records(zone_id, dns_records, existing_records)
43
+ records_to_delete = existing_records.to_delete(dns_records.count)
44
+ records_to_delete.each do |record|
45
+ @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
46
+ end
47
+ end
48
+
49
+ def create_new_records(zone_id, dns_records, existing_records)
50
+ records_to_create = existing_records.to_create(dns_records)
51
+ create(zone_id, records_to_create)
52
+ end
53
+
54
+ def create(zone_id, dns_records)
55
+ dns_records.each do |dns_record|
56
+ @client.post("/zones/#{zone_id}/dns_records", dns_record.to_h)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,33 @@
1
+ require 'glare/domain/cf_zones'
2
+
3
+ module Glare
4
+ class Domain
5
+ class Zone
6
+ def initialize(client, fqdn)
7
+ @client = client
8
+ @fqdn = fqdn
9
+ end
10
+
11
+ def records(type)
12
+ api_result = record_search(type)
13
+ CfDnsRecords.from_result(api_result)
14
+ end
15
+
16
+ def id
17
+ return @id if @id
18
+ zone_search = @client.get('/zones', name: registered_domain)
19
+ @id = CfZones.from_result(zone_search).first.id
20
+ end
21
+
22
+ private
23
+
24
+ def registered_domain
25
+ PublicSuffix.parse(@fqdn).domain
26
+ end
27
+
28
+ def record_search(type)
29
+ @client.get("/zones/#{id}/dns_records", name: @fqdn, type: type)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Glare
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Luis Salas
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-20 00:00:00.000000000 Z
12
+ date: 2016-07-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -96,12 +96,15 @@ files:
96
96
  - README.md
97
97
  - glare.gemspec
98
98
  - lib/glare.rb
99
+ - lib/glare/api_response.rb
100
+ - lib/glare/cf_dns_records.rb
99
101
  - lib/glare/client.rb
100
102
  - lib/glare/credentials.rb
101
103
  - lib/glare/dns_record.rb
102
- - lib/glare/dns_records.rb
103
104
  - lib/glare/domain.rb
104
- - lib/glare/result.rb
105
+ - lib/glare/domain/cf_zones.rb
106
+ - lib/glare/domain/record.rb
107
+ - lib/glare/domain/zone.rb
105
108
  - lib/glare/version.rb
106
109
  - spec/delete_domain_spec.rb
107
110
  - spec/fixtures/empty_result.json
@@ -110,7 +113,7 @@ files:
110
113
  - spec/resolve_domain_spec.rb
111
114
  - spec/spec_helper.rb
112
115
  - spec/units/glare_spec.rb
113
- homepage: https://github.com/peertransfer/cf
116
+ homepage: https://github.com/peertransfer/glare
114
117
  licenses:
115
118
  - MIT
116
119
  metadata: {}
@@ -135,10 +138,10 @@ signing_key:
135
138
  specification_version: 4
136
139
  summary: API client for CloudFlare v4 API
137
140
  test_files:
141
+ - spec/units/glare_spec.rb
138
142
  - spec/delete_domain_spec.rb
139
143
  - spec/resolve_domain_spec.rb
140
144
  - spec/spec_helper.rb
141
- - spec/units/glare_spec.rb
145
+ - spec/fixtures/wadus_records.json
142
146
  - spec/fixtures/empty_result.json
143
147
  - spec/fixtures/list_zone.json
144
- - spec/fixtures/wadus_records.json
@@ -1,56 +0,0 @@
1
- module Glare
2
- class CfDnsRecord
3
- def initialize(id:, name:, type:, content:)
4
- @id = id
5
- @name = name
6
- @type = type
7
- @content = content
8
- end
9
- attr_reader :id, :name, :type, :content
10
- end
11
-
12
- class DnsRecords < Result
13
- def initialize(result)
14
- super(result)
15
- @records = records
16
- end
17
-
18
- def records_to_update(desired_records)
19
- @records.reject do |record|
20
- desired_records.any? { |r| r.content == record.content }
21
- end
22
- end
23
-
24
- def count
25
- @records.count
26
- end
27
-
28
- def each
29
- @records.each { |record| yield(record) }
30
- end
31
-
32
- def records_to_delete(targer_number)
33
- records_to_delete = count - targer_number
34
- return [] if records_to_delete < 0
35
-
36
- @records.pop(records_to_delete)
37
- end
38
-
39
- def records_to_create(desired_records)
40
- desired_records.drop(count)
41
- end
42
-
43
- private
44
-
45
- def records
46
- result['result'].map do |item|
47
- CfDnsRecord.new(
48
- id: item['id'],
49
- name: item['name'],
50
- type: item['type'],
51
- content: item['content']
52
- )
53
- end
54
- end
55
- end
56
- end
@@ -1,26 +0,0 @@
1
- module Glare
2
- class Result
3
- def initialize(result)
4
- @result = result
5
- end
6
-
7
- def ocurrences
8
- result['result_info']['count'].to_i
9
- end
10
-
11
- def first_result_id
12
- result['result'].first['id']
13
- end
14
-
15
- def contents
16
- Array(result['result']).map { |item| item['content'] }
17
- end
18
-
19
- private
20
-
21
- def result
22
- @result.content
23
- end
24
- end
25
- private_constant :Result
26
- end