restulicious 0.0.13 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ module Restulicious
2
+ module Adapter
3
+ class Default
4
+
5
+ def initialize(klazz, key)
6
+ @klazz = klazz
7
+ @key = key
8
+ end
9
+
10
+ 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)
17
+ hydra.queue(request)
18
+ handle_response(request, &block)
19
+ end
20
+
21
+ 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)
28
+ hydra.queue(request)
29
+ handle_response(request, &block)
30
+ end
31
+
32
+ def on_success(&block)
33
+ @on_success = block
34
+ end
35
+
36
+ def on_complete(&block)
37
+ @on_complete = block
38
+ end
39
+
40
+ def on_failure(&block)
41
+ @on_failure = block
42
+ end
43
+
44
+ private
45
+
46
+ def parser(response)
47
+ Restulicious.config.parser_class.new(@klazz, @key, response.body)
48
+ end
49
+
50
+ def hydra
51
+ Restulicious.config.hydra
52
+ end
53
+
54
+ def handle_response(request, &block)
55
+ if @on_success
56
+ request.on_success do |response|
57
+ @on_success.call(parser(response).result)
58
+ end
59
+ end
60
+ if @on_complete
61
+ request.on_complete do |response|
62
+ @on_complete.call(parser(response).result)
63
+ end
64
+ end
65
+ if @on_failure
66
+ request.on_failure do |response|
67
+ @on_failure.call(parser(response).result)
68
+ end
69
+ end
70
+ if block_given?
71
+ request.on_complete do |response|
72
+ block.call(parser(response).result)
73
+ end
74
+ elsif !@on_success && !@on_complete
75
+ hydra.run
76
+ parser(request.response).result
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+
@@ -0,0 +1,73 @@
1
+ module Restulicious
2
+ module Adapter
3
+ class RESTApi
4
+
5
+ def initialize(klazz, key)
6
+ @klazz = klazz
7
+ @key = key
8
+ end
9
+
10
+ def get(url, params, &block)
11
+ request = ::RESTApi.get(url, params)
12
+ handle_response(request, &block)
13
+ end
14
+
15
+ def post(url, params, &block)
16
+ request = ::RESTApi.post(url, params)
17
+ handle_response(request, &block)
18
+ end
19
+
20
+ def on_success(&block)
21
+ @on_success = block
22
+ end
23
+
24
+ def on_complete(&block)
25
+ @on_complete = block
26
+ end
27
+
28
+ def on_failure(&block)
29
+ @on_failure = block
30
+ end
31
+
32
+ private
33
+
34
+ def parser(response)
35
+ Restulicious.config.parser_class.new(@klazz, @key, response.body)
36
+ end
37
+
38
+ def hydra
39
+ Restulicious.config.hydra
40
+ end
41
+
42
+ def handle_response(request, &block)
43
+ if @on_success
44
+ request.on_success do |response|
45
+ @on_success.call(parser(response).result)
46
+ end
47
+ end
48
+ if @on_complete
49
+ request.on_complete do |response|
50
+ @on_complete.call(parser(response).result)
51
+ end
52
+ end
53
+ if @on_failure
54
+ request.on_failure do |response|
55
+ @on_failure.call(parser(response).result)
56
+ end
57
+ end
58
+ if block_given?
59
+ request.on_complete do |response|
60
+ block.call(parser(response).result)
61
+ end
62
+ elsif !@on_success && !@on_complete
63
+ hydra.run
64
+ parser(request.response).result
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+
73
+
@@ -0,0 +1,4 @@
1
+ module Restulicious
2
+ module Adapter
3
+ end
4
+ end
@@ -1,12 +1,12 @@
1
1
  module Restulicious
2
2
  class Config
3
3
 
4
- attr_accessor :hydra, :connection_class, :parser_class
4
+ attr_accessor :hydra, :adapter_class, :parser_class
5
5
 
6
6
  def initialize
7
7
  self.hydra = Typhoeus::Hydra.new
8
- self.connection_class = Connection
9
- self.parser_class = Parser
8
+ self.adapter_class = Adapter::Default
9
+ self.parser_class = Parser::Default
10
10
  end
11
11
 
12
12
  end
@@ -1,24 +1,5 @@
1
1
  module Restulicious
2
- class Connection
3
-
4
- def get(url, params)
5
- request = ::Typhoeus::Request.new(url,
6
- method: :get,
7
- headers: { Accept: "application/json" },
8
- timeout: 100000, # milliseconds
9
- cache_timeout: 60, # seconds
10
- params: params)
11
- end
12
-
13
- def post(url, params)
14
- request = ::Typhoeus::Request.new(url,
15
- method: :post,
16
- headers: { Accept: "application/json" },
17
- timeout: 100000, # milliseconds
18
- cache_timeout: 60, # seconds
19
- body: params.to_json)
20
- end
21
-
2
+ module Connection
22
3
  end
