vertebrae 0.1.6 → 0.2.0

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: 7301e0ce2d4d44f8ef03d76adcbed3e63dbfafa1
4
- data.tar.gz: 8aac4e0d3b3c8249bc84fd125446e98a4c681d64
3
+ metadata.gz: 53a761abff5b9feda04b6f00fb12fbfcb50691b6
4
+ data.tar.gz: cf986de1d3ae7e4796d0e1865eeb2ccd90bea05e
5
5
  SHA512:
6
- metadata.gz: 1e7d28426d484aaa3a6c79c07a4336f4b4eacd14e8e4c8b32d4d834ced69c122f3f6a0d2b779bc8038ebeffa1153252f6a412aa1a49a86c56c9d47a5b0c2d5af
7
- data.tar.gz: f1d227611bb84986328dcce328f8874228c9507131505c4783b876e2ff1a206f7adf53598af6f09f7d3a07be019c61f9f5531de1fb1cddaf3675f2c742f90259
6
+ metadata.gz: a4b589b38d229a5b08f700a5618049ea48be1b39038cd78186a83703bf4bfed3b748a3418eb1c174cd8d682253a175dadb3bc98985f67dcc15257f5bf99d9ee9
7
+ data.tar.gz: cb9ce156956196a0bf6feceaa1067611064e6bb1cf20c0548d830d7b35868f73275e06b1ae67a0ae9893b3742669769aba1936010fba7644fb764ecfe72d383f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.2.0
data/lib/api.rb CHANGED
@@ -1,154 +1,31 @@
1
1
  module Vertebrae
2
2
  class API
3
-
4
- include Connection
5
3
  include Request
6
- include Authorization
7
-
8
- attr_reader *Configuration.keys
9
-
10
- attr_accessor :current_options
11
4
 
12
- # Callback to update current configuration options
13
- class_eval do
14
- Configuration.keys.each do |key|
15
- define_method "#{key}=" do |arg|
16
- self.instance_variable_set("@#{key}", arg)
17
- self.current_options.merge!({:"#{key}" => arg})
18
- end
19
- end
20
- end
5
+ attr_accessor :connection
21
6
 
22
7
  # Create new API
23
8
  #
24
9
  def initialize(options={}, &block)
25
- setup(options)
26
- yield_or_eval(&block) if block_given?
27
- end
10
+ options.merge!(default_options)
28
11
 
29
-
30
- def yield_or_eval(&block)
31
- return unless block
32
- block.arity > 0 ? yield(self) : self.instance_eval(&block)
33
- end
34
-
35
- # Configure options and process basic authorization
36
- #
37
- def setup(options={})
38
- options = Vertebrae::Base.options.merge(options)
39
- self.current_options = options
40
- Configuration.keys.each do |key|
41
- send("#{key}=", options[key])
42
- end
43
- process_basic_auth(options[:basic_auth])
44
- end
45
-
46
- # Extract login and password from basic_auth parameter
47
- #
48
- def process_basic_auth(auth)
49
- case auth
50
- when String
51
- self.username, self.password = auth.split(':', 2)
52
- when Hash
53
- self.username = auth[:username]
54
- self.password = auth[:password]
55
- end
56
- end
57
-
58
- # Responds to attribute query or attribute clear
59
- def method_missing(method, *args, &block) # :nodoc:
60
- case method.to_s
61
- when /^(.*)\?$/
62
- return !!self.send($1.to_s)
63
- when /^clear_(.*)$/
64
- self.send("#{$1.to_s}=", nil)
65
- else
66
- super
67
- end
68
- end
69
-
70
- # Acts as setter and getter for api requests arguments parsing.
71
- #
72
- # Returns Arguments instance.
73
- #
74
- def arguments(args=(not_set = true), options={}, &block)
75
- if not_set
76
- @arguments
77
- else
78
- @arguments = Arguments.new(self, options).parse(*args, &block)
79
- end
80
- end
81
-
82
- # Scope for passing request required arguments.
83
- #
84
- def with(args)
85
- case args
86
- when Hash
87
- set args
88
- when /.*\/.*/i
89
- user, repo = args.split('/')
90
- set :user => user, :repo => repo
91
- else
92
- ::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
93
- end
94
- end
95
-
96
- # Set an option to a given value
97
- def set(option, value=(not_set=true), ignore_setter=false, &block)
98
- raise ArgumentError, 'value not set' if block and !not_set
99
- return self if !not_set and value.nil?
100
-
101
- if not_set
102
- set_options option
103
- return self
104
- end
105
-
106
- if respond_to?("#{option}=") and not ignore_setter
107
- return __send__("#{option}=", value)
108
- end
109
-
110
- define_accessors option, value
111
- self
112
- end
113
-
114
- private
115
-
116
- def extract_id_from_response(resp)
117
- extract_id_from_location(resp.response.headers["location"])
118
- end
119
-
120
- def extract_id_from_location(location)
121
- location.scan(/\/(\d+)\/$/).first.first
12
+ yield_or_eval(&block) if block_given?
13
+ self.connection = Connection.new(options)
14
+ self.connection.configuration.process_basic_auth(options[:basic_auth])
15
+ setup
122
16
  end
