splicer-dynect 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ splicer-dynect
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in splicer-dynect.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matthew Johnston
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Splicer::Dynect
2
+
3
+ A Splicer adapter to interact with Dynect.
4
+
5
+
6
+ ## Installation
7
+
8
+ ```sh
9
+ gem install splicer
10
+ gem install splicer-dynect
11
+ ```
12
+
13
+ ## Using
14
+
15
+ It's fairly easy to set up. Simply register it with Splicer and everything
16
+ should work good.
17
+
18
+ ```rb
19
+ Splicer.configure do |config|
20
+ config.register(Splicer::Dynect::Config.new('customer','user','pass'))
21
+ end
22
+ ```
23
+
24
+
25
+ ## Contributing
26
+
27
+ * Fork it
28
+ * Create a feature branch
29
+ * Submit a pull request
30
+ * Wait for feedback and possible merge
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,137 @@
1
+ module Splicer
2
+ module Dynect
3
+
4
+ class Client
5
+ attr_reader :base_url, :headers, :customer, :username, :password
6
+
7
+ def initialize(customer, username, password)
8
+ @customer = customer
9
+ @username = username
10
+ @password = password
11
+
12
+ @base_url = 'https://api-v4.dynect.net/REST'
13
+ @headers = {
14
+ 'Content-Type' => 'application/json',
15
+ 'API-Version' => '3.4.0'
16
+ }
17
+ end
18
+
19
+ # @param [String] resource
20
+ # @param [Hash] payload
21
+ #
22
+ # @return [Hash]
23
+ def post(resource, payload={})
24
+ execute({
25
+ method: :post,
26
+ url: resource_url(resource),
27
+ payload: payload.to_json,
28
+ headers: @headers,
29
+ max_redirects: 20
30
+ })
31
+ rescue RestClient::Exception => error
32
+ raise Splicer::Errors::RequestError.new(error)
33
+ end
34
+
35
+ # @param [String] resource
36
+ #
37
+ # @return [Hash]
38
+ def get(resource)
39
+ execute({
40
+ method: :get,
41
+ url: resource_url(resource),
42
+ headers: @headers,
43
+ max_redirects: 20
44
+ })
45
+ rescue RestClient::Exception => error
46
+ raise Splicer::Errors::RequestError.new(error)
47
+ end
48
+
49
+ # @param [String] resource
50
+ # @param [Hash] payload
51
+ #
52
+ # @return [Hash]
53
+ def put(resource, payload={})
54
+ execute({
55
+ method: :put,
56
+ url: resource_url(resource),
57
+ payload: payload.to_json,
58
+ headers: @headers
59
+ })
60
+ rescue RestClient::Exception => error
61
+ raise Splicer::Errors::RequestError.new(error)
62
+ end
63
+
64
+ # @param [String] resource
65
+ #
66
+ # @return [Hash]
67
+ def delete(resource)
68
+ execute({
69
+ method: :delete,
70
+ url: resource_url(resource),
71
+ headers: @headers
72
+ })
73
+ rescue RestClient::Exception => error
74
+ raise Splicer::Errors::RequestError.new(error)
75
+ end
76
+
77
+ # Creates a client's session
78
+ #
79
+ # @return [Boolean]
80
+ def login
81
+ return true if logged_in?
82
+
83
+ resp = post('Session', {
84
+ user_name: @username,
85
+ password: @password,
86
+ customer_name: @customer
87
+ })
88
+ @headers['Auth-Token'] = resp['data']['token']
89
+ true
90
+ end
91
+
92
+ # Keeps the session alive
93
+ #
94
+ # @return [Boolean] True if successful
95
+ def noop
96
+ return false unless logged_in?
97
+
98
+ put('Session')
99
+ true
100
+ end
101
+
102
+ # Destroys the client's session
103
+ #
104
+ # @return [Boolean]
105
+ def logout
106
+ return false unless logged_in?
107
+ delete('Session')
108
+ @headers.delete('Auth-Token')
109
+ true
110
+ end
111
+
112
+ private
113
+
114
+ # @return [Boolean]
115
+ def logged_in?
116
+ !!@headers['Auth-Token']
117
+ end
118
+
119
+ # Wrapper around RestClient::Request.execute method
120
+ #
121
+ # @param [Hash] args
122
+ # @raise [Splicer::Errors::RequestError] when the request fails
123
+ # @return [Hash]
124
+ def execute(args={})
125
+ JSON.parse(RestClient::Request.execute(args))
126
+ rescue RestClient::Exception => error
127
+ raise Splicer::Errors::RequestError.new(error)
128
+ end
129
+
130
+ def resource_url resource
131
+ [@base_url, resource.gsub(/^\//,'').gsub(/REST\//,'')].join('/')
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,21 @@
1
+ module Splicer
2
+ module Dynect
3
+
4
+ # A simple configuration data structure
5
+ # @author Matthew A. Johnston <warmwaffles@gmail.com>
6
+ class Config
7
+ attr_reader :username, :password, :customer
8
+
9
+ def initialize(customer, username, password)
10
+ @customer = customer
11
+ @username = username
12
+ @password = password
13
+ end
14
+
15
+ def provider
16
+ Provider.new(self)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module Splicer
2
+ module Dynect
3
+
4
+ class Provider
5
+ def initialize(config)
6
+ @client = Client.new(config.customer, config.username, config.password)
7
+ end
8
+
9
+ def publish(zone, method)
10
+ @client.login
11
+
12
+ # Merge zone
13
+ # Publish zone
14
+
15
+ @client.logout
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Splicer
2
+ module Dynect
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'splicer'
2
+
3
+ require 'json'
4
+ require 'restclient'
5
+
6
+ require 'splicer/dynect/version'
7
+ require 'splicer/dynect/config'
8
+ require 'splicer/dynect/provider'
9
+ require 'splicer/dynect/client'
10
+
11
+ module Splicer
12
+ module Dynect
13
+ end
14
+ end
15
+
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'splicer'
3
+ require 'splicer/dynect'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe Splicer::Dynect::Client do
4
+ let(:client) { Splicer::Dynect::Client.new('customer','username','password') }
5
+ describe '#initialize' do
6
+ subject { client }
7
+ its(:customer) { should eq('customer') }
8
+ its(:username) { should eq('username') }
9
+ its(:password) { should eq('password') }
10
+ its(:base_url) { should eq('https://api-v4.dynect.net/REST') }
11
+ end
12
+
13
+ describe '#post' do
14
+ subject { client.post('resource', { data: 'something' }) }
15
+
16
+ context 'when Dynect returns with success' do
17
+ before { client.stub(:execute).and_return({}) }
18
+ it { should eq({})}
19
+ end
20
+
21
+ context 'when Dynect returns with an error' do
22
+ before { client.stub(:execute).and_raise(RestClient::Exception.new) }
23
+ it 'should raise a Splicer::Errors::RequestError' do
24
+ expect { subject }.to raise_error(Splicer::Errors::RequestError)
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#get' do
30
+ subject { client.get('resource') }
31
+
32
+ context 'when Dynect returns with success' do
33
+ before { client.stub(:execute).and_return({}) }
34
+ it { should eq({})}
35
+ end
36
+
37
+ context 'when Dynect returns with an error' do
38
+ before { client.stub(:execute).and_raise(RestClient::Exception.new) }
39
+ it 'should raise a Splicer::Errors::RequestError' do
40
+ expect { subject }.to raise_error(Splicer::Errors::RequestError)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#put' do
46
+ subject { client.put('resource', { data: 'something' }) }
47
+
48
+ context 'when Dynect returns with success' do
49
+ before { client.stub(:execute).and_return({}) }
50
+ it { should eq({})}
51
+ end
52
+
53
+ context 'when Dynect returns with an error' do
54
+ before { client.stub(:execute).and_raise(RestClient::Exception.new) }
55
+ it 'should raise a Splicer::Errors::RequestError' do
56
+ expect { subject }.to raise_error(Splicer::Errors::RequestError)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#delete' do
62
+ subject { client.delete('resource') }
63
+
64
+ context 'when Dynect returns with success' do
65
+ before { client.stub(:execute).and_return({}) }
66
+ it { should eq({})}
67
+ end
68
+
69
+ context 'when Dynect returns with an error' do
70
+ before { client.stub(:execute).and_raise(RestClient::Exception.new) }
71
+ it 'should raise a Splicer::Errors::RequestError' do
72
+ expect { subject }.to raise_error(Splicer::Errors::RequestError)
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#login' do
78
+ subject { client.login }
79
+
80
+ context 'when logged in' do
81
+ before { client.stub(:logged_in?).and_return(true) }
82
+ it { should eq(true) }
83
+ end
84
+
85
+ context 'when not logged in' do
86
+ let(:data) { {'data' => {'token' => 'blah'}} }
87
+ before { client.stub(:post).and_return(data) }
88
+
89
+ it { should eq(true) }
90
+ it 'should set the Auth-Token' do
91
+ subject
92
+ expect(client.headers['Auth-Token']).to eq('blah')
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '#noop' do
98
+ subject { client.noop }
99
+
100
+ context 'when logged in' do
101
+ before do
102
+ client.stub(:logged_in?).and_return(true)
103
+ client.stub(:put).and_return({})
104
+ end
105
+ it { should eq(true) }
106
+ end
107
+
108
+ context 'when not logged in' do
109
+ before { client.stub(:logged_in?).and_return(false) }
110
+ it { should eq(false) }
111
+ end
112
+ end
113
+
114
+ describe '#logout' do
115
+ subject { client.logout }
116
+
117
+ context 'when logged in' do
118
+ before do
119
+ client.headers['Auth-Token'] = 'blah'
120
+ client.stub(:delete).and_return({})
121
+ end
122
+ it { should eq(true) }
123
+ it 'should unset the Auth-Token' do
124
+ subject
125
+ expect(client.headers['Auth-Token']).to eq(nil)
126
+ end
127
+ end
128
+
129
+ context 'when not logged in' do
130
+ before { client.stub(:logged_in?).and_return(false) }
131
+ it { should eq(false) }
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Splicer::Dynect::Config do
4
+ let(:config) { Splicer::Dynect::Config.new('customer','user','password') }
5
+ describe '#initialize' do
6
+ subject { config }
7
+ its(:customer) { should eq('customer') }
8
+ its(:username) { should eq('user') }
9
+ its(:password) { should eq('password') }
10
+ end
11
+
12
+ describe '#provider' do
13
+ subject { config.provider }
14
+ it { should be_a(Splicer::Dynect::Provider) }
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Splicer::Dynect::Provider do
4
+ pending
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'splicer/dynect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "splicer-dynect"
8
+ spec.version = Splicer::Dynect::VERSION
9
+ spec.authors = ["Matthew Johnston"]
10
+ spec.email = ["warmwaffles@gmail.com"]
11
+ spec.description = %q{The splicer adapter for interacting Dynect}
12
+ spec.summary = %q{The splicer adapter for interacting Dynect}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency('splicer')
22
+ spec.add_dependency('rest-client')
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: splicer-dynect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Johnston
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: splicer
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: The splicer adapter for interacting Dynect
95
+ email:
96
+ - warmwaffles@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .ruby-gemset
104
+ - .ruby-version
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/splicer/dynect.rb
110
+ - lib/splicer/dynect/client.rb
111
+ - lib/splicer/dynect/config.rb
112
+ - lib/splicer/dynect/provider.rb
113
+ - lib/splicer/dynect/version.rb
114
+ - spec/spec_helper.rb
115
+ - spec/splicer/dynect/client_spec.rb
116
+ - spec/splicer/dynect/config_spec.rb
117
+ - spec/splicer/dynect/provider_spec.rb
118
+ - splicer-dynect.gemspec
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ segments:
133
+ - 0
134
+ hash: -2888442058802865494
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ segments:
142
+ - 0
143
+ hash: -2888442058802865494
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.25
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: The splicer adapter for interacting Dynect
150
+ test_files:
151
+ - spec/spec_helper.rb
152
+ - spec/splicer/dynect/client_spec.rb
153
+ - spec/splicer/dynect/config_spec.rb
154
+ - spec/splicer/dynect/provider_spec.rb