her 0.3.3 → 0.3.4

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/Rakefile CHANGED
@@ -7,12 +7,12 @@ require "rspec/core/rake_task"
7
7
  task :default => :spec
8
8
 
9
9
  desc "Run all specs"
10
- RSpec::Core::RakeTask.new(:spec) do |task| # {{{
10
+ RSpec::Core::RakeTask.new(:spec) do |task|
11
11
  task.pattern = "spec/**/*_spec.rb"
12
- end # }}}
12
+ end
13
13
 
14
14
  desc "Generate YARD Documentation"
15
- YARD::Rake::YardocTask.new do |task| # {{{
15
+ YARD::Rake::YardocTask.new do |task|
16
16
  task.options = [
17
17
  "-o", File.expand_path("../doc", __FILE__),
18
18
  "--readme=README.md",
@@ -24,4 +24,4 @@ YARD::Rake::YardocTask.new do |task| # {{{
24
24
  "--title=Her",
25
25
  ]
26
26
  task.files = ["lib/**/*.rb"]
27
- end # }}}
27
+ end
data/lib/her/api.rb CHANGED
@@ -6,10 +6,10 @@ module Her
6
6
  attr_reader :base_uri, :connection, :options
7
7
 
8
8
  # Setup a default API connection. Accepted arguments and options are the same as {API#setup}.
9
- def self.setup(attrs={}, &block) # {{{
9
+ def self.setup(attrs={}, &block)
10
10
  @@default_api = new
11
11
  @@default_api.setup(attrs, &block)
12
- end # }}}
12
+ end
13
13
 
14
14
  # Setup the API connection.
15
15
  #
@@ -49,21 +49,21 @@ module Her
49
49
  # connection.use MyCustomParser
50
50
  # connection.use Faraday::Adapter::NetHttp
51
51
  # end
52
- def setup(attrs={}) # {{{
52
+ def setup(attrs={})
53
53
  attrs[:url] = attrs.delete(:base_uri) if attrs.include?(:base_uri) # Support legacy :base_uri option
54
54
  @base_uri = attrs[:url]
55
55
  @options = attrs
56
56
  @connection = Faraday.new(attrs.slice(:url, :ssl)) do |connection|
57
57
  yield connection if block_given?
58
58
  end
59
- end # }}}
59
+ end
60
60
 
61
61
  # Define a custom parsing procedure. The procedure is passed the response object and is
62
62
  # expected to return a hash with three keys: a main data Hash, an errors Array
63
63
  # and a metadata Hash.
64
64
  #
65
65
  # @private
66
- def request(attrs={}) # {{{
66
+ def request(attrs={})
67
67
  method = attrs.delete(:_method)
68
68
  path = attrs.delete(:_path)
69
69
  headers = attrs.delete(:_headers)
@@ -80,12 +80,12 @@ module Her
80
80
  end
81
81
  end
82
82
  response.env[:body]
83
- end # }}}
83
+ end
84
84
 
85
85
  private
86
86
  # @private
87
- def self.default_api(attrs={}) # {{{
87
+ def self.default_api(attrs={})
88
88
  defined?(@@default_api) ? @@default_api : nil
89
- end # }}}
89
+ end
90
90
  end
91
91
  end
@@ -3,10 +3,10 @@ module Her
3
3
  attr_reader :metadata, :errors
4
4
 
5
5
  # @private
6
- def initialize(items=[], metadata={}, errors=[]) # {{{
6
+ def initialize(items=[], metadata={}, errors=[])
7
7
  super(items)
8
8
  @metadata = metadata || {}
9
9
  @errors = errors || []
10
- end # }}}
10
+ end
11
11
  end
12
12
  end
@@ -2,14 +2,14 @@ module Her
2
2
  module Middleware
3
3
  # This middleware adds a "Accept: application/json" HTTP header
4
4
  class AcceptJSON < Faraday::Middleware
