Module: CrowdPay::ClassMethods

Defined in:
lib/crowd_pay.rb

Instance Method Summary collapse

Instance Method Details

#build_hash_object(obj, attributes) ⇒ Object



95
96
97
98
99
100
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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_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.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) ⇒ Object



130
131
132
133
134
135
# 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) ⇒ Object



102
103
104
105
106
107
# 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) ⇒ 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



109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



120
121
122
123
124
125
126
127
128
# 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