Class: Paid::APIMethod
- Inherits:
-
Object
- Object
- Paid::APIMethod
- Defined in:
- lib/paid/api_method.rb
Instance Attribute Summary collapse
-
#api_base ⇒ Object
Returns the value of attribute api_base.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#error ⇒ Object
Returns the value of attribute error.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
-
#response_code ⇒ Object
Returns the value of attribute response_code.
Instance Method Summary collapse
- #compose_error(error) ⇒ Object
-
#error_with_response(error) ⇒ Object
Handle a few common cases.
- #execute ⇒ Object
-
#initialize(method, path, params, headers, object, api_key = nil, api_base = nil) ⇒ APIMethod
constructor
A new instance of APIMethod.
- #response_json ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(method, path, params, headers, object, api_key = nil, api_base = nil) ⇒ APIMethod
Returns a new instance of APIMethod.
16 17 18 19 20 21 22 23 24 |
# File 'lib/paid/api_method.rb', line 16 def initialize(method, path, params, headers, object, api_key=nil, api_base=nil) @api_key = api_key || Paid.api_key @api_base = api_base || Paid.api_base @method = method.to_sym @path = PathBuilder.build(path, object, params) @params = ParamsBuilder.build(params) @headers = HeadersBuilder.build(headers, @api_key, Paid.auth_header) end |
Instance Attribute Details
#api_base ⇒ Object
Returns the value of attribute api_base.
14 15 16 |
# File 'lib/paid/api_method.rb', line 14 def api_base @api_base end |
#api_key ⇒ Object
Returns the value of attribute api_key.
13 14 15 |
# File 'lib/paid/api_method.rb', line 13 def api_key @api_key end |
#error ⇒ Object
Returns the value of attribute error.
11 12 13 |
# File 'lib/paid/api_method.rb', line 11 def error @error end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/paid/api_method.rb', line 7 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
5 6 7 |
# File 'lib/paid/api_method.rb', line 5 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/paid/api_method.rb', line 6 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/paid/api_method.rb', line 4 def path @path end |
#response_body ⇒ Object
Returns the value of attribute response_body.
9 10 11 |
# File 'lib/paid/api_method.rb', line 9 def response_body @response_body end |
#response_code ⇒ Object
Returns the value of attribute response_code.
10 11 12 |
# File 'lib/paid/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/paid/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 #{Paid.api_base}' "\ "from the command line." else msg = "An unexpected error occured. If this problem persists let us " \ "know at #{Paid.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 96 97 98 99 100 101 |
# File 'lib/paid/api_method.rb', line 86 def error_with_response(error) = begin response_json[:error][:message] rescue nil end ||= error. case @response_code when 401 return AuthenticationError.new(, self) else return APIError.new(, self) end end |
#execute ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/paid/api_method.rb', line 26 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 response_json end |
#response_json ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/paid/api_method.rb', line 45 def response_json begin json = Util.symbolize_keys(JSON.parse(@response_body)) rescue JSON::ParserError if @response_body.is_a?(String) && @response_body.strip.empty? {} else @error = APIError.new("Unable to parse the server response as JSON.", self) raise @error end end end |
#url ⇒ Object
41 42 43 |
# File 'lib/paid/api_method.rb', line 41 def url "#{api_base}#{@path}" end |