ns1 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0eadbc0ca65b4e99e0da1933be0b2d61a6063a93
4
+ data.tar.gz: 869141a53e4107a9bf5169e6da73eac76db4b938
5
+ SHA512:
6
+ metadata.gz: ee7655d6ad2557ad79424358429d6c0c3c07e9d895e5ab6f8a179b14c1a39c8d9b9308228d77395fd1c51d883c80c415d5a778dc313a269c80baecaa02cb3b0e
7
+ data.tar.gz: c07111bf5b6fe83c9bcd40a19f369b955b33d706ad36357cc00f523a722bc597bf8040bebdd56af930d9491e2923771e323d70008c58961ba3f3444b9f94ecf3
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.6
5
+ - 2.4.4
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ns1.gemspec
6
+ gemspec
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ns1 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.5.2)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ crack (0.4.3)
12
+ safe_yaml (~> 1.0.0)
13
+ diff-lcs (1.3)
14
+ hashdiff (0.3.7)
15
+ public_suffix (3.0.2)
16
+ rake (10.5.0)
17
+ rspec (3.7.0)
18
+ rspec-core (~> 3.7.0)
19
+ rspec-expectations (~> 3.7.0)
20
+ rspec-mocks (~> 3.7.0)
21
+ rspec-core (3.7.1)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-expectations (3.7.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-mocks (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-support (3.7.1)
30
+ safe_yaml (1.0.4)
31
+ webmock (3.3.0)
32
+ addressable (>= 2.3.6)
33
+ crack (>= 0.3.2)
34
+ hashdiff
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.16)
41
+ ns1!
42
+ rake (~> 10.0)
43
+ rspec (~> 3.0)
44
+ webmock (~> 3.3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.16.1
@@ -0,0 +1,69 @@
1
+ # NS1
2
+
3
+ [NS1](https://ns1.com/) API Client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ns1'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ns1
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # Instantiate a client
25
+ client = NS1::Client.new("your api key")
26
+
27
+ # ZONES
28
+
29
+ # Get all active zones and basic zone configuration details for each.
30
+ client.zones
31
+
32
+ # Get a single active Zone and its basic configuration details.
33
+ client.zone("example.com")
34
+
35
+ # Create a new DNS zone
36
+ # For details on configuration options, see https://ns1.com/api#create-a-new-dns-zone
37
+ client.create_zone("yourdomain.com", { optional: :params })
38
+
39
+ # Modify basic details of a DNS zone
40
+ client.modify_zone("yourdomain.com", { refresh: 3600 })
41
+
42
+ # Delete an existing DNS zone and all records in the zone
43
+ client.delete_zone("yourdomain.com")
44
+
45
+ # RECORDS
46
+
47
+ # Get full configuration for a DNS record
48
+ client.record("example.com", "www.example.com", "A")
49
+
50
+ # Create a new DNS record in the specified zone, for the specified domain, of the given record type
51
+ # `answers` option is required. For other options, see https://ns1.com/api#create-a-new-dns-record
52
+ client.create_record("yourdomain.com", "www.yourdomain.com", "A", { answers: [] })
53
+
54
+ # Modify configuration details for an existing DNS record.
55
+ client.modify_record("yourdomain.com", "www.yourdomain.com", "A" { use_client_subnet: false })
56
+
57
+ # Remove an existing record and all associated answers and configuration details
58
+ client.delete_record("yourdomain.com", "www.yourdomain.com", "A")
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ns1.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ns1"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module NS1
2
+ end
3
+
4
+ require "ns1/version"
5
+ require "ns1/client"
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir[ File.expand_path('../api/*.rb', __FILE__) ].each { |f| require f }
4
+
5
+ module NS1
6
+ module API
7
+ HTTP_GET = "GET"
8
+ HTTP_POST = "POST"
9
+ HTTP_PUT = "PUT"
10
+ HTTP_DELETE = "DELETE"
11
+
12
+ def self.included(base)
13
+ base.send :include,
14
+ NS1::API::Zones,
15
+ NS1::API::Records
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NS1
4
+ module API
5
+ module Records
6
+ def record(zone, domain, type)
7
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
8
+ raise NS1::MissingParameter, "domain cannot be blank" if blank?(domain)
9
+ raise NS1::MissingParameter, "type cannot be blank" if blank?(type)
10
+ perform_request(HTTP_GET, "/v1/zones/#{zone}/#{domain}/#{type}")
11
+ end
12
+
13
+ def create_record(zone, domain, type, params = {})
14
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
15
+ raise NS1::MissingParameter, "domain cannot be blank" if blank?(domain)
16
+ raise NS1::MissingParameter, "type cannot be blank" if blank?(type)
17
+ validate_required!(params, :answers)
18
+ params = params.merge(zone: zone, domain: domain, type: type)
19
+ perform_request(HTTP_PUT, "/v1/zones/#{zone}/#{domain}/#{type}", params)
20
+ end
21
+
22
+ def modify_record(zone, domain, type, params)
23
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
24
+ raise NS1::MissingParameter, "domain cannot be blank" if blank?(domain)
25
+ raise NS1::MissingParameter, "type cannot be blank" if blank?(type)
26
+ perform_request(HTTP_POST, "/v1/zones/#{zone}/#{domain}/#{type}", params)
27
+ end
28
+
29
+ def delete_record(zone, domain, type)
30
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
31
+ raise NS1::MissingParameter, "domain cannot be blank" if blank?(domain)
32
+ raise NS1::MissingParameter, "type cannot be blank" if blank?(type)
33
+ perform_request(HTTP_DELETE, "/v1/zones/#{zone}/#{domain}/#{type}")
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NS1
4
+ module API
5
+ module Zones
6
+ def zones
7
+ perform_request(HTTP_GET, "/v1/zones")
8
+ end
9
+
10
+ def zone(zone)
11
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
12
+ perform_request(HTTP_GET, "/v1/zones/#{zone}")
13
+ end
14
+
15
+ def create_zone(zone, params = {})
16
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
17
+ params = params.merge(zone: zone)
18
+ perform_request(HTTP_PUT, "/v1/zones/#{zone}", params)
19
+ end
20
+
21
+ def modify_zone(zone, params)
22
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
23
+ perform_request(HTTP_POST, "/v1/zones/#{zone}", params)
24
+ end
25
+
26
+ def delete_zone(zone)
27
+ raise NS1::MissingParameter, "zone cannot be blank" if blank?(zone)
28
+ perform_request(HTTP_DELETE, "/v1/zones/#{zone}")
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ns1/api"
4
+ require "ns1/transport/net_http"
5
+ require "ns1/error"
6
+
7
+ module NS1
8
+ class Client
9
+ include NS1::API
10
+ BASE_URL = "https://api.nsone.net"
11
+
12
+ def initialize(api_key, base_url: BASE_URL, logger: nil)
13
+ @api_key = api_key
14
+ @base_url = base_url
15
+ @logger = logger
16
+ end
17
+
18
+ private
19
+
20
+ def perform_request(method, path, body = nil)
21
+ body = JSON.dump(body) if body.is_a? Hash
22
+ log("[NS1] > #{method} #{path}")
23
+ log("[NS1] > #{body}") if body
24
+ response = transport.request(method, path, body)
25
+ log("[NS1] < #{response.inspect}")
26
+ response
27
+ end
28
+
29
+ def log(message)
30
+ @logger && @logger.debug(message)
31
+ end
32
+
33
+ def transport
34
+ @transport ||= NS1::Transport::NetHttp.new(@base_url, @api_key)
35
+ end
36
+
37
+ def blank?(object)
38
+ object.respond_to?(:empty?) ? !!object.empty? : !object
39
+ end
40
+
41
+ def validate_required!(params, *keys)
42
+ raise ArgumentError, "Paramenters must be a hash" unless params.is_a? Hash
43
+ missing = keys.reject { |key| params.has_key?(key) or params.has_key?(key.to_s) }
44
+ if missing.any?
45
+ raise MissingParameter, "Missing key(s): #{missing.join(", ")}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ module NS1
2
+ class Error < StandardError; end
3
+ class MissingParameter < Error; end
4
+ end
@@ -0,0 +1,7 @@
1
+ module NS1
2
+ module Response
3
+ end
4
+ end
5
+
6
+ require "ns1/response/success"
7
+ require "ns1/response/error"
@@ -0,0 +1,8 @@
1
+ require 'delegate'
2
+
3
+ module NS1
4
+ module Response
5
+ class Base < SimpleDelegator
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ require "ns1/response/base"
2
+
3
+ module NS1
4
+ module Response
5
+ class Error < Base
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,8 @@
1
+ require "ns1/response/base"
2
+
3
+ module NS1
4
+ module Response
5
+ class Success < Base
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NS1
4
+ module Transport
5
+ class Error < StandardError; end
6
+ class ResponseParseError < Error; end
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "json"
6
+ require "uri"
7
+ require "ns1/transport"
8
+ require "ns1/response"
9
+
10
+ module NS1
11
+ module Transport
12
+ class NetHttp
13
+ def initialize(base_url, api_key)
14
+ @base_url = base_url
15
+ @api_key = api_key
16
+ end
17
+
18
+ def request(method, path, body = nil)
19
+ uri = URI.join(@base_url, path)
20
+ Net::HTTP.start(uri.host, uri.port, opts(uri)) do |http|
21
+ response = http.send_request(method, uri, body, headers(body))
22
+ process_response(response)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def process_response(response)
29
+ body = JSON.parse(response.body)
30
+ case response
31
+ when Net::HTTPOK
32
+ NS1::Response::Success.new(body)
33
+ else
34
+ NS1::Response::Error.new(body)
35
+ end
36
+ rescue JSON::ParserError
37
+ raise NS1::Transport::ResponseParseError
38
+ end
39
+
40
+ def opts(uri)
41
+ if uri.scheme == "https"
42
+ {
43
+ use_ssl: true,
44
+ ssl_mode: OpenSSL::SSL::VERIFY_PEER
45
+ }
46
+ end
47
+ end
48
+
49
+ def headers(body)
50
+ extra_headers = body.nil? ? {} : { "Content-Type" => "application/json" }
51
+ default_headers.merge(extra_headers)
52
+ end
53
+
54
+ def default_headers
55
+ { "X-NSONE-Key" => @api_key }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module NS1
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ns1/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ns1"
8
+ spec.version = NS1::VERSION
9
+ spec.authors = ["Esteban Pastorino"]
10
+ spec.email = ["ejpastorino@gmail.com"]
11
+
12
+ spec.summary = %q{NS1 API Client}
13
+ spec.description = %q{NS1 API Client}
14
+ spec.homepage = "https://github.com/kitop/ns1"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "webmock", "~> 3.3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ns1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Esteban Pastorino
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ description: NS1 API Client
70
+ email:
71
+ - ejpastorino@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/ns1.rb
86
+ - lib/ns1/api.rb
87
+ - lib/ns1/api/records.rb
88
+ - lib/ns1/api/zones.rb
89
+ - lib/ns1/client.rb
90
+ - lib/ns1/error.rb
91
+ - lib/ns1/response.rb
92
+ - lib/ns1/response/base.rb
93
+ - lib/ns1/response/error.rb
94
+ - lib/ns1/response/success.rb
95
+ - lib/ns1/transport.rb
96
+ - lib/ns1/transport/net_http.rb
97
+ - lib/ns1/version.rb
98
+ - ns1.gemspec
99
+ homepage: https://github.com/kitop/ns1
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.6.14
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: NS1 API Client
122
+ test_files: []