class TwelvedataRuby::Response
Constants
- BODY_MAX_BYTESIZE
- CONTENT_TYPE_HANDLERS
- CSV_COL_SEP
- HTTP_STATUSES
Attributes
body[R]
body_bytesize[R]
headers[R]
http_response[R]
request[RW]
Public Class Methods
http_status_codes()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 33 def http_status_codes @http_status_codes ||= HTTP_STATUSES.values.map(&:to_a).flatten end
new(http_response:, request:, headers: nil, body: nil)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 40 def initialize(http_response:, request:, headers: nil, body: nil) self.http_response = http_response end
resolve(http_response, request)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 16 def resolve(http_response, request) if http_status_codes.member?(http_response.status) new(http_response: http_response, request: request) else resolve_error(http_response, request) end end
resolve_error(http_response, request)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 24 def resolve_error(http_response, request) error_attribs = if http_response.respond_to?(:error) && http_response.error {message: http_response.error.message, code: http_response.error.class.name} else {message: http_response.body.to_s, code: http_response.status} end TwelvedataRuby::ResponseError.new(json: (error_attribs || {}), request: request) end
Public Instance Methods
attachment_filename()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 44 def attachment_filename return nil unless headers["content-disposition"] @attachment_filename ||= headers["content-disposition"].split("filename=").last.delete("\"") end
body_parser()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 50 def body_parser CONTENT_TYPE_HANDLERS[content_type][:parser] end
content_type()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 54 def content_type @content_type ||= headers["content-type"].match(%r{^.+/([a-z]+).*$})&.send(:[], 1)&.to_sym end
csv_dumper()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 62 def csv_dumper parsed_body.is_a?(CSV::Table) ? parsed_body.to_csv(col_sep: CSV_COL_SEP) : nil end
csv_parser(io)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 58 def csv_parser(io) CSV.parse(io, headers: true, col_sep: CSV_COL_SEP) end
dumped_parsed_body()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 66 def dumped_parsed_body @dumped_parsed_body ||= parsed_body.respond_to?(parsed_body_dumper) ? parsed_body.send(parsed_body_dumper) : send(parsed_body_dumper) end
error()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 71 def error klass_name = ResponseError.error_code_klass(status_code, success_http_status? ? :api : :http) return unless klass_name TwelvedataRuby.const_get(klass_name) .new(json: parsed_body, request: request, code: status_code, message: parsed_body) end
http_status_code()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 78 def http_status_code http_response&.status end
json_dumper()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 82 def json_dumper parsed_body.is_a?(Hash) ? JSON.dump(parsed_body) : nil end
json_parser(io)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 86 def json_parser(io) JSON.parse(io, symbolize_names: true) end
parsed_body()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 90 def parsed_body return @parsed_body if @parsed_body || http_response&.body.nil? || http_response&.body.closed? begin tmp_file = nil if body_bytesize < BODY_MAX_BYTESIZE @parsed_body = send(body_parser, http_response.body.to_s) else tmp_file = Tempfile.new http_response.body.copy_to(tmp_file) @parsed_body = send(body_parser, IO.read(tmp_file.path)) end ensure http_response.body.close tmp_file&.close tmp_file&.unlink end @parsed_body end
Also aliased as: body, parse_http_response_body
parsed_body_dumper()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 113 def parsed_body_dumper CONTENT_TYPE_HANDLERS[content_type][:dumper] end
plain_parser(io=nil)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 117 def plain_parser(io=nil) io.to_s || http_response.body.to_s end
status_code()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 121 def status_code @status_code ||= parsed_body.is_a?(Hash) ? parsed_body[:code] : http_status_code end
success_http_status?()
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 125 def success_http_status? @success_http_status ||= HTTP_STATUSES[:success].member?(http_status_code) || false end
to_disk_file(file_fullpath=attachment_filename)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 129 def to_disk_file(file_fullpath=attachment_filename) return nil unless file_fullpath begin file = File.open(file_fullpath, "w") file.puts dumped_parsed_body file ensure file&.close end end
Private Instance Methods
headers=(http_resp_headers)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 152 def headers=(http_resp_headers) @headers = http_response&.headers end
http_response=(http_resp)
click to toggle source
# File lib/twelvedata_ruby/response.rb, line 145 def http_response=(http_resp) @http_response = http_resp @body_bytesize = http_resp.body.bytesize self.headers = http_response.headers parse_http_response_body end