module Her::Model::HTTP
Public Instance Methods
Define custom DELETE requests
# 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
Define custom GET requests
@example
class User include Her::Model custom_get :popular end User.popular # Fetched from GET "/users/popular"
# 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
Define custom PATCH requests
# 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
Define custom POST requests
# 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
Define custom PUT requests
# 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
Make a DELETE request and return either a collection or a resource
# 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
Make a DELETE request and return a collection of resources
# 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
Make a DELETE request and return the parsed JSON response (not mapped to objects)
# 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
Make a DELETE request and return a collection of resources
# 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
Make a GET request and return either a collection or a resource
@example
class User include Her::Model end @popular_users = User.get(:popular) # Fetched via GET "/users/popular"
# 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
Make a GET request and return a collection of resources
# 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
Make a GET request and return the parsed JSON response (not mapped to objects)
# 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
Make a GET request and return a collection of resources
# 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
Automatically inherit a superclass’ api
# 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
# 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
Make a PATCH request and return either a collection or a resource
# 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
Make a PATCH request and return a collection of resources
# 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
Make a PATCH request and return the parsed JSON response (not mapped to objects)
# 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
Make a PATCH request and return a collection of resources
# 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
Make a POST request and return either a collection or a resource
# 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
Make a POST request and return a collection of resources
# 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
Make a POST request and return the parsed JSON response (not mapped to objects)
# 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
Make a POST request and return a collection of resources
# 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
Make a PUT request and return either a collection or a resource
# 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
Make a PUT request and return a collection of resources
# 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
Make a PUT request and return the parsed JSON response (not mapped to objects)
# 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
Make a PUT request and return a collection of resources
# 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
Main request wrapper around Her::API
. Used to make custom request to the API
. @private
# 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
Link a model with a Her::API
object
# File lib/her/model/http.rb, line 17 def uses_api(api) @her_api = api end