Module: Her::Model::HTTP

Defined in:
lib/her/model/http.rb

Overview

This module interacts with Her::API to fetch HTTP data

Instance Method Summary collapse

Instance Method Details

#custom_delete(*paths) ⇒ Object

Define custom DELETE requests



274
275
276
277
278
279
280
281
# File 'lib/her/model/http.rb', line 274

def custom_delete(*paths)
  metaclass = (class << self; self; end)
  paths.each do |path|
    metaclass.send(:define_method, path.to_sym) do |*attrs|
      delete(path, attrs.first || Hash.new)
    end
  end
end

#custom_get(*paths) ⇒ Object

Define custom GET requests

Examples:

class User
  include Her::Model
  custom_get :popular
end

User.popular
# Fetched from GET "/users/popular"


234
235
236
237
238
239
240
241
# File 'lib/her/model/http.rb', line 234

def custom_get(*paths)
  metaclass = (class << self; self; end)
  paths.each do |path|
    metaclass.send(:define_method, path.to_sym) do |*attrs|
      get(path, attrs.first || Hash.new)
    end
  end
end

#custom_patch(*paths) ⇒ Object

Define custom PATCH requests



264
265
266
267
268
269
270
271
# File 'lib/her/model/http.rb', line 264

def custom_patch(*paths)
  metaclass = (class << self; self; end)
  paths.each do |path|
    metaclass.send(:define_method, path.to_sym) do |*attrs|
      patch(path, attrs.first || Hash.new)
    end
  end
end

#custom_post(*paths) ⇒ Object

Define custom POST requests



244
245
246
247
248
249
250
251
# File 'lib/her/model/http.rb', line 244

def custom_post(*paths)
  metaclass = (class << self; self; end)
  paths.each do |path|
    metaclass.send(:define_method, path.to_sym) do |*attrs|
      post(path, attrs.first || Hash.new)
    end
  end
end

#custom_put(*paths) ⇒ Object

Define custom PUT requests



254
255
256
257
258
259
260
261
# File 'lib/her/model/http.rb', line 254

def custom_put(*paths)
  metaclass = (class << self; self; end)
  paths.each do |path|
    metaclass.send(:define_method, path.to_sym) do |*attrs|
      put(path, attrs.first || Hash.new)
    end
  end
end

#delete(path, attrs = {}) ⇒ Object

Make a DELETE request and return either a collection or a resource



191
192
193
194
195
196
197
198
199
200
# File 'lib/her/model/http.rb', line 191

