restulicious 0.1.4 → 0.1.5
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.
- data/lib/restulicious/adapter/default.rb +15 -15
- data/lib/restulicious/adapter/rest_api.rb +8 -3
- data/lib/restulicious/attributes.rb +1 -1
- data/lib/restulicious/collection.rb +43 -0
- data/lib/restulicious/coordinator.rb +23 -1
- data/lib/restulicious/parser/default.rb +17 -18
- data/lib/restulicious/query_methods.rb +12 -0
- data/lib/restulicious/version.rb +1 -1
- data/lib/restulicious.rb +1 -0
- data/test/support/dummy_class.rb +9 -0
- data/test/unit/collection_test.rb +28 -0
- data/test/unit/coordinator_test.rb +1 -1
- data/test/unit/query_methods_test.rb +0 -12
- metadata +5 -2
@@ -2,29 +2,21 @@ module Restulicious
|
|
2
2
|
module Adapter
|
3
3
|
class Default
|
4
4
|
|
5
|
-
def initialize(klazz, key)
|
6
|
-
@klazz
|
7
|
-
@key
|
5
|
+
def initialize(klazz, key, request_options, headers)
|
6
|
+
@klazz = klazz
|
7
|
+
@key = key
|
8
|
+
@request_options = request_options
|
9
|
+
@headers = headers
|
8
10
|
end
|
9
11
|
|
10
12
|
def get(url, params, &block)
|
11
|
-
request = ::Typhoeus::Request.new(url,
|
12
|
-
method: :get,
|
13
|
-
headers: { Accept: "application/json" },
|
14
|
-
timeout: 100000, # milliseconds
|
15
|
-
cache_timeout: 60, # seconds
|
16
|
-
params: params)
|
13
|
+
request = ::Typhoeus::Request.new(url, request_options.merge(method: :get, params: params))
|
17
14
|
hydra.queue(request)
|
18
15
|
handle_response(request, &block)
|
19
16
|
end
|
20
17
|
|
21
18
|
def post(url, params, &block)
|
22
|
-
request = ::Typhoeus::Request.new(url,
|
23
|
-
method: :post,
|
24
|
-
headers: { Accept: "application/json" },
|
25
|
-
timeout: 100000, # milliseconds
|
26
|
-
cache_timeout: 60, # seconds
|
27
|
-
body: params.to_json)
|
19
|
+
request = ::Typhoeus::Request.new(url, request_options.merge(method: :post, body: params.to_json))
|
28
20
|
hydra.queue(request)
|
29
21
|
handle_response(request, &block)
|
30
22
|
end
|
@@ -43,6 +35,14 @@ module Restulicious
|
|
43
35
|
|
44
36
|
private
|
45
37
|
|
38
|
+
def request_options
|
39
|
+
{
|
40
|
+
headers: { Accept: "application/json" }.merge(@headers),
|
41
|
+
timeout: 100000, # milliseconds
|
42
|
+
cache_timeout: 60 # seconds
|
43
|
+
}.merge(@request_options)
|
44
|
+
end
|
45
|
+
|
46
46
|
def parser(response)
|
47
47
|
Restulicious.config.parser_class.new(@klazz, @key, response.body)
|
48
48
|
end
|
@@ -2,7 +2,7 @@ module Restulicious
|
|
2
2
|
module Adapter
|
3
3
|
class RESTApi
|
4
4
|
|
5
|
-
def initialize(klazz, key)
|
5
|
+
def initialize(klazz, key, request_options, headers)
|
6
6
|
@klazz = klazz
|
7
7
|
@key = key
|
8
8
|
end
|
@@ -17,6 +17,11 @@ module Restulicious
|
|
17
17
|
handle_response(&block)
|
18
18
|
end
|
19
19
|
|
20
|
+
def put(url, params, &block)
|
21
|
+
@request = ::RESTApi.put(url, params)
|
22
|
+
handle_response(&block)
|
23
|
+
end
|
24
|
+
|
20
25
|
def on_success(&block)
|
21
26
|
@on_success = block
|
22
27
|
end
|
@@ -61,8 +66,8 @@ module Restulicious
|
|
61
66
|
end
|
62
67
|
end
|
63
68
|
if @on_failure
|
64
|
-
@request.on_failure do |response|
|
65
|
-
@on_failure.call(
|
69
|
+
@request.on_failure do |response, exception|
|
70
|
+
@on_failure.call(response, exception)
|
66
71
|
end
|
67
72
|
end
|
68
73
|
if block_given?
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Restulicious
|
2
|
+
class Collection
|
3
|
+
include Enumerable
|
4
|
+
include Attributes
|
5
|
+
|
6
|
+
attr_accessor :collection_key
|
7
|
+
|
8
|
+
def self.from_parser(klazz, collection_key, data)
|
9
|
+
setup_collection_of_items(klazz, collection_key, data)
|
10
|
+
self.attributes *data.keys
|
11
|
+
collection = new(data)
|
12
|
+
collection.collection_key = collection_key
|
13
|
+
collection
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(&block)
|
17
|
+
collection_accessor.each(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(sym, *args, &block)
|
21
|
+
if collection_accessor.respond_to?(sym)
|
22
|
+
collection_accessor.send(sym, *args, &block)
|
23
|
+
else
|
24
|
+
super(sym, *args, &block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def collection_accessor
|
31
|
+
send(collection_key)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.setup_collection_of_items(klazz, collection_key, attributes)
|
35
|
+
collection = []
|
36
|
+
attributes.delete(collection_key).each do |object_attributes|
|
37
|
+
collection << klazz.from_api(object_attributes.symbolize_keys!)
|
38
|
+
end
|
39
|
+
attributes[collection_key] = collection
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -1,15 +1,32 @@
|
|
1
1
|
module Restulicious
|
2
2
|
class Coordinator
|
3
3
|
|
4
|
+
attr_accessor :request_options, :headers
|
5
|
+
|
4
6
|
def initialize(klazz, api_options)
|
5
7
|
@klazz = klazz
|
6
8
|
api_options(api_options)
|
9
|
+
@request_options ||= {}
|
10
|
+
@headers ||= {}
|
7
11
|
end
|
8
12
|
|
9
13
|
def query_interface
|
10
14
|
@query_interface ||= Restulicious::QueryInterface.new(@url)
|
11
15
|
end
|
12
16
|
|
17
|
+
def basic_auth(username, password)
|
18
|
+
request_options[:username] = username
|
19
|
+
request_options[:password] = password
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def headers(header_hash)
|
24
|
+
header_hash.each do |key, value|
|
25
|
+
@headers[key] = value
|
26
|
+
end
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
13
30
|
def where(*args)
|
14
31
|
query_interface.where(*args)
|
15
32
|
self
|
@@ -47,6 +64,10 @@ module Restulicious
|
|
47
64
|
adapter.post(query_interface.first_url, query_interface.params, &block)
|
48
65
|
end
|
49
66
|
|
67
|
+
def update(&block)
|
68
|
+
adapter.put(query_interface.first_url, query_interface.params, &block)
|
69
|
+
end
|
70
|
+
|
50
71
|
def on_failure(&block)
|
51
72
|
adapter.on_failure(&block)
|
52
73
|
self
|
@@ -63,6 +84,7 @@ module Restulicious
|
|
63
84
|
end
|
64
85
|
|
65
86
|
def api_options(options)
|
87
|
+
option ||= {}
|
66
88
|
@url = options[:url]
|
67
89
|
@type = options[:type]
|
68
90
|
@key = options[:key]
|
@@ -71,7 +93,7 @@ module Restulicious
|
|
71
93
|
private
|
72
94
|
|
73
95
|
def adapter
|
74
|
-
@adapter ||= Restulicious.config.adapter_class.new(@klazz, key)
|
96
|
+
@adapter ||= Restulicious.config.adapter_class.new(@klazz, key, request_options, @headers)
|
75
97
|
end
|
76
98
|
|
77
99
|
def key
|
@@ -2,16 +2,23 @@ module Restulicious
|
|
2
2
|
module Parser
|
3
3
|
class Default
|
4
4
|
|
5
|
-
def initialize(klazz,
|
6
|
-
@klazz
|
7
|
-
@
|
8
|
-
@body
|
5
|
+
def initialize(klazz, collection_key, body)
|
6
|
+
@klazz = klazz
|
7
|
+
@collection_key = collection_key
|
8
|
+
@body = body
|
9
9
|
end
|
10
10
|
|
11
11
|
def result
|
12
12
|
if collection?
|
13
|
-
|
14
|
-
|
13
|
+
if @collection_key == "none"
|
14
|
+
collection = []
|
15
|
+
hashified_body.each do |object_attributes|
|
16
|
+
collection << @klazz.from_api(object_attributes.symbolize_keys!)
|
17
|
+
end
|
18
|
+
collection
|
19
|
+
else
|
20
|
+
Restulicious::Collection.from_parser(@klazz, @collection_key, hashified_body)
|
21
|
+
end
|
15
22
|
else
|
16
23
|
@klazz.from_api(hashified_body.symbolize_keys!)
|
17
24
|
end
|
@@ -24,19 +31,11 @@ module Restulicious
|
|
24
31
|
end
|
25
32
|
|
26
33
|
def collection?
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@struct ||= OpenStruct.new
|
32
|
-
end
|
33
|
-
|
34
|
-
def set_collection
|
35
|
-
collection = []
|
36
|
-
hashified_body.delete(@key).each do |object_attributes|
|
37
|
-
collection << @klazz.from_api(object_attributes.symbolize_keys!)
|
34
|
+
if @collection_key == "none"
|
35
|
+
hashified_body.is_a?(Array)
|
36
|
+
else
|
37
|
+
hashified_body[@collection_key].is_a?(Array)
|
38
38
|
end
|
39
|
-
hashified_body[@key] = collection
|
40
39
|
end
|
41
40
|
|
42
41
|
end
|
@@ -4,6 +4,14 @@ module Restulicious
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
|
7
|
+
def basic_auth(username, password)
|
8
|
+
coordinator.basic_auth(username, password)
|
9
|
+
end
|
10
|
+
|
11
|
+
def headers(header_hash)
|
12
|
+
coordinator.headers(header_hash)
|
13
|
+
end
|
14
|
+
|
7
15
|
def where(params)
|
8
16
|
coordinator.where(params)
|
9
17
|
end
|
@@ -36,6 +44,10 @@ module Restulicious
|
|
36
44
|
coordinator.create(&block)
|
37
45
|
end
|
38
46
|
|
47
|
+
def update(&bock)
|
48
|
+
coordinator.update(&block)
|
49
|
+
end
|
50
|
+
|
39
51
|
private
|
40
52
|
|
41
53
|
def api_options(options)
|
data/lib/restulicious/version.rb
CHANGED
data/lib/restulicious.rb
CHANGED
@@ -9,6 +9,7 @@ module Restulicious
|
|
9
9
|
autoload :QueryInterface, "restulicious/query_interface"
|
10
10
|
autoload :Config, "restulicious/config"
|
11
11
|
autoload :Coordinator, "restulicious/coordinator"
|
12
|
+
autoload :Collection, "restulicious/collection"
|
12
13
|
autoload :Adapter, "restulicious/adapter"
|
13
14
|
Adapter.autoload :Default, "restulicious/adapter/default"
|
14
15
|
Adapter.autoload :RESTApi, "restulicious/adapter/rest_api"
|
data/test/support/dummy_class.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path('../../support/test_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Restulicious::Collection do
|
4
|
+
before do
|
5
|
+
dummies = [
|
6
|
+
{ id: 1, name: "Einstein" },
|
7
|
+
{ id: 2, name: "Jobs" }
|
8
|
+
]
|
9
|
+
data = { "collection" => dummies, "total" => 2 }
|
10
|
+
@collection = Restulicious::Collection.from_parser(DummyClass, "collection", data)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "contains DummyClass objects" do
|
14
|
+
assert_kind_of DummyClass, @collection.first
|
15
|
+
end
|
16
|
+
|
17
|
+
it "each runs on the objects" do
|
18
|
+
@collection.each do |item|
|
19
|
+
assert_kind_of DummyClass, @collection.first
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "sets others keys as methods" do
|
24
|
+
assert_equal 2, @collection.total
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
@@ -4,7 +4,7 @@ describe Restulicious::Coordinator do
|
|
4
4
|
describe "Interface" do
|
5
5
|
|
6
6
|
before do
|
7
|
-
@coordinator = Restulicious::Coordinator.new(Class.new)
|
7
|
+
@coordinator = Restulicious::Coordinator.new(Class.new, {})
|
8
8
|
@coordinator.instance_variable_set(:@url, "www.example.com")
|
9
9
|
end
|
10
10
|
|
@@ -2,10 +2,6 @@ require File.expand_path('../../support/test_helper', __FILE__)
|
|
2
2
|
|
3
3
|
describe Restulicious::QueryMethods do
|
4
4
|
|
5
|
-
before do
|
6
|
-
DummyClass.send(:include, Restulicious::QueryMethods)
|
7
|
-
end
|
8
|
-
|
9
5
|
it "has where method, which sends to coordinator" do
|
10
6
|
Restulicious::Coordinator.any_instance.expects(:where).with(id: 5)
|
11
7
|
DummyClass.where(id: 5)
|
@@ -41,13 +37,5 @@ describe Restulicious::QueryMethods do
|
|
41
37
|
DummyClass.all
|
42
38
|
end
|
43
39
|
|
44
|
-
it "has api_options method, which sends to coordinator" do
|
45
|
-
options = {
|
46
|
-
url: "www.awesome.com/stuff"
|
47
|
-
}
|
48
|
-
Restulicious::Coordinator.any_instance.expects(:api_options).with(options)
|
49
|
-
DummyClass.send(:api_options, options)
|
50
|
-
end
|
51
|
-
|
52
40
|
end
|
53
41
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restulicious
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/restulicious/adapter/default.rb
|
127
127
|
- lib/restulicious/adapter/rest_api.rb
|
128
128
|
- lib/restulicious/attributes.rb
|
129
|
+
- lib/restulicious/collection.rb
|
129
130
|
- lib/restulicious/config.rb
|
130
131
|
- lib/restulicious/connection.rb
|
131
132
|
- lib/restulicious/coordinator.rb
|
@@ -138,6 +139,7 @@ files:
|
|
138
139
|
- test/support/dummy_class.rb
|
139
140
|
- test/support/test_helper.rb
|
140
141
|
- test/unit/attributes_test.rb
|
142
|
+
- test/unit/collection_test.rb
|
141
143
|
- test/unit/config_test.rb
|
142
144
|
- test/unit/connection/default_test.rb
|
143
145
|
- test/unit/coordinator_test.rb
|
@@ -172,6 +174,7 @@ test_files:
|
|
172
174
|
- test/support/dummy_class.rb
|
173
175
|
- test/support/test_helper.rb
|
174
176
|
- test/unit/attributes_test.rb
|
177
|
+
- test/unit/collection_test.rb
|
175
178
|
- test/unit/config_test.rb
|
176
179
|
- test/unit/connection/default_test.rb
|
177
180
|
- test/unit/coordinator_test.rb
|