23
4
  end
24
5
 
@@ -3,7 +3,6 @@ module Restulicious
3
3
 
4
4
  def initialize(klazz)
5
5
  @klazz = klazz
6
- @after_complete_methods = []
7
6
  end
8
7
 
9
8
  def query_interface
@@ -35,30 +34,31 @@ module Restulicious
35
34
  self
36
35
  end
37
36
 
38
- def includes(*args)
39
- @after_complete_methods = args
40
- self
37
+ def first(&block)
38
+ adapter.get(query_interface.first_url, query_interface.params, &block)
39
+ end
40
+
41
+ def all(&block)
42
+ adapter.get(query_interface.first_url, query_interface.params, &block)
41
43
  end
42
44
 
43
- def first
44
- @request = connection.get(query_interface.first_url, query_interface.params)
45
- hydra.queue(@request)
46
- hydra.run
47
- parse.first
45
+ def create(&block)
46
+ adapter.post(query_interface.first_url, query_interface.params, &block)
48
47
  end
49
48
 
50
- def all
51
- @request = connection.get(query_interface.all_url, query_interface.params)
52
- hydra.queue(@request)
53
- hydra.run
54
- parse
49
+ def on_failure(&block)
50
+ adapter.on_failure(&block)
51
+ self
52
+ end
53
+
54
+ def on_success(&block)
55
+ adapter.on_success(&block)
56
+ self
55
57
  end
56
58
 
57
- def create
58
- @request = connection.post(query_interface.all_url, query_interface.params)
59
- hydra.queue(@request)
60
- hydra.run
61
- # parse
59
+ def on_complete(&block)
60
+ adapter.on_complete(&block)
61
+ self
62
62
  end
63
63
 
64
64
  def api_options(options)
@@ -69,24 +69,8 @@ module Restulicious
69
69
 
70
70
  private
71
71
 
72
- def connection
73
- Restulicious.config.connection_class.new
74
- end
75
-
76
- def parser
77
- Restulicious.config.parser_class.new(@klazz, key, @request.response.body)
78
- end
79
-
80
- def parse
81
- objects = parser.objects
82
- @after_complete_methods.each do |name|
83
- @klazz.send("after_api_complete_#{name}", objects)
84
- end
85
- objects
86
- end
87
-
88
- def hydra
89
- Restulicious.config.hydra
72
+ def adapter
73
+ @adapter ||= Restulicious.config.adapter_class.new(@klazz, key)
90
74
  end
91
75
 
92
76
  def key
@@ -0,0 +1,44 @@
1
+ module Restulicious
2
+ module Parser
3
+ class Default
4
+
5
+ def initialize(klazz, key, body)
6
+ @klazz = klazz
7
+ @key = key
8
+ @body = body
9
+ end
10
+
11
+ def result
12
+ if collection?
13
+ set_collection
14
+ hashified_body
15
+ else
16
+ @klazz.from_api(hashified_body.symbolize_keys!)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def hashified_body
23
+ @hashified_body ||= JSON.parse(@body)
24
+ end
25
+
26
+ def collection?
27
+ hashified_body[@key].is_a?(Array)
28
+ end
29
+
30
+ def struct
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!)
38
+ end
39
+ hashified_body[@key] = collection
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -1,33 +1,4 @@
1
1
  module Restulicious
2
- class Parser
3
-
4
- def initialize(klazz, key, body)
5
- @klazz = klazz
6
- @key = key
7
- @body = body
8
- end
9
-
10
- def objects
11
- objects = []
12
- if collection?
13
- hashified_body[@key].each do |object_attributes|
14
- objects << @klazz.from_api(object_attributes.symbolize_keys!)
15
- end
16
- else
17
- objects << @klazz.from_api(hashified_body.symbolize_keys!)
18
- end
19
- objects
20
- end
21
-
22
- private
23
-
24
- def hashified_body
25
- @hashified_body ||= JSON.parse(@body)
26
- end
27
-
28
- def collection?
29
- hashified_body[@key].is_a?(Array)
30
- end
31
-
2
+ module Parser
32
3
  end
33
4
  end
@@ -24,20 +24,16 @@ module Restulicious
24
24
  coordinator.select(fields)
25
25
  end
26
26
 
27
- def includes(*args)
28
- coordinator.includes(*args)
27
+ def first(&block)
28
+ coordinator.first(&block)
29
29
  end
