module CrowdPay::ClassMethods

Public Instance Methods

build_hash_object(obj, attributes) click to toggle source
# File lib/crowd_pay.rb, line 95
def build_hash_object(obj, attributes)
  attributes = attributes.each_with_object({}) do |(k, v), hash|
    hash[k.downcase] = v
  end
  obj.assign_attributes(attributes)
end
build_object(status, attributes) click to toggle source
# File lib/crowd_pay.rb, line 78
def build_object(status, attributes)
  obj = new

  case status
  when 200, 201
    build_hash_object obj, attributes
  when 409
    build_hash_object obj, attributes['ModelObject']
  when 400, 405, 404
    obj.populate_errors attributes
  else
    obj.errors.add(:base, "Unknown Error Status #{status}: crowd_pay.rb#parse method")
  end

  obj
end
create_connection() click to toggle source
# File lib/crowd_pay.rb, line 55
def create_connection
  @@connection = Faraday.new(url: domain) do |faraday|
    faraday.adapter Faraday.default_adapter

    faraday.headers['X-ApiKey'] = api_key
    faraday.headers['X-PortalKey'] = portal_key
    faraday.headers['X-ByPassValidation'] = by_pass_validation if by_pass_validation
    faraday.headers['Authorization'] = authorization if authorization
  end
end
delete(url) click to toggle source
# File lib/crowd_pay.rb, line 130
def delete(url)
  connection.delete do |req|
    req.url(url)
    req.headers['Content-Type'] = 'application/json'
  end
end
get(url) click to toggle source
# File lib/crowd_pay.rb, line 102
def get(url)
  connection.get do |req|
    req.url(url)
    req.headers['Content-Type'] = 'application/json'
  end
end
parse(response) click to toggle source
# File lib/crowd_pay.rb, line 66
def parse(response)
  body = JSON.parse response.body

  if body.is_a? Hash
    build_object response.status, body
  else
    body.map do |attributes|
      build_object response.status, attributes
    end
  end
end
post(url, data, cip_by_pass_validation = false) click to toggle source
# File lib/crowd_pay.rb, line 109
def post(url, data, cip_by_pass_validation = false)
  data = data.to_json unless data.is_a? String

  connection.post do |req|
    req.url(url)
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-CipByPassValidation'] = 'true' if cip_by_pass_validation
    req.body = data
  end
end
put(url, data) click to toggle source
# File lib/crowd_pay.rb, line 120
def put(url, data)
  data = data.to_json unless data.is_a? String

  connection.put do |req|
    req.url(url)
    req.headers['Content-Type'] = 'application/json'
    req.body = data
  end
end

Private Instance Methods

register_association(assoc_name, details) click to toggle source
# File lib/crowd_pay.rb, line 139
def register_association(assoc_name, details)
  hash = class_variable_get(:@@associations)
  class_variable_set(:@@associations, hash.merge({ assoc_name => details.symbolize_keys }.symbolize_keys))
  attr_accessor assoc_name.to_sym
end