Class: SynapsePay::APIMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay/apibits/api_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, params, headers, object) ⇒ APIMethod

Returns a new instance of APIMethod.



15
16
17
18
19
20
21
22
# File 'lib/synapse_pay/apibits/api_method.rb', line 15

def initialize(method, path, params, headers, object)
  @api_base = api_base || SynapsePay.api_base

  @method = method.to_sym
  @path = PathBuilder.build(path, object, params)
  @params = ParamsBuilder.build(params)
  @headers = HeadersBuilder.build(headers)
end

Instance Attribute Details

#api_baseObject

Returns the value of attribute api_base.



13
14
15
# File 'lib/synapse_pay/apibits/api_method.rb', line 13

def api_base
  @api_base
end

#errorObject

Returns the value of attribute error.



11
12
13
# File 'lib/synapse_pay/apibits/api_method.rb', line 11

def error
  @error
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/synapse_pay/apibits/api_method.rb', line 7

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/synapse_pay/apibits/api_method.rb', line 5

def method
  @method
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/synapse_pay/apibits/api_method.rb', line 6

def params
  @params
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/synapse_pay/apibits/api_method.rb', line 4

def path
  @path
end

#response_bodyObject

Returns the value of attribute response_body.



9
10
11
# File 'lib/synapse_pay/apibits/api_method.rb', line 9

def response_body
  @response_body
end

#response_codeObject

Returns the value of attribute response_code.



10
11
12
# File 'lib/synapse_pay/apibits/api_method.rb', line 10

def response_code
  @response_code
end

Instance Method Details

#compose_error(error) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/synapse_pay/apibits/api_method.rb', line 58

def compose_error(error)
  msg = "An error occured while making the API call."

  case error
  when RestClient::ExceptionWithResponse
    return error_with_response(error)

  when RestClient::RequestTimeout
    msg = "The request timed out while making the API call."

  when RestClient::ServerBrokeConnection
    msg = "The connection to the server broke before the request completed."

  when SocketError
    msg = "An unexpected error occured while trying to connect to " \
      "the API. You may be seeing this message because your DNS is " \
      "not working. To check, try running 'host #{SynapsePay.api_base}' "\
      "from the command line."

  else
    msg = "An unexpected error occured. If this problem persists let us " \
      "know at #{SynapsePay.support_email}."
  end

  return APIConnectionError.new(msg, self)
end

#error_with_response(error) ⇒ Object

Handle a few common cases.



86
87
88
89
90
91
92
93
94
95
# File 'lib/synapse_pay/apibits/api_method.rb', line 86

def error_with_response(error)
  case @response_code
  when 400, 404
    return APIError.new(@response_body || "Invalid request. Please check the URL and parameters.", self)
  when 401
    return AuthenticationError.new(@response_body || "Authentication failed.", self)
  else
    return APIError.new(@response_body || "An error occured while making the API call.", self)
  end
end

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/synapse_pay/apibits/api_method.rb', line 24

def execute
  begin
    response = Requester.request(method, url, params, headers)
    @response_body = response.body
    @response_code = response.code
  rescue StandardError => e
    @response_body = e.http_body if e.respond_to?(:http_body)
    @response_code = e.http_code if e.respond_to?(:http_code)
    @error = compose_error(e)
    raise @error
  end

  if(!response_json[:success])
    @error = APIError.new(response_json, self)
    raise @error
  end
  response_json
end

#response_jsonObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/synapse_pay/apibits/api_method.rb', line 47

def response_json
  return @json if @json
  begin
    @json = Util.symbolize_keys(JSON.parse(@response_body))
  rescue JSON::ParserError
    @error = APIError.new("Unable to parse the server response as JSON.", self)
    raise @error
  end
  @json
end

#urlObject



43
44
45
# File 'lib/synapse_pay/apibits/api_method.rb', line 43

def url
  "#{api_base}#{@path}"
end