Module: TDAmeritrade::Util
- Included in:
- Authentication, Operations::BaseOperation
- Defined in:
- lib/tdameritrade/util.rb
Class Method Summary collapse
- .handle_api_error(response) ⇒ Object
- .parse_json_response(response) ⇒ Object (also: parse_api_response)
- .response_success?(response) ⇒ Boolean
Class Method Details
.handle_api_error(response) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tdameritrade/util.rb', line 7 def handle_api_error(response) # "Individual App's transactions per seconds restriction, please update to commercial apps for unrestricted tps" if response.code == 429 raise TDAmeritrade::Error::RateLimitError.new(response.body) elsif response.code == 401 raise TDAmeritrade::Error::NotAuthorizedError.new(response.body) end = JSON.parse(response.body)['error'] raise TDAmeritrade::Error::TDAmeritradeError.new("#{response.code}: #{}") rescue JSON::ParserError raise TDAmeritrade::Error::TDAmeritradeError.new( "Unable to parse error response from TD Ameritrade API: #{response.body}" ) end |
.parse_json_response(response) ⇒ Object Also known as: parse_api_response
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tdameritrade/util.rb', line 23 def parse_json_response(response) handle_api_error(response) unless response_success?(response) stripped_text = response.body.strip sanitized_text = stripped_text[0] == "\\" ? stripped_text[1..-1] : stripped_text json = JSON.parse(sanitized_text) case json when Array json.map { |item| Hashie::Mash.new(item) } # assuming that we're only dealing with arrays of hashes when Hash Hashie::Mash.new(json) else json end rescue JSON::ParserError raise TDAmeritrade::Error::TDAmeritradeError.new( "Unable to parse response from TD Ameritrade API: #{sanitized_text}" ) end |
.response_success?(response) ⇒ Boolean
46 47 48 |
# File 'lib/tdameritrade/util.rb', line 46 def response_success?(response) response.code.to_s =~ /^2\d\d/ end |