30
30
 
31
- def first
32
- coordinator.first
31
+ def all(&block)
32
+ coordinator.all(&block)
33
33
  end
34
34
 
35
- def all
36
- coordinator.all
37
- end
38
-
39
- def create
40
- coordinator.create
35
+ def create(&block)
36
+ coordinator.create(&block)
41
37
  end
42
38
 
43
39
  private
@@ -1,3 +1,3 @@
1
1
  module Restulicious
2
- VERSION = "0.0.13"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/restulicious.rb CHANGED
@@ -4,13 +4,16 @@ require "restulicious/version"
4
4
  require 'active_support/all'
5
5
 
6
6
  module Restulicious
7
- autoload :Attributes, "restulicious/attributes"
8
- autoload :QueryMethods, "restulicious/query_methods"
9
- autoload :QueryInterface, "restulicious/query_interface"
10
- autoload :Config, "restulicious/config"
11
- autoload :Coordinator, "restulicious/coordinator"
12
- autoload :Connection, "restulicious/connection"
13
- autoload :Parser, "restulicious/parser"
7
+ autoload :Attributes, "restulicious/attributes"
8
+ autoload :QueryMethods, "restulicious/query_methods"
9
+ autoload :QueryInterface, "restulicious/query_interface"
10
+ autoload :Config, "restulicious/config"
11
+ autoload :Coordinator, "restulicious/coordinator"
12
+ autoload :Adapter, "restulicious/adapter"
13
+ Adapter.autoload :Default, "restulicious/adapter/default"
14
+ Adapter.autoload :RESTApi, "restulicious/adapter/rest_api"
15
+ autoload :Parser, "restulicious/parser"
16
+ Parser.autoload :Default, "restulicious/parser/default"
14
17
 
15
18
  def self.config
16
19
  @config ||= Config.new
@@ -4,54 +4,54 @@ describe Restulicious::Config do
4
4
  describe "with block" do
5
5
  it "has defaults" do
6
6
  Restulicious.config {}
7
- assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
8
- assert_equal Restulicious::Parser, Restulicious.config.parser_class
9
- assert_equal Restulicious::Connection, Restulicious.config.connection_class
7
+ assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
8
+ assert_equal Restulicious::Parser::Default, Restulicious.config.parser_class
9
+ assert_equal Restulicious::Adapter::Default, Restulicious.config.adapter_class
10
10
  end
11
11
 
12
12
  it "set options overrides defaults" do
13
13
  Restulicious.config do |config|
14
- config.connection_class = "boo"
15
- config.hydra = "yah"
16
- config.parser_class = "w00t"
14
+ config.adapter_class = "boo"
15
+ config.hydra = "yah"
16
+ config.parser_class = "w00t"
17
17
  end
18
- assert_equal "boo", Restulicious.config.connection_class
18
+ assert_equal "boo", Restulicious.config.adapter_class
19
19
  assert_equal "yah", Restulicious.config.hydra
20
20
  assert_equal "w00t", Restulicious.config.parser_class
21
21
  end
22
22
 
23
23
  it "set 1 option, other defaults remain" do
24
24
  Restulicious.config do |config|
25
- config.connection_class = "boo"
25
+ config.adapter_class = "boo"
26
26
  end
27
- assert_equal "boo", Restulicious.config.connection_class
28
- assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
29
- assert_equal Restulicious::Parser, Restulicious.config.parser_class
27
+ assert_equal "boo", Restulicious.config.adapter_class
28
+ assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
29
+ assert_equal Restulicious::Parser::Default, Restulicious.config.parser_class
30
30
  end
31
31
 
32
32
  end
33
33
 
34
34
  describe "without block" do
35
35
  it "has defaults" do
36
- assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
37
- assert_equal Restulicious::Parser, Restulicious.config.parser_class
38
- assert_equal Restulicious::Connection, Restulicious.config.connection_class
36
+ assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
37
+ assert_equal Restulicious::Parser::Default, Restulicious.config.parser_class
38
+ assert_equal Restulicious::Adapter::Default, Restulicious.config.adapter_class
39
39
  end
40
40
 
41
41
  it "set options overrides defaults" do
42
- Restulicious.config.connection_class = "boo"
43
- Restulicious.config.hydra = "yah"
44
- Restulicious.config.parser_class = "w00t"
45
- assert_equal "boo", Restulicious.config.connection_class
42
+ Restulicious.config.adapter_class = "boo"
43
+ Restulicious.config.hydra = "yah"
44
+ Restulicious.config.parser_class = "w00t"
45
+ assert_equal "boo", Restulicious.config.adapter_class
46
46
  assert_equal "yah", Restulicious.config.hydra
