Class: Quandl::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/connection.rb

Class Method Summary collapse

Class Method Details

.execute_request(opts, &block) ⇒ Object



23
24
25
26
27
28
# File 'lib/quandl/connection.rb', line 23

def self.execute_request(opts, &block)
  RestClient::Request.execute(opts, &block)
rescue RestClient::ExceptionWithResponse => e
  handle_api_error(e.response) if e.response
  raise e
end

.general_error(rcode, rbody) ⇒ Object



36
37
38
39
# File 'lib/quandl/connection.rb', line 36

def self.general_error(rcode, rbody)
  QuandlError.new("Invalid response object from API: #{rbody.inspect} " \
               "(HTTP response code was #{rcode})", rcode, rbody)
end

.handle_api_error(resp) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/quandl/connection.rb', line 41

def self.handle_api_error(resp)
  error_body = parse(resp.body)
  code = error_body['quandl_error']['code']
  message = error_body['quandl_error']['message']
  code_letter = code.match(/QE([a-zA-Z])x/).captures.first

  case code_letter
  when 'L'
    fail LimitExceededError.new(message, resp.code, resp.body, error_body,
                                resp.headers, code)
  when 'M'
    fail InternalServerError.new(message, resp.code, resp.body, error_body,
                                 resp.headers, code)
  when 'A'
    fail AuthenticationError.new(message, resp.code, resp.body, error_body,
                                 resp.headers, code)
  when 'P'
    fail ForbiddenError.new(message, resp.code, resp.body, error_body,
                            resp.headers, code)
  when 'S'
    fail InvalidRequestError.new(message, resp.code, resp.body, error_body,
                                 resp.headers, code)
  when 'C'
    fail NotFoundError.new(message, resp.code, resp.body, error_body,
                           resp.headers, code)
  when 'X'
    fail ServiceUnavailableError.new(message, resp.code, resp.body,
                                     error_body, resp.headers, code)
  else
    fail QuandlError.new(message, resp.code, resp.body, error_body,
                         resp.headers, code)
  end
end

.parse(response) ⇒ Object



30
31
32
33
34
# File 'lib/quandl/connection.rb', line 30

def self.parse(response)
  ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(response))
rescue JSON::ParserError
  raise general_error(response.code, response.body)
end

.request(http_verb, url, options = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/quandl/connection.rb', line 3

def self.request(http_verb, url, options = {}, &block)
  params = options.delete(:params) || {}
  headers = options.delete(:headers) || {}
  accept_value = 'application/json'
  accept_value += ", application/vnd.quandl+json;version=#{ApiConfig.api_version}" if ApiConfig.api_version
  headers = { accept: accept_value, request_source: 'ruby', request_source_version: Quandl::VERSION }.merge(headers)
  headers = { x_api_token: ApiConfig.api_key }.merge(headers) if ApiConfig.api_key

  request_url = ApiConfig.api_base + '/' + url
  request_url = request_url + '?' + params.to_query if params.present?

  request_opts = { url: request_url, headers: headers, method: http_verb }
  response = execute_request(request_opts, &block)

  return response if block_given?

  response_data = Quandl::Util.convert_to_dates(parse(response))
  [response, response_data]
end