her 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +7 -0
- data/README.md +111 -9
- data/examples/twitter-oauth/Gemfile +13 -0
- data/examples/twitter-oauth/app.rb +42 -0
- data/examples/twitter-oauth/config.ru +5 -0
- data/examples/twitter-oauth/views/index.haml +9 -0
- data/examples/twitter-search/Gemfile +11 -0
- data/examples/twitter-search/app.rb +30 -0
- data/examples/twitter-search/config.ru +5 -0
- data/examples/twitter-search/views/index.haml +9 -0
- data/her.gemspec +14 -10
- data/lib/her.rb +1 -0
- data/lib/her/api.rb +27 -7
- data/lib/her/errors.rb +5 -0
- data/lib/her/middleware.rb +2 -0
- data/lib/her/model.rb +5 -2
- data/lib/her/model/http.rb +20 -44
- data/lib/her/model/introspection.rb +1 -3
- data/lib/her/model/orm.rb +8 -9
- data/lib/her/model/paths.rb +59 -0
- data/lib/her/model/relationships.rb +3 -6
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +15 -45
- data/spec/model/hooks_spec.rb +1 -2
- data/spec/model/introspection_spec.rb +2 -2
- data/spec/model/orm_spec.rb +5 -4
- data/spec/model/paths_spec.rb +131 -0
- data/spec/model/relationships_spec.rb +6 -4
- data/spec/spec_helper.rb +3 -2
- metadata +115 -38
data/lib/her/middleware.rb
CHANGED
data/lib/her/model.rb
CHANGED
@@ -16,12 +16,14 @@ module Her
|
|
16
16
|
autoload :Relationships, "her/model/relationships"
|
17
17
|
autoload :Hooks, "her/model/hooks"
|
18
18
|
autoload :Introspection, "her/model/introspection"
|
19
|
+
autoload :Paths, "her/model/paths"
|
19
20
|
|
20
21
|
extend ActiveSupport::Concern
|
21
22
|
|
22
23
|
# Instance methods
|
23
24
|
include Her::Model::ORM
|
24
25
|
include Her::Model::Introspection
|
26
|
+
include Her::Model::Paths
|
25
27
|
|
26
28
|
# Class methods
|
27
29
|
included do
|
@@ -30,10 +32,11 @@ module Her
|
|
30
32
|
extend Her::Model::ORM
|
31
33
|
extend Her::Model::Relationships
|
32
34
|
extend Her::Model::Hooks
|
35
|
+
extend Her::Model::Paths
|
33
36
|
|
34
37
|
# Define default settings
|
35
|
-
collection_path "
|
36
|
-
|
38
|
+
collection_path "/#{self.to_s.downcase.pluralize}"
|
39
|
+
resource_path "/#{self.to_s.downcase.pluralize}/:id"
|
37
40
|
uses_api Her::API.default_api
|
38
41
|
end
|
39
42
|
end
|
data/lib/her/model/http.rb
CHANGED
@@ -7,30 +7,6 @@ module Her
|
|
7
7
|
@her_api = api
|
8
8
|
end # }}}
|
9
9
|
|
10
|
-
# Defines a custom collection path for the resource
|
11
|
-
#
|
12
|
-
# @example
|
13
|
-
# class User
|
14
|
-
# include Her::Model
|
15
|
-
# collection_path "users"
|
16
|
-
# end
|
17
|
-
def collection_path(path=nil) # {{{
|
18
|
-
return @her_collection_path unless path
|
19
|
-
@her_collection_path = path
|
20
|
-
end # }}}
|
21
|
-
|
22
|
-
# Defines a custom item path for the resource
|
23
|
-
#
|
24
|
-
# @example
|
25
|
-
# class User
|
26
|
-
# include Her::Model
|
27
|
-
# item_path "user"
|
28
|
-
# end
|
29
|
-
def item_path(path=nil) # {{{
|
30
|
-
return @her_item_path unless path
|
31
|
-
@her_item_path = path
|
32
|
-
end # }}}
|
33
|
-
|
34
10
|
# Main request wrapper around Her::API. Used to make custom request to the API.
|
35
11
|
# @private
|
36
12
|
def request(attrs={}, &block) # {{{
|
@@ -47,7 +23,7 @@ module Her
|
|
47
23
|
# @popular_users = User.get(:popular)
|
48
24
|
# # Fetched via GET "/users/popular"
|
49
25
|
def get(path, attrs={}) # {{{
|
50
|
-
path = "#{
|
26
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
51
27
|
get_raw(path, attrs) do |parsed_data|
|
52
28
|
if parsed_data[:data].is_a?(Array)
|
53
29
|
new_collection(parsed_data)
|
@@ -59,13 +35,13 @@ module Her
|
|
59
35
|
|
60
36
|
# Make a GET request and return the parsed JSON response (not mapped to objects)
|
61
37
|
def get_raw(path, attrs={}, &block) # {{{
|
62
|
-
path = "#{
|
38
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
63
39
|
request(attrs.merge(:_method => :get, :_path => path), &block)
|
64
40
|
end # }}}
|
65
41
|
|
66
42
|
# Make a GET request and return a collection of resources
|
67
43
|
def get_collection(path, attrs={}) # {{{
|
68
|
-
path = "#{
|
44
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
69
45
|
get_raw(path, attrs) do |parsed_data|
|
70
46
|
new_collection(parsed_data)
|
71
47
|
end
|
@@ -73,7 +49,7 @@ module Her
|
|
73
49
|
|
74
50
|
# Make a GET request and return a collection of resources
|
75
51
|
def get_resource(path, attrs={}) # {{{
|
76
|
-
path = "#{
|
52
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
77
53
|
get_raw(path, attrs) do |parsed_data|
|
78
54
|
new(parsed_data[:data])
|
79
55
|
end
|
@@ -81,7 +57,7 @@ module Her
|
|
81
57
|
|
82
58
|
# Make a POST request and return either a collection or a resource
|
83
59
|
def post(path, attrs={}) # {{{
|
84
|
-
path = "#{
|
60
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
85
61
|
post_raw(path, attrs) do |parsed_data|
|
86
62
|
if parsed_data[:data].is_a?(Array)
|
87
63
|
new_collection(parsed_data)
|
@@ -93,13 +69,13 @@ module Her
|
|
93
69
|
|
94
70
|
# Make a POST request and return the parsed JSON response (not mapped to objects)
|
95
71
|
def post_raw(path, attrs={}, &block) # {{{
|
96
|
-
path = "#{
|
72
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
97
73
|
request(attrs.merge(:_method => :post, :_path => path), &block)
|
98
74
|
end # }}}
|
99
75
|
|
100
76
|
# Make a POST request and return a collection of resources
|
101
77
|
def post_collection(path, attrs={}) # {{{
|
102
|
-
path = "#{
|
78
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
103
79
|
post_raw(path, attrs) do |parsed_data|
|
104
80
|
new_collection(parsed_data)
|
105
81
|
end
|
@@ -107,7 +83,7 @@ module Her
|
|
107
83
|
|
108
84
|
# Make a POST request and return a collection of resources
|
109
85
|
def post_resource(path, attrs={}) # {{{
|
110
|
-
path = "#{
|
86
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
111
87
|
post_raw(path, attrs) do |parsed_data|
|
112
88
|
new(parsed_data[:data])
|
113
89
|
end
|
@@ -115,7 +91,7 @@ module Her
|
|
115
91
|
|
116
92
|
# Make a PUT request and return either a collection or a resource
|
117
93
|
def put(path, attrs={}) # {{{
|
118
|
-
path = "#{
|
94
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
119
95
|
put_raw(path, attrs) do |parsed_data|
|
120
96
|
if parsed_data[:data].is_a?(Array)
|
121
97
|
new_collection(parsed_data)
|
@@ -127,13 +103,13 @@ module Her
|
|
127
103
|
|
128
104
|
# Make a PUT request and return the parsed JSON response (not mapped to objects)
|
129
105
|
def put_raw(path, attrs={}, &block) # {{{
|
130
|
-
path = "#{
|
106
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
131
107
|
request(attrs.merge(:_method => :put, :_path => path), &block)
|
132
108
|
end # }}}
|
133
109
|
|
134
110
|
# Make a PUT request and return a collection of resources
|
135
111
|
def put_collection(path, attrs={}) # {{{
|
136
|
-
path = "#{
|
112
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
137
113
|
put_raw(path, attrs) do |parsed_data|
|
138
114
|
new_collection(parsed_data)
|
139
115
|
end
|
@@ -141,7 +117,7 @@ module Her
|
|
141
117
|
|
142
118
|
# Make a PUT request and return a collection of resources
|
143
119
|
def put_resource(path, attrs={}) # {{{
|
144
|
-
path = "#{
|
120
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
145
121
|
put_raw(path, attrs) do |parsed_data|
|
146
122
|
new(parsed_data[:data])
|
147
123
|
end
|
@@ -149,7 +125,7 @@ module Her
|
|
149
125
|
|
150
126
|
# Make a PATCH request and return either a collection or a resource
|
151
127
|
def patch(path, attrs={}) # {{{
|
152
|
-
path = "#{
|
128
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
153
129
|
patch_raw(path, attrs) do |parsed_data|
|
154
130
|
if parsed_data[:data].is_a?(Array)
|
155
131
|
new_collection(parsed_data)
|
@@ -161,13 +137,13 @@ module Her
|
|
161
137
|
|
162
138
|
# Make a PATCH request and return the parsed JSON response (not mapped to objects)
|
163
139
|
def patch_raw(path, attrs={}, &block) # {{{
|
164
|
-
path = "#{
|
140
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
165
141
|
request(attrs.merge(:_method => :patch, :_path => path), &block)
|
166
142
|
end # }}}
|
167
143
|
|
168
144
|
# Make a PATCH request and return a collection of resources
|
169
145
|
def patch_collection(path, attrs={}) # {{{
|
170
|
-
path = "#{
|
146
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
171
147
|
patch_raw(path, attrs) do |parsed_data|
|
172
148
|
new_collection(parsed_data)
|
173
149
|
end
|
@@ -175,7 +151,7 @@ module Her
|
|
175
151
|
|
176
152
|
# Make a PATCH request and return a collection of resources
|
177
153
|
def patch_resource(path, attrs={}) # {{{
|
178
|
-
path = "#{
|
154
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
179
155
|
patch_raw(path, attrs) do |parsed_data|
|
180
156
|
new(parsed_data[:data])
|
181
157
|
end
|
@@ -183,7 +159,7 @@ module Her
|
|
183
159
|
|
184
160
|
# Make a DELETE request and return either a collection or a resource
|
185
161
|
def delete(path, attrs={}) # {{{
|
186
|
-
path = "#{
|
162
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
187
163
|
delete_raw(path, attrs) do |parsed_data|
|
188
164
|
if parsed_data[:data].is_a?(Array)
|
189
165
|
new_collection(parsed_data)
|
@@ -195,13 +171,13 @@ module Her
|
|
195
171
|
|
196
172
|
# Make a DELETE request and return the parsed JSON response (not mapped to objects)
|
197
173
|
def delete_raw(path, attrs={}, &block) # {{{
|
198
|
-
path = "#{
|
174
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
199
175
|
request(attrs.merge(:_method => :delete, :_path => path), &block)
|
200
176
|
end # }}}
|
201
177
|
|
202
178
|
# Make a DELETE request and return a collection of resources
|
203
179
|
def delete_collection(path, attrs={}) # {{{
|
204
|
-
path = "#{
|
180
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
205
181
|
delete_raw(path, attrs) do |parsed_data|
|
206
182
|
new_collection(parsed_data)
|
207
183
|
end
|
@@ -209,7 +185,7 @@ module Her
|
|
209
185
|
|
210
186
|
# Make a DELETE request and return a collection of resources
|
211
187
|
def delete_resource(path, attrs={}) # {{{
|
212
|
-
path = "#{
|
188
|
+
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
|
213
189
|
delete_raw(path, attrs) do |parsed_data|
|
214
190
|
new(parsed_data[:data])
|
215
191
|
end
|
@@ -11,9 +11,7 @@ module Her
|
|
11
11
|
# @user = User.find(1)
|
12
12
|
# p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
|
13
13
|
def inspect # {{{
|
14
|
-
|
15
|
-
resource_path << "/#{id}" if @data.include?(:id)
|
16
|
-
"#<#{self.class}(#{resource_path}) #{@data.inject([]) { |memo, item| key, value = item; memo << "#{key}=#{attribute_for_inspect(value)}"}.join(" ")}>"
|
14
|
+
"#<#{self.class}(#{self.class.build_request_path(@data)}) #{@data.inject([]) { |memo, item| key, value = item; memo << "#{key}=#{attribute_for_inspect(value)}"}.join(" ")}>"
|
17
15
|
end # }}}
|
18
16
|
|
19
17
|
private
|
data/lib/her/model/orm.rb
CHANGED
@@ -50,7 +50,7 @@ module Her
|
|
50
50
|
# @user = User.find(1)
|
51
51
|
# # Fetched via GET "/users/1"
|
52
52
|
def find(id, params={}) # {{{
|
53
|
-
request(params.merge(:_method => :get, :_path => "#{
|
53
|
+
request(params.merge(:_method => :get, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
|
54
54
|
new(parsed_data[:data])
|
55
55
|
end
|
56
56
|
end # }}}
|
@@ -61,7 +61,7 @@ module Her
|
|
61
61
|
# @users = User.all
|
62
62
|
# # Fetched via GET "/users"
|
63
63
|
def all(params={}) # {{{
|
64
|
-
request(params.merge(:_method => :get, :_path => "#{
|
64
|
+
request(params.merge(:_method => :get, :_path => "#{build_request_path(params)}")) do |parsed_data|
|
65
65
|
new_collection(parsed_data)
|
66
66
|
end
|
67
67
|
end # }}}
|
@@ -76,7 +76,7 @@ module Her
|
|
76
76
|
perform_hook(resource, :before, :create)
|
77
77
|
perform_hook(resource, :before, :save)
|
78
78
|
params = resource.instance_eval { @data }
|
79
|
-
request(params.merge(:_method => :post, :_path => "#{
|
79
|
+
request(params.merge(:_method => :post, :_path => "#{build_request_path(params)}")) do |parsed_data|
|
80
80
|
resource.instance_eval do
|
81
81
|
@data = parsed_data[:data]
|
82
82
|
end
|
@@ -118,7 +118,7 @@ module Her
|
|
118
118
|
perform_hook(resource, :before, :update)
|
119
119
|
perform_hook(resource, :before, :save)
|
120
120
|
end
|
121
|
-
self.class.request(params.merge(:_method => :put, :_path => "#{
|
121
|
+
self.class.request(params.merge(:_method => :put, :_path => "#{request_path}")) do |parsed_data|
|
122
122
|
@data = parsed_data[:data]
|
123
123
|
end
|
124
124
|
self.class.class_eval do
|
@@ -131,7 +131,7 @@ module Her
|
|
131
131
|
perform_hook(resource, :before, :create)
|
132
132
|
perform_hook(resource, :before, :save)
|
133
133
|
end
|
134
|
-
self.class.request(params.merge(:_method => :post, :_path => "#{
|
134
|
+
self.class.request(params.merge(:_method => :post, :_path => "#{request_path}")) do |parsed_data|
|
135
135
|
@data = parsed_data[:data]
|
136
136
|
end
|
137
137
|
self.class.class_eval do
|
@@ -152,7 +152,7 @@ module Her
|
|
152
152
|
params = @data.dup
|
153
153
|
resource = self
|
154
154
|
self.class.class_eval { perform_hook(resource, :before, :destroy) }
|
155
|
-
self.class.request(params.merge(:_method => :delete, :_path => "#{
|
155
|
+
self.class.request(params.merge(:_method => :delete, :_path => "#{request_path}")) do |parsed_data|
|
156
156
|
@data = parsed_data[:data]
|
157
157
|
end
|
158
158
|
self.class.class_eval { perform_hook(resource, :after, :destroy) }
|
@@ -164,9 +164,8 @@ module Her
|
|
164
164
|
# @example
|
165
165
|
# User.destroy_existing(1)
|
166
166
|
# # Called via DELETE "/users/1"
|
167
|
-
def destroy_existing(id) # {{{
|
168
|
-
params
|
169
|
-
request(params.merge(:_method => :delete, :_path => "#{collection_path}/#{id}")) do |parsed_data|
|
167
|
+
def destroy_existing(id, params={}) # {{{
|
168
|
+
request(params.merge(:_method => :delete, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
|
170
169
|
new(parsed_data[:data])
|
171
170
|
end
|
172
171
|
end # }}}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Paths
|
4
|
+
# Defines a custom collection path for the resource
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# class User
|
8
|
+
# include Her::Model
|
9
|
+
# collection_path "/users"
|
10
|
+
# end
|
11
|
+
def collection_path(path=nil) # {{{
|
12
|
+
return @her_collection_path unless path
|
13
|
+
@her_resource_path = "#{path}/:id"
|
14
|
+
@her_collection_path = path
|
15
|
+
end # }}}
|
16
|
+
|
17
|
+
# Defines a custom resource path for the resource
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# class User
|
21
|
+
# include Her::Model
|
22
|
+
# resource_path "/users/:id"
|
23
|
+
# end
|
24
|
+
def resource_path(path=nil) # {{{
|
25
|
+
return @her_resource_path unless path
|
26
|
+
@her_resource_path = path
|
27
|
+
end # }}}
|
28
|
+
|
29
|
+
# Return a custom path based on the collection path and variable parameters
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# class User
|
33
|
+
# include Her::Model
|
34
|
+
# collection_path "/utilisateurs"
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# User.all # Fetched via GET /utilisateurs
|
38
|
+
def build_request_path(parameters={}) # {{{
|
39
|
+
(path = parameters.include?(:id) ? @her_resource_path : @her_collection_path).gsub(/:([\w_]+)/) do
|
40
|
+
# Look for :key or :_key, otherwise raise an exception
|
41
|
+
parameters[$1.to_sym] || parameters["_#{$1}".to_sym] || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
|
42
|
+
end
|
43
|
+
end # }}}
|
44
|
+
|
45
|
+
# Return a path based on the collection path and a resource data
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# class User
|
49
|
+
# include Her::Model
|
50
|
+
# collection_path "/utilisateurs"
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# User.find(1) # Fetched via GET /utilisateurs/1
|
54
|
+
def request_path # {{{
|
55
|
+
self.class.build_request_path(@data)
|
56
|
+
end # }}}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -49,11 +49,10 @@ module Her
|
|
49
49
|
def has_many(name, attrs={}) # {{{
|
50
50
|
@her_relationships ||= {}
|
51
51
|
(@her_relationships[:has_many] ||= []) << attrs.merge(:name => name)
|
52
|
-
collection_path = @her_collection_path
|
53
52
|
|
54
53
|
define_method(name) do
|
55
54
|
return @data[name] if @data.include?(name) # Do not fetch from API again if we have it in @data
|
56
|
-
self.class.get_collection("#{
|
55
|
+
self.class.get_collection("#{self.class.build_request_path(:id => id)}/#{name.to_s.pluralize}")
|
57
56
|
end
|
58
57
|
end # }}}
|
59
58
|
|
@@ -78,11 +77,10 @@ module Her
|
|
78
77
|
def has_one(name, attrs={}) # {{{
|
79
78
|
@her_relationships ||= {}
|
80
79
|
(@her_relationships[:has_one] ||= []) << attrs.merge(:name => name)
|
81
|
-
collection_path = @her_collection_path
|
82
80
|
|
83
81
|
define_method(name) do
|
84
82
|
return @data[name] if @data.include?(name) # Do not fetch from API again if we have it in @data
|
85
|
-
self.class.get_resource("#{
|
83
|
+
self.class.get_resource("#{self.class.build_request_path(:id => id)}/#{name.to_s.singularize}")
|
86
84
|
end
|
87
85
|
end # }}}
|
88
86
|
|
@@ -107,11 +105,10 @@ module Her
|
|
107
105
|
def belongs_to(name, attrs={}) # {{{
|
108
106
|
@her_relationships ||= {}
|
109
107
|
(@her_relationships[:belongs_to] ||= []) << attrs.merge(:name => name)
|
110
|
-
collection_path = @her_collection_path
|
111
108
|
|
112
109
|
define_method(name) do
|
113
110
|
return @data[name] if @data.include?(name) # Do not fetch from API again if we have it in @data
|
114
|
-
self.class.get_resource("#{Object.const_get(name.to_s.classify).
|
111
|
+
self.class.get_resource("#{Object.const_get(name.to_s.classify).build_request_path(:id => @data["#{name}_id".to_sym])}")
|
115
112
|
end
|
116
113
|
end # }}}
|
117
114
|
end
|
data/lib/her/version.rb
CHANGED
data/spec/api_spec.rb
CHANGED
@@ -16,36 +16,6 @@ describe Her::API do
|
|
16
16
|
@api.setup :base_uri => "https://api.example.com"
|
17
17
|
@api.base_uri.should == "https://api.example.com"
|
18
18
|
end # }}}
|
19
|
-
|
20
|
-
it "sets additional middleware" do # {{{
|
21
|
-
class Foo < Faraday::Response::Middleware; end;
|
22
|
-
class Bar < Faraday::Response::Middleware; end;
|
23
|
-
|
24
|
-
@api = Her::API.new
|
25
|
-
@api.setup :base_uri => "https://api.example.com", :add_middleware => [Foo, Bar]
|
26
|
-
@api.middleware.should == [Foo, Bar, Her::Middleware::FirstLevelParseJSON, Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]
|
27
|
-
|
28
|
-
@api = Her::API.new
|
29
|
-
@api.setup :base_uri => "https://api.example.com", :add_middleware => Foo
|
30
|
-
@api.middleware.should == [Foo, Her::Middleware::FirstLevelParseJSON, Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]
|
31
|
-
end # }}}
|
32
|
-
|
33
|
-
it "overrides middleware" do # {{{
|
34
|
-
class Foo < Faraday::Response::Middleware; end;
|
35
|
-
class Bar < Faraday::Response::Middleware; end;
|
36
|
-
|
37
|
-
@api = Her::API.new
|
38
|
-
@api.setup :base_uri => "https://api.example.com", :middleware => [Foo, Bar]
|
39
|
-
@api.middleware.should == [Foo, Bar]
|
40
|
-
end # }}}
|
41
|
-
|
42
|
-
it "sets a parse middleware" do # {{{
|
43
|
-
class Foo < Faraday::Response::Middleware; end;
|
44
|
-
|
45
|
-
@api = Her::API.new
|
46
|
-
@api.setup :base_uri => "https://api.example.com", :parse_middleware => Foo
|
47
|
-
@api.middleware.should == [Foo, Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]
|
48
|
-
end # }}}
|
49
19
|
end
|
50
20
|
|
51
21
|
describe "#request" do
|
@@ -59,22 +29,20 @@ describe Her::API do
|
|
59
29
|
end
|
60
30
|
|
61
31
|
@api = Her::API.new
|
62
|
-
@api.setup :base_uri => "https://api.example.com", :
|
63
|
-
@api.request(:_method => :get, :_path => "/foo")
|
64
|
-
|
65
|
-
end
|
32
|
+
@api.setup :base_uri => "https://api.example.com", :parse_middleware => Foo
|
33
|
+
parsed_data = @api.request(:_method => :get, :_path => "/foo")
|
34
|
+
parsed_data[:data] == "Foo, it is."
|
66
35
|
end # }}}
|
67
36
|
|
68
37
|
it "parses a request with the default parser" do # {{{
|
69
|
-
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => MultiJson.dump(
|
38
|
+
FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => MultiJson.dump({ :id => 1, :name => "George Michael Bluth", :errors => ["This is a single error"], :metadata => { :page => 1, :per_page => 10 } }))
|
70
39
|
|
71
40
|
@api = Her::API.new
|
72
41
|
@api.setup :base_uri => "https://api.example.com"
|
73
|
-
@api.request(:_method => :get, :_path => "users/1")
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
42
|
+
parsed_data = @api.request(:_method => :get, :_path => "users/1")
|
43
|
+
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
44
|
+
parsed_data[:errors].should == ["This is a single error"]
|
45
|
+
parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
|
78
46
|
end # }}}
|
79
47
|
|
80
48
|
it "parses a request with a custom parser" do # {{{
|
@@ -94,12 +62,14 @@ describe Her::API do
|
|
94
62
|
end
|
95
63
|
|
96
64
|
@api = Her::API.new
|
97
|
-
@api.setup :base_uri => "https://api.example.com"
|
98
|
-
|
99
|
-
|
100
|
-
parsed_data[:errors].should == []
|
101
|
-
parsed_data[:metadata].should == {}
|
65
|
+
@api.setup :base_uri => "https://api.example.com" do |connection|
|
66
|
+
connection.delete Her::Middleware::DefaultParseJSON
|
67
|
+
connection.use CustomParser
|
102
68
|
end
|
69
|
+
parsed_data = @api.request(:_method => :get, :_path => "users/1")
|
70
|
+
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
71
|
+
parsed_data[:errors].should == []
|
72
|
+
parsed_data[:metadata].should == {}
|
103
73
|
end # }}}
|
104
74
|
end
|
105
75
|
end
|