def delete(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  delete_raw(path, attrs) do |parsed_data|
    if parsed_data[:data].is_a?(Array)
      new_collection(parsed_data)
    else
      new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
    end
  end
end

#delete_collection(path, attrs = {}) ⇒ Object

Make a DELETE request and return a collection of resources



209
210
211
212
213
214
# File 'lib/her/model/http.rb', line 209

def delete_collection(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  delete_raw(path, attrs) do |parsed_data|
    new_collection(parsed_data)
  end
end

#delete_raw(path, attrs = {}, &block) ⇒ Object

Make a DELETE request and return the parsed JSON response (not mapped to objects)



203
204
205
206
# File 'lib/her/model/http.rb', line 203

def delete_raw(path, attrs={}, &block)
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  request(attrs.merge(:_method => :delete, :_path => path), &block)
end

#delete_resource(path, attrs = {}) ⇒ Object

Make a DELETE request and return a collection of resources



217
218
219
220
221
222
# File 'lib/her/model/http.rb', line 217

def delete_resource(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  delete_raw(path, attrs) do |parsed_data|
    new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
  end
end

#get(path, attrs = {}) ⇒ Object

Make a GET request and return either a collection or a resource

Examples:

class User
  include Her::Model
end

@popular_users = User.get(:popular)
# Fetched via GET "/users/popular"


55
56
57
58
59
60
61
62
63
64
# File 'lib/her/model/http.rb', line 55

def get(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  get_raw(path, attrs) do |parsed_data|
    if parsed_data[:data].is_a?(Array)
      new_collection(parsed_data)
    else
      new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
    end
  end
end

#get_collection(path = nil, attrs = {}) ⇒ Object

Make a GET request and return a collection of resources



73
74
75
76
77
78
# File 'lib/her/model/http.rb', line 73

def get_collection(path=nil, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  get_raw(path, attrs) do |parsed_data|
    new_collection(parsed_data)
  end
end

#get_raw(path, attrs = {}, &block) ⇒ Object

Make a GET request and return the parsed JSON response (not mapped to objects)



67
68
69
70
# File 'lib/her/model/http.rb', line 67

def get_raw(path, attrs={}, &block)
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  request(attrs.merge(:_method => :get, :_path => path), &block)
end

#get_resource(path, attrs = {}) ⇒ Object

Make a GET request and return a collection of resources



81
82
83
84
85
86
# File 'lib/her/model/http.rb', line 81

def get_resource(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  get_raw(path, attrs) do |parsed_data|
    new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
  end
end

#her_apiObject

Automatically inherit a superclass’ api



6
7
8
9
10
11
12
13
14
# File 'lib/her/model/http.rb', line 6

def her_api
  @her_api ||= begin
    if superclass.respond_to?(:her_api)
      superclass.her_api
    else
      Her::API.default_api
    end
  end
end

#log(attrs, time) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/her/model/http.rb', line 37

def log(attrs, time)
  return unless defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger.respond_to?(:debug)

  method = attrs.delete(:_method).to_s.upcase
  path = attrs.delete(:_path)

  Rails.logger.debug("* HER request: #{method} #{path} [#{time}s] #{attrs}")
end

#patch(path, attrs = {}) ⇒ Object

Make a PATCH request and return either a collection or a resource



157
158
159
160
161
162
163
164
165
166
# File 'lib/her/model/http.rb', line 157

def patch(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  patch_raw(path, attrs) do |parsed_data|
    if parsed_data[:data].is_a?(Array)
      new_collection(parsed_data)
    else
      new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
    end
  end
end

#patch_collection(path, attrs = {}) ⇒ Object

Make a PATCH request and return a collection of resources



175
176
177
178
179
180
# File 'lib/her/model/http.rb', line 175

def patch_collection(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  patch_raw(path, attrs) do |parsed_data|
    new_collection(parsed_data)
  end
end

#patch_raw(path, attrs = {}, &block) ⇒ Object

Make a PATCH request and return the parsed JSON response (not mapped to objects)



169
170
171
172
# File 'lib/her/model/http.rb', line 169

def patch_raw(path, attrs={}, &block)
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  request(attrs.merge(:_method => :patch, :_path => path), &block)
end

#patch_resource(path, attrs = {}) ⇒ Object

Make a PATCH request and return a collection of resources



183
184
185
186
187
188
# File 'lib/her/model/http.rb', line 183

def patch_resource(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  patch_raw(path, attrs) do |parsed_data|
    new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
  end
end

#post(path, attrs = {}) ⇒ Object

Make a POST request and return either a collection or a resource



89
90
91
92
93
94
95
96
97
98
# File 'lib/her/model/http.rb', line 89

def post(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  post_raw(path, attrs) do |parsed_data|
    if parsed_data[:data].is_a?(Array)
      new_collection(parsed_data)
    else
      new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
    end
  end
end

#post_collection(path, attrs = {}) ⇒ Object

Make a POST request and return a collection of resources



107
108
109
110
111
112
# File 'lib/her/model/http.rb', line 107

def post_collection(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  post_raw(path, attrs) do |parsed_data|
    new_collection(parsed_data)
  end
end

#post_raw(path, attrs = {}, &block) ⇒ Object

Make a POST request and return the parsed JSON response (not mapped to objects)



101
102
103
104
# File 'lib/her/model/http.rb', line 101

def post_raw(path, attrs={}, &block)
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  request(attrs.merge(:_method => :post, :_path => path), &block)
end

#post_resource(path, attrs = {}) ⇒ Object

Make a POST request and return a collection of resources



115
116
117
118
119
120
# File 'lib/her/model/http.rb', line 115

def post_resource(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  post_raw(path, attrs) do |parsed_data|
    new(parse(parsed_data[:data]))
  end
end

#put(path, attrs = {}) ⇒ Object

Make a PUT request and return either a collection or a resource



123
124
125
126
127
128
129
130
131
132
# File 'lib/her/model/http.rb', line 123

def put(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  put_raw(path, attrs) do |parsed_data|
    if parsed_data[:data].is_a?(Array)
      new_collection(parsed_data)
    else
      new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
    end
  end
end

#put_collection(path, attrs = {}) ⇒ Object

Make a PUT request and return a collection of resources



141
142
143
144
145
146
# File 'lib/her/model/http.rb', line 141

def put_collection(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  put_raw(path, attrs) do |parsed_data|
    new_collection(parsed_data)
  end
end

#put_raw(path, attrs = {}, &block) ⇒ Object

Make a PUT request and return the parsed JSON response (not mapped to objects)



135
136
137
138
# File 'lib/her/model/http.rb', line 135

def put_raw(path, attrs={}, &block)
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  request(attrs.merge(:_method => :put, :_path => path), &block)
end

#put_resource(path, attrs = {}) ⇒ Object

Make a PUT request and return a collection of resources



149
150
151
152
153
154
# File 'lib/her/model/http.rb', line 149

def put_resource(path, attrs={})
  path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
  put_raw(path, attrs) do |parsed_data|
    new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
  end
end

#request(attrs = {}) ⇒ Object

Main request wrapper around Her::API. Used to make custom request to the API.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/her/model/http.rb', line 23

def request(attrs={})
  initial_attrs = attrs.dup
  started = Time.now.to_f
  parsed_data = her_api.request(attrs)
  request_time = Time.now.to_f - started
  log(initial_attrs, request_time)

  if block_given?
    yield parsed_data
  else
    parsed_data
  end
end

#uses_api(api) ⇒ Object

Link a model with a Her::API object



17
18
19
# File 'lib/her/model/http.rb', line 17

def uses_api(api)
  @her_api = api
end