123
17
 
124
- # Set multiple options
125
- #
126
- def set_options(options)
127
- unless options.respond_to?(:each)
128
- raise ArgumentError, 'cannot iterate over value'
129
- end
130
- options.each { |key, value| set(key, value) }
18
+ def setup
131
19
  end
132
20
 
133
- def define_accessors(option, value)
134
- setter = proc { |val| set option, val, true }
135
- getter = proc { value }
136
21
 
137
- define_singleton_method("#{option}=", setter) if setter
138
- define_singleton_method(option, getter) if getter
22
+ def default_options
23
+ {}
139
24
  end
140
25
 
141
- # Dynamically define a method for setting request option
142
- #
143
- def define_singleton_method(method_name, content=Proc.new)
144
- (class << self; self; end).class_eval do
145
- undef_method(method_name) if method_defined?(method_name)
146
- if String === content
147
- class_eval("def #{method_name}() #{content}; end")
148
- else
149
- define_method(method_name, &content)
150
- end
151
- end
26
+ def yield_or_eval(&block)
27
+ return unless block
28
+ block.arity > 0 ? yield(self) : self.instance_eval(&block)
152
29
  end
153
30
 
154
31
  end
data/lib/authorization.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  module Vertebrae
2
2
  module Authorization
3
+ def username?
4
+ username.present?
5
+ end
6
+
7
+ def password?
8
+ password.present?
9
+ end
3
10
 
4
11
  # Check whether authentication credentials are present
5
12
  def authenticated?
data/lib/base.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  module Vertebrae
2
2
  module Base
3
- extend Configuration
4
-
5
-
6
3
  def logger
7
4
  @@logger ||= Logger.new(STDOUT)
8
5
  end
data/lib/configuration.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Vertebrae
2
- module Configuration
2
+ class Configuration
3
3
  include Vertebrae::Constants
4
+ include ActiveSupport::Inflector
5
+ include Authorization
4
6
 
5
7
  VALID_OPTIONS_KEYS = [
6
8
  :adapter,
@@ -45,64 +47,64 @@ module Vertebrae
45
47
  DEFAULT_CONTENT_TYPE = 'application/json'.freeze
46
48
 
47
49
 
48
- attr_accessor *VALID_OPTIONS_KEYS
50
+ VALID_OPTIONS_KEYS.each do | key |
51
+ define_method("default_#{key}".intern) { default_options[key] }
52
+ end
49
53
 
50
- # Convenience method to allow for global setting of configuration options
51
- def configure
52
- yield self
54
+ VALID_OPTIONS_KEYS.each do | key |
55
+ define_method(key) do
56
+ options[key] || self.send("default_#{key}")
57
+ end
53
58
  end
54
59
 
55
- def self.extended(base)
56
- base.reset!
60
+ VALID_OPTIONS_KEYS.each do | key |
61
+ define_method("#{key}=".intern) do |value|
62
+ options[key] = value
63
+ end
57
64
  end
58
65
 
59
- class << self
60
- def keys
61
- VALID_OPTIONS_KEYS
66
+ attr_accessor :options
67
+ attr_accessor :default_options
68
+
69
+ def initialize(options)
70
+ self.options = options
71
+ self.default_options = {}
72
+
73
+ VALID_OPTIONS_KEYS.each do |key|
74
+ default_options[key] = "Vertebrae::Configuration::DEFAULT_#{key.to_s.upcase}".constantize
62
75
  end
63
76
  end
64
77
 
65
78
 
66
- def faraday_options(ops)
79
+ def faraday_options
67
80
  {
68
- :headers => {
69
- ACCEPT => "application/json;q=0.1",
70
- ACCEPT_CHARSET => "utf-8",
71
- USER_AGENT => user_agent,
72
- CONTENT_TYPE => content_type
73
- },
74
- :ssl => ops.fetch(:ssl) { ssl },
75
- :url => ops.fetch(:endpoint) { self.endpoint(ops) }
76
- }.merge(options)
81
+ :headers => {
82
+ ACCEPT => "application/json;q=0.1",
83
+ ACCEPT_CHARSET => "utf-8",
84
+ USER_AGENT => user_agent,
85
+ CONTENT_TYPE => content_type
86
+ },
87
+ :ssl => ssl,
88
+ :url => endpoint
89
+ }
77
90
  end