5
- def add_header(headers) # {{{
5
+ def add_header(headers)
6
6
  headers.merge! "Accept" => "application/json"
7
- end # }}}
7
+ end
8
8
 
9
- def call(env) # {{{
9
+ def call(env)
10
10
  add_header(env[:request_headers])
11
11
  @app.call(env)
12
- end # }}}
12
+ end
13
13
  end
14
14
  end
15
15
  end
@@ -6,7 +6,7 @@ module Her
6
6
  #
7
7
  # @param [String] body The response body
8
8
  # @return [Mixed] the parsed response
9
- def parse(body) # {{{
9
+ def parse(body)
10
10
  json = MultiJson.load(body, :symbolize_keys => true)
11
11
  errors = json.delete(:errors) || []
12
12
  metadata = json.delete(:metadata) || []
@@ -15,15 +15,15 @@ module Her
15
15
  :errors => errors,
16
16
  :metadata => metadata
17
17
  }
18
- end # }}}
18
+ end
19
19
 
20
20
  # This method is triggered when the response has been received. It modifies
21
21
  # the value of `env[:body]`.
22
22
  #
23
23
  # @param [Hash] env The response environment
24
- def on_complete(env) # {{{
24
+ def on_complete(env)
25
25
  env[:body] = parse(env[:body])
26
- end # }}}
26
+ end
27
27
  end
28
28
  end
29
29
  end
@@ -7,22 +7,22 @@ module Her
7
7
  #
8
8
  # @param [String] body The response body
9
9
  # @return [Mixed] the parsed response
10
- def parse(body) # {{{
10
+ def parse(body)
11
11
  json = MultiJson.load(body, :symbolize_keys => true)
12
12
  {
13
13
  :data => json[:data],
14
14
  :errors => json[:errors],
15
15
  :metadata => json[:metadata]
16
16
  }
17
- end # }}}
17
+ end
18
18
 
19
19
  # This method is triggered when the response has been received. It modifies
20
20
  # the value of `env[:body]`.
21
21
  #
22
22
  # @param [Hash] env The response environment
23
- def on_complete(env) # {{{
23
+ def on_complete(env)
24
24
  env[:body] = parse(env[:body])
25
- end # }}}
25
+ end
26
26
  end
27
27
  end
28
28
  end
@@ -53,14 +53,14 @@ module Her
53
53
 
54
54
  # Wrap a block between “before” and “after” hooks
55
55
  # @private
56
- def wrap_in_hooks(resource, *hooks) # {{{
56
+ def wrap_in_hooks(resource, *hooks)
57
57
  perform_before_hooks(resource, *hooks)
58
58
  yield(resource, resource.class)
59
59
  perform_after_hooks(resource, *hooks.reverse)
60
- end # }}}
60
+ end
61
61
 
62
62
  # @private
63
- def hooks # {{{
63
+ def hooks
64
64
  @her_hooks ||= begin
65
65
  if superclass.respond_to?(:hooks)
66
66
  superclass.hooks.dup
@@ -68,16 +68,16 @@ module Her
68
68
  {}
69
69
  end
70
70
  end
71
- end # }}}
71
+ end
72
72
 
73
73
  private
74
74
  # @private
75
- def set_hook(time, name, action) # {{{
75
+ def set_hook(time, name, action)
76
76
  (self.hooks["#{time}_#{name}".to_sym] ||= []) << action
77
- end # }}}
77
+ end
78
78
 
79
79
  # @private
80
- def perform_hook(record, time, name) # {{{
80
+ def perform_hook(record, time, name)
81
81
  Array(self.hooks["#{time}_#{name}".to_sym]).each do |hook|
82
82
  if hook.is_a? Symbol
83
83
  record.send(hook)
@@ -85,23 +85,23 @@ module Her
85
85
  hook.call(record)
86
86
  end
87
87
  end
88
- end # }}}
88
+ end
89
89
 
90
90
  # Perform “after” hooks on a resource
