Module: Xfers::APIOperations

Defined in:
lib/xfers/api_operations.rb

Class Method Summary collapse

Class Method Details

.create_auth_headers(connect_key) ⇒ Object



4
5
6
# File 'lib/xfers/api_operations.rb', line 4

def self.create_auth_headers(connect_key)
  connect_key.nil? ? {'X-XFERS-USER-API-KEY' => Xfers.get_api_key} : {'X-XFERS-APP-API-KEY' => connect_key}
end

.delete(resource_url, connect_key = nil, params = nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/xfers/api_operations.rb', line 65

def self.delete(resource_url, connect_key=nil, params=nil)
  RestClient::Request.execute(method: :delete, url: "#{Xfers.get_api_base}#{resource_url}", payload: params,
                                  headers: create_auth_headers(connect_key)) {
      |r, request, result, &block|
    handle_api_error(JSON.parse(r.body), r.code) if r.code != 200
    parse_and_symbolize(r.body)
  }
end

.get(params = {}, resource_url = nil, connect_key = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/xfers/api_operations.rb', line 38

def self.get(params={}, resource_url=nil, connect_key=nil)
  RestClient::Request.execute(method: :get, url: "#{Xfers.get_api_base}#{resource_url}",
                              payload: params, headers: create_auth_headers(connect_key)) {
      |r, request, result, &block|
    handle_api_error(JSON.parse(r.body), r.code) if r.code != 200
    parse_and_symbolize(r.body)
  }
end

.handle_api_error(msg, status_code) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/xfers/api_operations.rb', line 8

def self.handle_api_error(msg, status_code)
  case status_code
  when 400
    raise InvalidRequestError.new msg, status_code
  when 401
    raise AuthenticationError.new msg, status_code
  when 500
    raise InternalServerError.new msg, status_code
  else
    raise XfersError.new msg, status_code
  end
end

.parse_and_symbolize(json_string) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xfers/api_operations.rb', line 21

def self.parse_and_symbolize(json_string)
  parsed = JSON.parse(json_string)
  begin
    if parsed.kind_of?(Array)
      arr = []
      parsed.each { |item|
        arr.push Hash[item.map{ |k, v| [k.to_sym, v] }]
      }
      arr
    else
      Hash[parsed.map{ |k, v| [k.to_sym, v] }]
    end
  rescue NoMethodError
    parsed
  end
end

.post(params = {}, resource_url = nil, connect_key = nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/xfers/api_operations.rb', line 47

def self.post(params={}, resource_url=nil, connect_key=nil)
  RestClient::Request.execute(method: :post, url: "#{Xfers.get_api_base}#{resource_url}",
                                  payload: params, headers: create_auth_headers(connect_key)) {
      |r, request, result, &block|
    handle_api_error(JSON.parse(r.body), r.code) if r.code != 200
    parse_and_symbolize(r.body)
  }
end

.put(params = {}, resource_url = nil, connect_key = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/xfers/api_operations.rb', line 56

def self.put(params={}, resource_url=nil, connect_key=nil)
  RestClient::Request.execute(method: :put, url: "#{Xfers.get_api_base}#{resource_url}",
                                  payload: params, headers: create_auth_headers(connect_key)) {
      |r, request, result, &block|
    handle_api_error(JSON.parse(r.body), r.code) if r.code != 200
    parse_and_symbolize(r.body)
  }
end