78
91
 
79
- def endpoint(ops)
80
- h = ops[:host] ? ops[:host] : self.host
81
- p = ops[:prefix] ? ops[:prefix] : self.prefix
82
92
 
83
- "https://#{h}#{p}"
93
+ # Extract login and password from basic_auth parameter
94
+ #
95
+ def process_basic_auth(auth)
96
+ case auth
97
+ when String
98
+ self.username, self.password = auth.split(':', 2)
99
+ when Hash
100
+ self.username = auth[:username]
101
+ self.password = auth[:password]
102
+ end
84
103
  end
85
104
 
86
- def options
87
- options = {}
88
- VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
89
- options
90
- end
91
105
 
92
- # Reset configuration options to their defaults
93
- #
94
- def reset!
95
- self.adapter = DEFAULT_ADAPTER
96
- self.prefix = DEFAULT_PREFIX
97
- self.ssl = DEFAULT_SSL
98
- self.mime_type = DEFAULT_MIME_TYPE
99
- self.user_agent = DEFAULT_USER_AGENT
100
- self.host = DEFAULT_HOST
101
- self.username = DEFAULT_USERNAME
102
- self.password = DEFAULT_PASSWORD
103
- self.connection_options = DEFAULT_CONNECTION_OPTIONS
104
- self.content_type = DEFAULT_CONTENT_TYPE
105
- self
106
+ def endpoint
107
+ "https://#{self.host}#{self.prefix}"
106
108
  end
107
- end # Configuration
109
+ end
108
110
  end
data/lib/connection.rb CHANGED
@@ -2,12 +2,15 @@ require 'faraday'
2
2
  require 'faraday_middleware'
3
3
  require 'request/basic_auth'
4
4
  require 'response/raise_error'
5
+ require 'authorization'
5
6
 
6
7
  module Vertebrae
7
- module Connection
8
-
9
- extend self
8
+ class Connection
10
9
  include Vertebrae::Constants
10
+ include Vertebrae::Authorization
11
+
12
+ attr_accessor :options
13
+ attr_accessor :configuration
11
14
 
12
15
  ALLOWED_OPTIONS = [
13
16
  :headers,
@@ -17,18 +20,21 @@ module Vertebrae
17
20
  :ssl
18
21
  ].freeze
19
22
 
20
- def default_options(options={})
21
- Vertebrae::Base.faraday_options(options)
23
+ def initialize(options)
24
+ @options = options
25
+ @configuration = Vertebrae::Configuration.new(options)
26
+ @connection = nil
27
+ @stack = nil
22
28
  end
23
29
 
24
30
  # Default middleware stack that uses default adapter as specified at
25
31
  # configuration stage.
26
32
  #
27
- def default_middleware(options={})
33
+ def default_middleware
28
34
  Proc.new do |builder|
29
35
  builder.use Faraday::Request::Multipart
30
36
  builder.use Faraday::Request::UrlEncoded
31
- builder.use Vertebrae::Request::BasicAuth, authentication if authenticated?
37
+ builder.use Vertebrae::Request::BasicAuth, configuration.authentication if configuration.authenticated?
32
38
 
33
39
  builder.use Faraday::Response::Logger if ENV['DEBUG']
34
40
  unless options[:raw]
@@ -36,45 +42,31 @@ module Vertebrae
36
42
  builder.use FaradayMiddleware::ParseJson
37
43
  end
38
44
  builder.use Vertebrae::Response::RaiseError