91
91
  # @private
92
- def perform_after_hooks(resource, *hooks) # {{{
92
+ def perform_after_hooks(resource, *hooks)
93
93
  hooks.each do |hook|
94
94
  perform_hook(resource, :after, hook)
95
95
  end
96
- end # }}}
96
+ end
97
97
 
98
98
  # Perform “before” hooks on a resource
99
99
  # @private
100
- def perform_before_hooks(resource, *hooks) # {{{
100
+ def perform_before_hooks(resource, *hooks)
101
101
  hooks.each do |hook|
102
102
  perform_hook(resource, :before, hook)
103
103
  end
104
- end # }}}
104
+ end
105
105
  end
106
106
  end
107
107
  end
@@ -3,27 +3,27 @@ module Her
3
3
  # This module interacts with Her::API to fetch HTTP data
4
4
  module HTTP
5
5
  # Automatically inherit a superclass' api
6
- def her_api # {{{
6
+ def her_api
7
7
  @her_api ||= begin
8
8
  superclass.her_api if superclass.respond_to?(:her_api)
9
9
  end
10
- end # }}}
10
+ end
11
11
 
12
12
  # Link a model with a Her::API object
13
- def uses_api(api) # {{{
13
+ def uses_api(api)
14
14
  @her_api = api
15
- end # }}}
15
+ end
16
16
 
17
17
  # Main request wrapper around Her::API. Used to make custom request to the API.
18
18
  # @private
19
- def request(attrs={}) # {{{
19
+ def request(attrs={})
20
20
  parsed_data = her_api.request(attrs)
21
21
  if block_given?
22
22
  yield parsed_data
23
23
  else
24
24
  parsed_data
25
25
  end
26
- end # }}}
26
+ end
27
27
 
28
28
  # Make a GET request and return either a collection or a resource
29
29
  #
@@ -34,7 +34,7 @@ module Her
34
34
  #
35
35
  # @popular_users = User.get(:popular)
36
36
  # # Fetched via GET "/users/popular"
37
- def get(path, attrs={}) # {{{
37
+ def get(path, attrs={})
38
38
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
39
39
  get_raw(path, attrs) do |parsed_data|
40
40
  if parsed_data[:data].is_a?(Array)
@@ -43,32 +43,32 @@ module Her
43
43
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
44
44
  end
45
45
  end
46
- end # }}}
46
+ end
47
47
 
48
48
  # Make a GET request and return the parsed JSON response (not mapped to objects)
49
- def get_raw(path, attrs={}, &block) # {{{
49
+ def get_raw(path, attrs={}, &block)
50
50
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
51
51
  request(attrs.merge(:_method => :get, :_path => path), &block)
52
- end # }}}
52
+ end
53
53
 
54
54
  # Make a GET request and return a collection of resources
55
- def get_collection(path, attrs={}) # {{{
55
+ def get_collection(path, attrs={})
56
56
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
57
57
  get_raw(path, attrs) do |parsed_data|
58
58
  new_collection(parsed_data)
59
59
  end
60
- end # }}}
60
+ end
61
61
 
62
62
  # Make a GET request and return a collection of resources
63
- def get_resource(path, attrs={}) # {{{
63
+ def get_resource(path, attrs={})
64
64
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
65
65
  get_raw(path, attrs) do |parsed_data|
66
66
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
67
67
  end
68
- end # }}}
68
+ end
69
69
 
70
70
  # Make a POST request and return either a collection or a resource
71
- def post(path, attrs={}) # {{{
71
+ def post(path, attrs={})
72
72
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
73
73
  post_raw(path, attrs) do |parsed_data|
74
74
  if parsed_data[:data].is_a?(Array)
@@ -77,32 +77,32 @@ module Her
77
77
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
78
78
  end
79
79
  end
80
- end # }}}
80
+ end
81
81
 
82
82
  # Make a POST request and return the parsed JSON response (not mapped to objects)