47
47
  assert_equal "w00t", Restulicious.config.parser_class
48
48
  end
49
49
 
50
50
  it "set 1 option, other defaults remain" do
51
- Restulicious.config.connection_class = "boo"
52
- assert_equal "boo", Restulicious.config.connection_class
53
- assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
54
- assert_equal Restulicious::Parser, Restulicious.config.parser_class
51
+ Restulicious.config.adapter_class = "boo"
52
+ assert_equal "boo", Restulicious.config.adapter_class
53
+ assert_kind_of Typhoeus::Hydra, Restulicious.config.hydra
54
+ assert_equal Restulicious::Parser::Default, Restulicious.config.parser_class
55
55
  end
56
56
 
57
57
  end
@@ -1,9 +1,9 @@
1
1
  require File.expand_path('../../support/test_helper', __FILE__)
2
2
 
3
- describe Restulicious::Connection do
3
+ describe Restulicious::Connection::Default do
4
4
 
5
5
  it "has a get method, that expects url and params" do
6
- Restulicious::Connection.new.get("http://www.example.com", { id: 123 })
6
+ Restulicious::Connection::Default.new.get("http://www.example.com", { id: 123 })
7
7
  end
8
8
 
9
9
  end
@@ -56,11 +56,6 @@ describe Restulicious::Coordinator do
56
56
  @coordinator.from("www.what.com").offset(10).limit(100).where(id: 42).select("id,name").all
57
57
  end
58
58
 
59
- it "includes stores args in after_complete_methods" do
60
- @coordinator.includes(:users, :comments)
61
- assert_equal [:users, :comments], @coordinator.instance_variable_get(:@after_complete_methods)
62
- end
63
-
64
59
  it "has first method"
65
60
  it "has all method"
66
61
  end
@@ -41,7 +41,6 @@ describe Restulicious::QueryMethods do
41
41
  DummyClass.all
42
42
  end
43
43
 
44
-
45
44
  it "has api_options method, which sends to coordinator" do
46
45
  options = {
47
46
  url: "www.awesome.com/stuff"
@@ -50,14 +49,5 @@ describe Restulicious::QueryMethods do
50
49
  DummyClass.send(:api_options, options)
51
50
  end
52
51
 
53
- it "has includes method, which sends to coordinator" do
54
- Restulicious::Coordinator.any_instance.expects(:includes).with(:user)
55
- DummyClass.includes(:user)
56
- end
57
-
58
- it "has includes method, that allows multiple arguments" do
59
- Restulicious::Coordinator.any_instance.expects(:includes).with(:user, :comments)
60
- DummyClass.includes(:user, :comments)
61
- end
62
52
  end
63
53
 
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.0.13
4
+ version: 0.1.1
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-10-12 00:00:00.000000000 Z
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -122,11 +122,15 @@ files:
122
122
  - README.md
123
123
  - Rakefile
124
124
  - lib/restulicious.rb
125
+ - lib/restulicious/adapter.rb
126
+ - lib/restulicious/adapter/default.rb
127
+ - lib/restulicious/adapter/rest_api.rb
125
128
  - lib/restulicious/attributes.rb
126
129
  - lib/restulicious/config.rb
127
130
  - lib/restulicious/connection.rb
128
131
  - lib/restulicious/coordinator.rb
129
132
  - lib/restulicious/parser.rb
133
+ - lib/restulicious/parser/default.rb
130
134
  - lib/restulicious/query_interface.rb
131
135
  - lib/restulicious/query_methods.rb
132
136
  - lib/restulicious/version.rb
@@ -135,7 +139,7 @@ files:
135
139
  - test/support/test_helper.rb
136
140
  - test/unit/attributes_test.rb
137
141
  - test/unit/config_test.rb
138
- - test/unit/connection_test.rb
142
+ - test/unit/connection/default_test.rb
139
143
  - test/unit/coordinator_test.rb
140
144
  - test/unit/query_interface_test.rb
141
145
  - test/unit/query_methods_test.rb
@@ -169,7 +173,7 @@ test_files:
169
173
  - test/support/test_helper.rb
170
174
  - test/unit/attributes_test.rb
171
175
  - test/unit/config_test.rb
172
- - test/unit/connection_test.rb
176
+ - test/unit/connection/default_test.rb
173
177
  - test/unit/coordinator_test.rb
174
178
  - test/unit/query_interface_test.rb
175
179
  - test/unit/query_methods_test.rb