class OldPlaid::Connection

Public Class Methods

delete(path, options = {}) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 53
def delete(path, options = {})
  uri = build_uri(path)
  options.merge!(client_id: OldPlaid.customer_id, secret: OldPlaid.secret)
  req = Net::HTTP::Delete.new(uri.path)
  req.body = URI.encode_www_form(options) if options
  req.content_type = 'application/x-www-form-urlencoded'
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
end
get(path, id = nil) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 21
def get(path, id = nil)
  uri = build_uri(path,id)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.path)
  res = http.request(request)
  parse_get_response(res.body)
end
patch(path, options = {}) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 42
def patch(path, options = {})
  uri = build_uri(path)
  options.merge!(client_id: OldPlaid.customer_id, secret: OldPlaid.secret)
  req = Net::HTTP::Patch.new(uri.path)
  req.body = URI.encode_www_form(options) if options
  req.content_type = 'application/x-www-form-urlencoded'
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  parse_response(res)
end
post(path, options = {}) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 9
def post(path, options = {})
  uri = build_uri(path)
  options.merge!(client_id: OldPlaid.customer_id, secret: OldPlaid.secret)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.path)
  request.set_form_data(options)
  res = http.request(request)
  parse_response(res)
end
secure_get(path, access_token, options = {}) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 31
def secure_get(path, access_token, options = {})
  uri = build_uri(path)
  options.merge!({access_token:access_token, client_id: OldPlaid.customer_id, secret: OldPlaid.secret})
  req = Net::HTTP::Get.new(uri.path)
  req.body = URI.encode_www_form(options) if options
  req.content_type = 'application/x-www-form-urlencoded'
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  parse_response(res)
end

Protected Class Methods

build_uri(path, option = nil) click to toggle source

API: semi-private

# File lib/old_plaid/connection.rb, line 65
def build_uri(path, option = nil)
  path = path + '/' + option unless option.nil?
  URI.parse(OldPlaid.environment_location + path)
end

Private Class Methods

parse_get_response(res) click to toggle source
# File lib/old_plaid/connection.rb, line 93
def parse_get_response(res)
  body = JSON.parse(res)
  return body if body.kind_of?(Array)

  case body['code']
  when nil
    body
  when 1301, 1401, 1501, 1601
    raise OldPlaid::NotFound.new(body['code'], body['message'], body['resolve'])
  else
    body
  end
end
parse_response(res) click to toggle source
# File lib/old_plaid/connection.rb, line 72
def parse_response(res)
  # unfortunately, the JSON gem will raise an exception if the response is empty
  raise OldPlaid::ServerError.new(res.code, res.msg, '') if res.body.to_s.length < 2
  # we got a response from the server, so parse it
  body = JSON.parse(res.body)
  case res.code.delete('.').to_i
  when 200 then body
  when 201 then { msg: 'Requires further authentication', body: body}
  when 400
    raise OldPlaid::BadRequest.new(body['code'], body['message'], body['resolve'])
  when 401
    raise OldPlaid::Unauthorized.new(body['code'], body['message'], body['resolve'])
  when 402
    raise OldPlaid::RequestFailed.new(body['code'], body['message'], body['resolve'])
  when 404
    raise OldPlaid::NotFound.new(body['code'], body['message'], body['resolve'])
  else
    raise OldPlaid::ServerError.new(body['code'], body['message'], body['resolve'])
  end
end