Module: CrowdPay::ClassMethods

Defined in:
lib/crowd_pay.rb

Instance Method Summary collapse

Instance Method Details

#build_hash_object(obj, attributes) ⇒ Object



94
95
96
97
98
99
# File 'lib/crowd_pay.rb', line 94

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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/crowd_pay.rb', line 78

def build_object status, attributes
  obj = self.new
  case status
  when 200, 201
    build_hash_object obj, attributes
  when 409
    build_hash_object obj, attributes['ModelObject']
  when 400, 405, 404
    #FIX ME: 404 catching is not tested
    obj.populate_errors attributes
  else
      obj.errors.add(:base, "Unknown Error Status #{status}: crowd_pay.rb#parse method")
  end
  return obj
end

#create_connectionObject



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

def create_connection
  @@connection = Faraday.new(:url => domain) do |faraday|
    # faraday.response :logger if Rails.env.develop? || Rails.env.test? # log requests to STDOUT
    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
  end
end

#delete(url) ⇒ Object



129
130
131
132
133
134
# File 'lib/crowd_pay.rb', line 129

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

#get(url) ⇒ Object



101
102
103
104
105
106
# File 'lib/crowd_pay.rb', line 101

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

#parse(response) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/crowd_pay.rb', line 108

def post(url, data, cip_by_pass_validation=false)
  data = data.to_json unless data.kind_of? 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) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/crowd_pay.rb', line 119

def put(url, data)
  data = data.to_json unless data.kind_of? String

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