39
- builder.adapter adapter
45
+ builder.adapter configuration.adapter
40
46
  end
41
47
  end
42
48
 
43
- @connection = nil
44
-
45
- @stack = nil
46
-
47
- def clear_cache
48
- @connection = nil
49
- end
50
-
51
- def caching?
52
- !@connection.nil?
53
- end
54
-
55
49
  # Exposes middleware builder to facilitate custom stacks and easy
56
50
  # addition of new extensions such as cache adapter.
57
51
  #
58
- def stack(options={}, &block)
52
+ def stack(&block)
59
53
  @stack ||= begin
60
54
  if block_given?
61
55
  Faraday::Builder.new(&block)
62
56
  else
63
- Faraday::Builder.new(&default_middleware(options))
57
+ Faraday::Builder.new(&default_middleware)
64
58
  end
65
59
  end
66
60
  end
67
61
 
68
62
  # Returns a Fraday::Connection object
69
63
  #
70
- def connection(options={})
71
- conn_options = default_options(options)
72
- clear_cache unless options.empty?
73
- Vertebrae::Base.logger.debug "OPTIONS:#{conn_options.inspect}"
74
-
75
- @connection ||= Faraday.new(conn_options.merge(:builder => stack(options)))
64
+ def connection
65
+ if @connection
66
+ @connection
67
+ else
68
+ @connection ||= Faraday.new(configuration.faraday_options.merge(:builder => stack))
69
+ end
76
70
  end
77
-
78
-
79
71
  end
80
72
  end
data/lib/model.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Vertebrae
2
+ class Model
3
+ attr_accessor :client
4
+
5
+ def initialize(attrs = {})
6
+ attrs.each do |key,value|
7
+ if self.respond_to?("#{key}=")
8
+ self.send("#{key}=", value)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/vertebrae.rb CHANGED
@@ -1,13 +1,15 @@
1
+ require 'active_support/all'
2
+
1
3
  require 'constants'
4
+ require 'authorization'
2
5
  require 'configuration'
3
6
  require 'connection'
4
- require 'authorization'
5
7
  require 'request'
6
8
  require 'api'
7
9
  require 'base'
10
+ require 'model'
8
11
 
9
12
 
10
- require 'active_support/all'
11
13
  require 'railties' if defined? Rails
12
14
 
13
15
 
data/spec/api_spec.rb CHANGED
@@ -4,29 +4,8 @@ describe Vertebrae::API do
4
4
 
5
5
  subject { described_class.new(options) }
6
6
 
7
- it { described_class.included_modules.should include Vertebrae::Authorization }
8
- it { described_class.included_modules.should include Vertebrae::Connection }
9
7
  it { described_class.included_modules.should include Vertebrae::Request }
10
8
 
11
-
12
- describe '#extract_id_from_location' do
13
- let(:options) { {} }
14
- it "should extract the id" do
15
- subject.send(:extract_id_from_location, "https://roboticdogs.actionkit.com/rest/v1/importpage/1093/").should == "1093"
16
- end
17
- end
18
-
19
-
20
- context 'process_basic_auth' do
21
- let(:options) { { :basic_auth => 'login:password' } }
22
-
23
- its(:username) { should eq 'login' }
24
-
25
- its(:password) { should eq 'password' }
26
-
27
- its(:basic_auth) { should eq 'login:password' }
28
- end
29
-
30
9
  describe 'dummy' do
31
10
  describe 'should delegate to the client class' do
32
11
  specify{ Dummy.new.should respond_to(:api) }
@@ -1,41 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Vertebrae::Configuration do
4
- let(:klass) {
5
- ::Class.new do
6
- extend Vertebrae::Configuration
7
- end
8
- }
9
-
10
- subject { klass }
11
-
12
- its(:adapter) { should == described_class::DEFAULT_ADAPTER }
13
4
 
14
- its(:ssl) { should == described_class::DEFAULT_SSL }
5
+ subject { Vertebrae::Configuration.new({}) }
15
6
 
16
- its(:ssl) { should be_a Hash }
17
-
18
- its(:user_agent) { should == described_class::DEFAULT_USER_AGENT }
7
+ {:adapter => described_class::DEFAULT_ADAPTER,
8
+ :ssl => described_class::DEFAULT_SSL,
9
+ :user_agent => described_class::DEFAULT_USER_AGENT,
10
+ :username => described_class::DEFAULT_USERNAME,
11
+ :password => described_class::DEFAULT_PASSWORD }.each do | key, value |
12
+ its(key) { should == value }
13
+ its("default_#{key}") { should == value }
14
+ end
19
15
 