83
- def post_raw(path, attrs={}, &block) # {{{
83
+ def post_raw(path, attrs={}, &block)
84
84
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
85
85
  request(attrs.merge(:_method => :post, :_path => path), &block)
86
- end # }}}
86
+ end
87
87
 
88
88
  # Make a POST request and return a collection of resources
89
- def post_collection(path, attrs={}) # {{{
89
+ def post_collection(path, attrs={})
90
90
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
91
91
  post_raw(path, attrs) do |parsed_data|
92
92
  new_collection(parsed_data)
93
93
  end
94
- end # }}}
94
+ end
95
95
 
96
96
  # Make a POST request and return a collection of resources
97
- def post_resource(path, attrs={}) # {{{
97
+ def post_resource(path, attrs={})
98
98
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
99
99
  post_raw(path, attrs) do |parsed_data|
100
100
  new(parsed_data[:data])
101
101
  end
102
- end # }}}
102
+ end
103
103
 
104
104
  # Make a PUT request and return either a collection or a resource
105
- def put(path, attrs={}) # {{{
105
+ def put(path, attrs={})
106
106
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
107
107
  put_raw(path, attrs) do |parsed_data|
108
108
  if parsed_data[:data].is_a?(Array)
@@ -111,32 +111,32 @@ module Her
111
111
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
112
112
  end
113
113
  end
114
- end # }}}
114
+ end
115
115
 
116
116
  # Make a PUT request and return the parsed JSON response (not mapped to objects)
117
- def put_raw(path, attrs={}, &block) # {{{
117
+ def put_raw(path, attrs={}, &block)
118
118
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
119
119
  request(attrs.merge(:_method => :put, :_path => path), &block)
120
- end # }}}
120
+ end
121
121
 
122
122
  # Make a PUT request and return a collection of resources
123
- def put_collection(path, attrs={}) # {{{
123
+ def put_collection(path, attrs={})
124
124
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
125
125
  put_raw(path, attrs) do |parsed_data|
126
126
  new_collection(parsed_data)
127
127
  end
128
- end # }}}
128
+ end
129
129
 
130
130
  # Make a PUT request and return a collection of resources
131
- def put_resource(path, attrs={}) # {{{
131
+ def put_resource(path, attrs={})
132
132
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
133
133
  put_raw(path, attrs) do |parsed_data|
134
134
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
135
135
  end
136
- end # }}}
136
+ end
137
137
 
138
138
  # Make a PATCH request and return either a collection or a resource
139
- def patch(path, attrs={}) # {{{
139
+ def patch(path, attrs={})
140
140
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
141
141
  patch_raw(path, attrs) do |parsed_data|
142
142
  if parsed_data[:data].is_a?(Array)
@@ -145,32 +145,32 @@ module Her
145
145
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
146
146
  end
147
147
  end
148
- end # }}}
148
+ end
149
149
 
150
150
  # Make a PATCH request and return the parsed JSON response (not mapped to objects)
151
- def patch_raw(path, attrs={}, &block) # {{{
151
+ def patch_raw(path, attrs={}, &block)
152
152
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
153
153
  request(attrs.merge(:_method => :patch, :_path => path), &block)
154
- end # }}}
154
+ end
155
155
 
156
156
  # Make a PATCH request and return a collection of resources
157
- def patch_collection(path, attrs={}) # {{{
157
+ def patch_collection(path, attrs={})
158
158
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
159
159
  patch_raw(path, attrs) do |parsed_data|
160
160
  new_collection(parsed_data)
161
161
  end
162
- end # }}}
162
+ end
163
163
 
164
164
  # Make a PATCH request and return a collection of resources
165
- def patch_resource(path, attrs={}) # {{{
165
+ def patch_resource(path, attrs={})
166
166
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
167
167
  patch_raw(path, attrs) do |parsed_data|
168
168
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
169
169
  end
170
- end # }}}
170
+ end
171
171
 
