module Validic::REST::Request

Public Instance Methods

latest(type, options = {}) click to toggle source
# File lib/validic/rest/request.rb, line 7
def latest(type, options = {})
  organization_id = options[:organization_id] || Validic.organization_id
  user_id = options.delete(:user_id)
  if user_id
    path = "#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/latest.json"
  else
    path = "#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}/latest.json"
  end
  get(path, options)
end

Private Instance Methods

construct_path(type, options) click to toggle source
# File lib/validic/rest/request.rb, line 40
def construct_path(type, options)
  organization_id = options.delete(:organization_id) || Validic.organization_id
  user_id = options.delete(:user_id)
  activity_id = options.delete(:_id)
  if activity_id
    path = "#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/#{activity_id}.json"
  elsif user_id && type == :users
    path = "#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}.json"
  elsif user_id
    path = "#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}.json"
  elsif type == :me
    path = "#{Validic.api_version}/me.json"
  elsif type == :profile
    path = "#{Validic.api_version}/profile.json"
  elsif type == :organizations
    path = "#{Validic.api_version}/organizations/#{organization_id}.json"
  else
    path = "#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}.json"
  end
  path
end
delete(path, options) click to toggle source
# File lib/validic/rest/request.rb, line 74
def delete(path, options)
  request(:delete, path, options)
end
delete_request(type, options = {}) click to toggle source
# File lib/validic/rest/request.rb, line 35
def delete_request(type, options = {})
  path = construct_path(type, options)
  delete(path, options)
end
get(path, options) click to toggle source
# File lib/validic/rest/request.rb, line 62
def get(path, options)
  request(:get, path, options)
end
get_request(type, options = {}) click to toggle source
# File lib/validic/rest/request.rb, line 20
def get_request(type, options = {})
  path = construct_path(type, options)
  get(path, options)
end
post(path, options) click to toggle source
# File lib/validic/rest/request.rb, line 66
def post(path, options)
  request(:post, path, options)
end
post_request(type, options = {}) click to toggle source
# File lib/validic/rest/request.rb, line 25
def post_request(type, options = {})
  path = construct_path(type, options)
  post(path, options)
end
put(path, options) click to toggle source
# File lib/validic/rest/request.rb, line 70
def put(path, options)
  request(:put, path, options)
end
put_request(type, options = {}) click to toggle source
# File lib/validic/rest/request.rb, line 30
def put_request(type, options = {})
  path = construct_path(type, options)
  put(path, options)
end
request(method, path, options) click to toggle source
# File lib/validic/rest/request.rb, line 78
def request(method, path, options)
  options[:access_token] = options[:access_token].nil? ? Validic.access_token : options[:access_token]
  response = connection.send(method) do |request|
    case method
    when :get
      request.url(path, options)
    when :post, :put, :delete
      request.path = path
      request.body = MultiJson.encode(options) unless options.empty?
    end
  end
  error = Validic::Error::ERRORS[response.status]
  raise error.from_response(response.body) if error
  response.body
end