20
16
  its(:connection_options) { should be_a Hash }
21
-
22
17
  its(:connection_options) { should be_empty }
23
18
 
24
- its(:username) { should == described_class::DEFAULT_USERNAME }
19
+ describe "override" do
20
+ subject{ Vertebrae::Configuration.new({username: 'foo', password: 'bar'}) }
25
21
 
26
- its(:password) { should == described_class::DEFAULT_PASSWORD }
22
+ its(:default_username) { should == described_class::DEFAULT_USERNAME }
23
+ its(:default_password) { should == described_class::DEFAULT_PASSWORD }
27
24
 
28
- describe ".configure" do
29
- it { should respond_to :configure }
25
+ its(:username) { should == 'foo'}
26
+ its(:password) { should == 'bar'}
27
+ end
30
28
 
31
- described_class.keys.each do |key|
32
- it "should set the #{key}" do
33
- subject.configure do |config|
34
- config.send("#{key}=", key)
35
- subject.send(key).should == key
36
- end
37
- end
29
+ describe "setter" do
30
+ before(:each) do
31
+ subject.username = 'foo'
38
32
  end
33
+ its(:username) { should == 'foo'}
34
+ its(:default_username) { should == described_class::DEFAULT_USERNAME }
39
35
  end
40
36
 
37
+ describe 'endpoint' do
38
+ subject { Vertebrae::Configuration.new({host: 'test.com', prefix: ''}) }
39
+
40
+ specify { subject.host.should == 'test.com' }
41
+ specify { subject.endpoint.should == 'https://test.com'}
42
+ end
41
43
  end
data/vertebrae.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "vertebrae"
8
- s.version = "0.1.6"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Woodhull"]
12
- s.date = "2013-07-17"
12
+ s.date = "2013-07-30"
13
13
  s.description = "A set of low level infrastructure and reusable code for building API clients"
14
14
  s.email = "nathan@controlshiftlabs.com"
15
15
  s.extra_rdoc_files = [
@@ -34,13 +34,13 @@ Gem::Specification.new do |s|
34
34
  "lib/connection.rb",
35
35
  "lib/constants.rb",
36
36
  "lib/core_ext/array.rb",
37
+ "lib/model.rb",
37
38
  "lib/railties.rb",
38
39
  "lib/request.rb",
39
40
  "lib/request/basic_auth.rb",
40
41
  "lib/response/raise_error.rb",
41
42
  "lib/vertebrae.rb",
42
43
  "spec/api_spec.rb",
43
- "spec/authorization_spec.rb",
44
44
  "spec/configuration_spec.rb",
45
45
  "spec/dummy/client.rb",
46
46
  "spec/dummy/dummy.rb",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vertebrae
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Woodhull
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-17 00:00:00.000000000 Z
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -134,13 +134,13 @@ files:
134
134
  - lib/connection.rb
135
135
  - lib/constants.rb
136
136
  - lib/core_ext/array.rb
137
+ - lib/model.rb
137
138
  - lib/railties.rb
138
139
  - lib/request.rb
139
140
  - lib/request/basic_auth.rb
140
141
  - lib/response/raise_error.rb
141
142
  - lib/vertebrae.rb
142
143
  - spec/api_spec.rb
143
- - spec/authorization_spec.rb
144
144
  - spec/configuration_spec.rb
145
145
  - spec/dummy/client.rb
146
146
  - spec/dummy/dummy.rb
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Vertebrae::Authorization do
4
- let(:options) { {} }
5
-
6
- subject(:vb) { Dummy.new options }
7
-
8
- after do
9
- reset_authentication_for vb
10
- end
11
-
12
- context ".authenticated?" do
13
- it { should respond_to(:authenticated?) }
14
- end
15
-
16
- context "authentication" do
17
-
18
- context 'username & password' do
19
- let(:options) { { :username => 'vb', :password => 'pass' } }
20
-
21
- it "should return hash with username & password params" do
22
- vb.basic_auth.should == "#{options[:username]}:#{options[:password]}"
23
- end
24
- end
25
- end
26
- end # authentication