172
172
  # Make a DELETE request and return either a collection or a resource
173
- def delete(path, attrs={}) # {{{
173
+ def delete(path, attrs={})
174
174
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
175
175
  delete_raw(path, attrs) do |parsed_data|
176
176
  if parsed_data[:data].is_a?(Array)
@@ -179,29 +179,29 @@ module Her
179
179
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
180
180
  end
181
181
  end
182
- end # }}}
182
+ end
183
183
 
184
184
  # Make a DELETE request and return the parsed JSON response (not mapped to objects)
185
- def delete_raw(path, attrs={}, &block) # {{{
185
+ def delete_raw(path, attrs={}, &block)
186
186
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
187
187
  request(attrs.merge(:_method => :delete, :_path => path), &block)
188
- end # }}}
188
+ end
189
189
 
190
190
  # Make a DELETE request and return a collection of resources
191
- def delete_collection(path, attrs={}) # {{{
191
+ def delete_collection(path, attrs={})
192
192
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
193
193
  delete_raw(path, attrs) do |parsed_data|
194
194
  new_collection(parsed_data)
195
195
  end
196
- end # }}}
196
+ end
197
197
 
198
198
  # Make a DELETE request and return a collection of resources
199
- def delete_resource(path, attrs={}) # {{{
199
+ def delete_resource(path, attrs={})
200
200
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
201
201
  delete_raw(path, attrs) do |parsed_data|
202
202
  new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
203
203
  end
204
- end # }}}
204
+ end
205
205
 
206
206
  # Define custom GET requests
207
207
  #
@@ -213,54 +213,54 @@ module Her
213
213
  #
214
214
  # User.popular
215
215
  # # Fetched from GET "/users/popular"
216
- def custom_get(*paths) # {{{
216
+ def custom_get(*paths)
217
217
  metaclass = (class << self; self; end)
218
218
  paths.each do |path|
219
219
  metaclass.send(:define_method, path.to_sym) do |*attrs|
220
220
  get(path, attrs.first || Hash.new)
221
221
  end
222
222
  end
223
- end # }}}
223
+ end
224
224
 
225
225
  # Define custom POST requests
226
- def custom_post(*paths) # {{{
226
+ def custom_post(*paths)
227
227
  metaclass = (class << self; self; end)
228
228
  paths.each do |path|
229
229
  metaclass.send(:define_method, path.to_sym) do |*attrs|
230
230
  post(path, attrs.first || Hash.new)
231
231
  end
232
232
  end
233
- end # }}}
233
+ end
234
234
 
235
235
  # Define custom PUT requests
236
- def custom_put(*paths) # {{{
236
+ def custom_put(*paths)
237
237
  metaclass = (class << self; self; end)
238
238
  paths.each do |path|
239
239
  metaclass.send(:define_method, path.to_sym) do |*attrs|
240
240
  put(path, attrs.first || Hash.new)
241
241
  end
242
242
  end
243
- end # }}}
243
+ end
244
244
 
245
245
  # Define custom PATCH requests
246
- def custom_patch(*paths) # {{{
246
+ def custom_patch(*paths)
247
247
  metaclass = (class << self; self; end)
248
248
  paths.each do |path|
249
249
  metaclass.send(:define_method, path.to_sym) do |*attrs|
250
250
  patch(path, attrs.first || Hash.new)
251
251
  end
252
252
  end
253
- end # }}}
253
+ end
254
254
 
255
255
  # Define custom DELETE requests
256
- def custom_delete(*paths) # {{{
256
+ def custom_delete(*paths)
257
257
  metaclass = (class << self; self; end)
258
258
  paths.each do |path|
259
259
  metaclass.send(:define_method, path.to_sym) do |*attrs|
260
260
  delete(path, attrs.first || Hash.new)
261
261
  end
262
262
  end
263
- end # }}}
263
+ end
264
264
  end
265
265
  end
266
266
  end