module Her::Model::HTTP

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

Public Instance Methods

custom_delete(*paths) click to toggle source

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
custom_get(*paths) click to toggle source

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
custom_patch(*paths) click to toggle source

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
custom_post(*paths) click to toggle source

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
custom_put(*paths) click to toggle source

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
delete(path, attrs={}) click to toggle source

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
delete_collection(path, attrs={}) click to toggle source

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
delete_raw(path, attrs={}, &block) click to toggle source

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
delete_resource(path, attrs={}) click to toggle source

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
get(path, attrs={}) click to toggle source

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
get_collection(path=nil, attrs={}) click to toggle source

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
get_raw(path, attrs={}, &block) click to toggle source

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
get_resource(path, attrs={}) click to toggle source

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
her_api() click to toggle source

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
log(attrs, time) click to toggle source
# 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={}) click to toggle source

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
patch_collection(path, attrs={}) click to toggle source

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
patch_raw(path, attrs={}, &block) click to toggle source

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
patch_resource(path, attrs={}) click to toggle source

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
post(path, attrs={}) click to toggle source

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
post_collection(path, attrs={}) click to toggle source

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
post_raw(path, attrs={}, &block) click to toggle source

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
post_resource(path, attrs={}) click to toggle source

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
put(path, attrs={}) click to toggle source

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
put_collection(path, attrs={}) click to toggle source

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
put_raw(path, attrs={}, &block) click to toggle source

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
put_resource(path, attrs={}) click to toggle source

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
request(attrs={}) { |parsed_data| ... } click to toggle source

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
uses_api(api) click to toggle source

Link a model with a Her::API object

# File lib/her/model/http.rb, line 17
def uses_api(api)
  @her_api = api
end