apitizer 0.0.2 → 0.0.3
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 +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +4 -0
- data/Guardfile +1 -1
- data/README.md +0 -2
- data/apitizer.gemspec +1 -1
- data/lib/apitizer/base.rb +2 -2
- data/lib/apitizer/connection/adaptor/standard.rb +6 -5
- data/lib/apitizer/connection/dispatcher.rb +5 -4
- data/lib/apitizer/connection/request.rb +4 -4
- data/lib/apitizer/connection/response.rb +3 -3
- data/lib/apitizer/helper.rb +4 -0
- data/lib/apitizer/result.rb +4 -4
- data/lib/apitizer/routing/map.rb +6 -4
- data/lib/apitizer/routing/node/base.rb +1 -1
- data/lib/apitizer/routing/node/collection.rb +6 -3
- data/lib/apitizer/routing/node/operation.rb +5 -5
- data/lib/apitizer/routing/path.rb +6 -6
- data/lib/apitizer/routing/proxy.rb +4 -4
- data/lib/apitizer/version.rb +1 -1
- data/spec/apitizer/connection/adaptor_spec.rb +6 -6
- data/spec/apitizer/routing/node_spec.rb +4 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2b8ff93fa7f78efed6e935c176d6f9ff3323faf
|
4
|
+
data.tar.gz: 5da515bb449d54753870d85872aa91dfddbad6d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b25f42624fd95badd0f04af17494d3b763e1f280ab4c217663449497a3d29f9df40e4ad90124d97deea389037e0dcbad37238fde4d7ccf71027132ca46fcd0ae
|
7
|
+
data.tar.gz: 5bcdceb62d92026e806511f501ddb399f52ecdd1f5f5caebac99798d3d41738162e41e20cda7e773cefaf20c365fdba8919e5f956e70aeeaaae3f983f79f414d
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Guardfile
CHANGED
data/README.md
CHANGED
data/apitizer.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^spec/})
|
19
19
|
spec.require_paths = [ 'lib' ]
|
20
20
|
|
21
|
-
spec.required_ruby_version = '>=
|
21
|
+
spec.required_ruby_version = '>= 1.9.3'
|
22
22
|
|
23
23
|
spec.add_dependency 'rack', '~> 1.5'
|
24
24
|
spec.add_dependency 'json', '~> 1.8'
|
data/lib/apitizer/base.rb
CHANGED
@@ -4,7 +4,7 @@ module Apitizer
|
|
4
4
|
|
5
5
|
def_delegator :map, :define
|
6
6
|
|
7
|
-
def initialize(
|
7
|
+
def initialize(options = {}, &block)
|
8
8
|
@options = Helper.deep_merge(Apitizer.defaults, options)
|
9
9
|
@block = block
|
10
10
|
end
|
@@ -41,7 +41,7 @@ module Apitizer
|
|
41
41
|
def prepare(action, *path)
|
42
42
|
action = action.to_sym
|
43
43
|
method = @options[:dictionary][action] or raise Error, 'Unknown action'
|
44
|
-
parameters =
|
44
|
+
parameters = Helper.extract_hash!(path)
|
45
45
|
steps = path.flatten.map(&:to_sym)
|
46
46
|
|
47
47
|
[ action, method, steps, parameters ]
|
@@ -6,10 +6,12 @@ module Apitizer
|
|
6
6
|
module Adaptor
|
7
7
|
class Standard
|
8
8
|
def process(method, address, parameters = {}, headers = {})
|
9
|
+
uri = URI(address)
|
10
|
+
|
9
11
|
request = build_request(method, address, parameters)
|
10
12
|
headers.each { |k, v| request[k] = v }
|
11
13
|
|
12
|
-
http = Net::HTTP.new(
|
14
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
13
15
|
http.use_ssl = true if address =~ /^https:/
|
14
16
|
|
15
17
|
response = http.request(request)
|
@@ -27,15 +29,14 @@ module Apitizer
|
|
27
29
|
def build_request(method, address, parameters)
|
28
30
|
klass = Net::HTTP.const_get(method.to_s.capitalize)
|
29
31
|
|
30
|
-
return klass.new(
|
32
|
+
return klass.new(address) if parameters.empty?
|
31
33
|
|
32
34
|
parameters = Helper.build_query(parameters)
|
33
35
|
|
34
36
|
if klass == Net::HTTP::Get
|
35
|
-
|
36
|
-
request = klass.new(URI(address))
|
37
|
+
request = klass.new([ address, parameters ].join('?'))
|
37
38
|
else
|
38
|
-
request = klass.new(
|
39
|
+
request = klass.new(address)
|
39
40
|
request.body = parameters
|
40
41
|
request['Content-Type'] = [
|
41
42
|
'application/x-www-form-urlencoded',
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module Apitizer
|
2
2
|
module Connection
|
3
3
|
class Dispatcher
|
4
|
-
def initialize(
|
5
|
-
@format = Format.build(format)
|
6
|
-
@adaptor = Adaptor.build(adaptor)
|
7
|
-
@headers = headers
|
4
|
+
def initialize(options)
|
5
|
+
@format = Format.build(options.fetch(:format))
|
6
|
+
@adaptor = Adaptor.build(options[:adaptor] || :standard)
|
7
|
+
@headers = options[:headers] || {}
|
8
|
+
@headers.merge!('Accept' => @format.mime_type)
|
8
9
|
end
|
9
10
|
|
10
11
|
def process(request)
|
@@ -6,10 +6,10 @@ module Apitizer
|
|
6
6
|
attr_reader :method, :path, :parameters
|
7
7
|
def_delegator :path, :address
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
@method = method
|
11
|
-
@path = path
|
12
|
-
@parameters = parameters
|
9
|
+
def initialize(options)
|
10
|
+
@method = options.fetch(:method)
|
11
|
+
@path = options.fetch(:path)
|
12
|
+
@parameters = options[:parameters] || {}
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/apitizer/helper.rb
CHANGED
@@ -25,6 +25,10 @@ module Apitizer
|
|
25
25
|
one.merge(two, &merger)
|
26
26
|
end
|
27
27
|
|
28
|
+
def self.extract_hash!(arguments)
|
29
|
+
arguments.last.is_a?(Hash) ? arguments.pop : {}
|
30
|
+
end
|
31
|
+
|
28
32
|
def self.build_query(parameters)
|
29
33
|
query = Rack::Utils.build_nested_query(prepare_parameters(parameters))
|
30
34
|
query.encode!('UTF-8')
|
data/lib/apitizer/result.rb
CHANGED
@@ -6,10 +6,10 @@ module Apitizer
|
|
6
6
|
def_delegator :@response, :code
|
7
7
|
def_delegators :__getobj__, :is_a?, :kind_of?, :instance_of?
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
|
11
|
-
@
|
12
|
-
@response
|
9
|
+
def initialize(options)
|
10
|
+
@request = options.fetch(:request)
|
11
|
+
@response = options.fetch(:response)
|
12
|
+
super(@response.content)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/apitizer/routing/map.rb
CHANGED
@@ -21,8 +21,9 @@ module Apitizer
|
|
21
21
|
proxy.instance_eval(&block)
|
22
22
|
end
|
23
23
|
|
24
|
-
def define_resources(name,
|
25
|
-
|
24
|
+
def define_resources(name, options, &block)
|
25
|
+
parent = options.delete(:parent) || @root
|
26
|
+
child = Node::Collection.new(name, options)
|
26
27
|
parent.append(child)
|
27
28
|
return unless block_given?
|
28
29
|
proxy = Proxy.new(self, parent: child)
|
@@ -30,8 +31,9 @@ module Apitizer
|
|
30
31
|
end
|
31
32
|
|
32
33
|
Apitizer.actions.each do |action|
|
33
|
-
define_method "define_#{ action }" do |name,
|
34
|
-
|
34
|
+
define_method "define_#{ action }" do |name, options|
|
35
|
+
parent = options.delete(:parent)
|
36
|
+
child = Node::Operation.new(name, options.merge(action: action))
|
35
37
|
parent.append(child)
|
36
38
|
end
|
37
39
|
end
|
@@ -2,8 +2,10 @@ module Apitizer
|
|
2
2
|
module Routing
|
3
3
|
module Node
|
4
4
|
class Collection < Base
|
5
|
-
def initialize(name,
|
5
|
+
def initialize(name, options = {})
|
6
6
|
@name = name
|
7
|
+
only = options[:only]
|
8
|
+
except = options[:except] || []
|
7
9
|
@actions = (only && Array(only) || Apitizer.actions) - Array(except)
|
8
10
|
end
|
9
11
|
|
@@ -11,8 +13,9 @@ module Apitizer
|
|
11
13
|
@name == steps.first
|
12
14
|
end
|
13
15
|
|
14
|
-
def permit?(action,
|
15
|
-
@actions.include?(action) &&
|
16
|
+
def permit?(action, options)
|
17
|
+
@actions.include?(action) &&
|
18
|
+
Helper.action_scope(action) == options.fetch(:on)
|
16
19
|
end
|
17
20
|
|
18
21
|
private
|
@@ -2,18 +2,18 @@ module Apitizer
|
|
2
2
|
module Routing
|
3
3
|
module Node
|
4
4
|
class Operation < Base
|
5
|
-
def initialize(name,
|
5
|
+
def initialize(name, options = {})
|
6
6
|
@name = name
|
7
|
-
@action = action
|
8
|
-
@on = on
|
7
|
+
@action = options.fetch(:action)
|
8
|
+
@on = options.fetch(:on)
|
9
9
|
end
|
10
10
|
|
11
11
|
def recognize?(steps)
|
12
12
|
@name == steps.first || @name.to_s =~ /^:/
|
13
13
|
end
|
14
14
|
|
15
|
-
def permit?(action,
|
16
|
-
@action == action && @on == on
|
15
|
+
def permit?(action, options)
|
16
|
+
@action == action && @on == options.fetch(:on)
|
17
17
|
end
|
18
18
|
|
19
19
|
def on?(on)
|
@@ -3,19 +3,19 @@ module Apitizer
|
|
3
3
|
class Path
|
4
4
|
attr_reader :steps, :node
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@steps = steps
|
8
|
-
@node = node
|
6
|
+
def initialize(options = {})
|
7
|
+
@steps = options[:steps] || []
|
8
|
+
@node = options[:node]
|
9
9
|
end
|
10
10
|
|
11
11
|
def address
|
12
12
|
@steps.map(&:to_s).join('/')
|
13
13
|
end
|
14
14
|
|
15
|
-
def advance(step,
|
15
|
+
def advance(step, options)
|
16
16
|
@steps << step
|
17
|
-
@node = node
|
18
|
-
@on = on
|
17
|
+
@node = options.fetch(:node)
|
18
|
+
@on = options[:on]
|
19
19
|
end
|
20
20
|
|
21
21
|
def permit?(action)
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module Apitizer
|
2
2
|
module Routing
|
3
3
|
class Proxy
|
4
|
-
def initialize(owner,
|
4
|
+
def initialize(owner, options = {})
|
5
5
|
@owner = owner
|
6
6
|
@options = options
|
7
7
|
end
|
8
8
|
|
9
|
-
def method_missing(name, *arguments,
|
9
|
+
def method_missing(name, *arguments, &block)
|
10
10
|
name = :"define_#{ name }"
|
11
11
|
return super unless @owner.respond_to?(name)
|
12
|
-
|
13
|
-
@owner.send(name, *arguments,
|
12
|
+
options = Helper.extract_hash!(arguments)
|
13
|
+
@owner.send(name, *arguments, options.merge(@options), &block)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
data/lib/apitizer/version.rb
CHANGED
@@ -4,7 +4,7 @@ RSpec.describe Apitizer::Connection::Adaptor do
|
|
4
4
|
let(:parent_module) { Apitizer::Connection }
|
5
5
|
let(:address) { 'https://service.com/api/articles' }
|
6
6
|
|
7
|
-
shared_examples 'a proper postman' do |method
|
7
|
+
shared_examples 'a proper postman' do |method|
|
8
8
|
it 'takes into account headers' do
|
9
9
|
headers = { 'Secret-Token' => 'arbitrary' }
|
10
10
|
stub = stub_http_request(method, address).with(headers: headers)
|
@@ -13,7 +13,7 @@ RSpec.describe Apitizer::Connection::Adaptor do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
shared_examples '#call of a Rack app' do |method
|
16
|
+
shared_examples '#call of a Rack app' do |method|
|
17
17
|
let(:code) { 200 }
|
18
18
|
let(:headers) { { 'a' => [ 'b' ] } }
|
19
19
|
let(:body) { 'Hej!' }
|
@@ -60,8 +60,8 @@ RSpec.describe Apitizer::Connection::Adaptor do
|
|
60
60
|
describe '#process' do
|
61
61
|
[ :get ].each do |method|
|
62
62
|
context "when sending #{ method } requests" do
|
63
|
-
it_behaves_like '#call of a Rack app', method
|
64
|
-
it_behaves_like 'a proper postman', method
|
63
|
+
it_behaves_like '#call of a Rack app', method
|
64
|
+
it_behaves_like 'a proper postman', method
|
65
65
|
|
66
66
|
it 'encodes parameters into the URI' do
|
67
67
|
stub = stub_http_request(method, address).with(query: { life: 42 })
|
@@ -73,8 +73,8 @@ RSpec.describe Apitizer::Connection::Adaptor do
|
|
73
73
|
|
74
74
|
[ :post, :put, :patch, :delete ].each do |method|
|
75
75
|
context "when sending #{ method } requests" do
|
76
|
-
it_behaves_like '#call of a Rack app', method
|
77
|
-
it_behaves_like 'a proper postman', method
|
76
|
+
it_behaves_like '#call of a Rack app', method
|
77
|
+
it_behaves_like 'a proper postman', method
|
78
78
|
|
79
79
|
it 'encodes parameters into the body' do
|
80
80
|
stub = stub_http_request(method, address).with(body: 'life=42')
|
@@ -10,7 +10,7 @@ RSpec.describe Apitizer::Routing::Node do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
shared_examples 'an adequate collection guard' do |only
|
13
|
+
shared_examples 'an adequate collection guard' do |only = restful_actions|
|
14
14
|
(restful_collection_actions & only).each do |action|
|
15
15
|
it "permits #{ action } actions" do
|
16
16
|
path = root.trace(steps)
|
@@ -33,7 +33,7 @@ RSpec.describe Apitizer::Routing::Node do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
shared_examples 'an adequate member guard' do |only
|
36
|
+
shared_examples 'an adequate member guard' do |only = restful_actions|
|
37
37
|
(restful_member_actions & only).each do |action|
|
38
38
|
it "permites #{ action } actions" do
|
39
39
|
path = root.trace(steps)
|
@@ -96,13 +96,13 @@ RSpec.describe Apitizer::Routing::Node do
|
|
96
96
|
context 'when looking for collections' do
|
97
97
|
let(:steps) { [ :articles ] }
|
98
98
|
it_behaves_like 'an adequate pathfinder'
|
99
|
-
it_behaves_like 'an adequate collection guard', only
|
99
|
+
it_behaves_like 'an adequate collection guard', only
|
100
100
|
end
|
101
101
|
|
102
102
|
context 'when looking for members' do
|
103
103
|
let(:steps) { [ :articles, 'xxx' ] }
|
104
104
|
it_behaves_like 'an adequate pathfinder'
|
105
|
-
it_behaves_like 'an adequate member guard', only
|
105
|
+
it_behaves_like 'an adequate member guard', only
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Ukhov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -175,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
175
|
requirements:
|
176
176
|
- - ">="
|
177
177
|
- !ruby/object:Gem::Version
|
178
|
-
version:
|
178
|
+
version: 1.9.3
|
179
179
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
180
|
requirements:
|
181
181
|
- - ">="
|