Class: QuestradeApi::REST::Base

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/questrade_api/rest/base.rb

Overview

Author:

Constant Summary collapse

BASE_ENDPOINT =
'/v1'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization) ⇒ Base

Initialize an instance of QuestradeApi::REST::Base

Parameters:

  • authorization (Object)

    to access API. Any object that responds to url and access_token.



19
20
21
22
23
24
25
26
# File 'lib/questrade_api/rest/base.rb', line 19

def initialize(authorization)
  @authorization = authorization

  # TODO: Review this later
  @connection =
    self.class.connection(url: url,
                          access_token: @authorization.access_token)
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def 
  @account_id
end

#authorizationObject

Returns the value of attribute authorization.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def authorization
  @authorization
end

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def connection
  @connection
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def data
  @data
end

#endpointObject

Returns the value of attribute endpoint.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def endpoint
  @endpoint
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def id
  @id
end

#raw_bodyObject

Returns the value of attribute raw_body.



12
13
14
# File 'lib/questrade_api/rest/base.rb', line 12

def raw_body
  @raw_body
end

Class Method Details

.connection(params = {}) ⇒ Faraday

Builds a new Faraday connection to access endpoint.

Parameters:

  • params (Hash) (defaults to: {})

    for connection.

Options Hash (params):

  • :url (String)

    of endpoint.

  • :access_token (String)

    to call endpoint.

Returns:

  • (Faraday)

    Object with attributes set up to call proper endpoint.



40
41
42
43
44
45
46
47
48
# File 'lib/questrade_api/rest/base.rb', line 40

def self.connection(params = {})
  Faraday.new(params[:url]) do |faraday|
 #         faraday.response :logger
    faraday.adapter Faraday.default_adapter
    faraday.headers['Content-Type'] = 'application/json'
    faraday.headers['User-Agent'] = "QuestradeApi v#{QuestradeApi::VERSION}"
    faraday.headers['Authorization'] = "Bearer #{params[:access_token]}"
  end
end

.fetch(params = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/questrade_api/rest/base.rb', line 76

def fetch(params = {})
  connection = connection(params)

  connection.get do |req|
    req.path = params[:endpoint]

    params.fetch(:params, []).each do |key, value|
      req.params[key] = value
    end
  end
end

.post(params = {}) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/questrade_api/rest/base.rb', line 88

def post(params = {})
  connection = connection(params)

  connection.post do |req|
    req.path = params[:endpoint]
    req.body = params[:body] if params[:body]
  end
end

Instance Method Details

#build_attributes(response) ⇒ Object (protected)



57
58
59
# File 'lib/questrade_api/rest/base.rb', line 57

def build_attributes(response)
  @raw_body = JSON.parse(response.body)
end

#build_data(data) ⇒ Object (protected)



52
53
54
55
# File 'lib/questrade_api/rest/base.rb', line 52

def build_data(data)
  hash = hash_to_snakecase(data)
  @data = OpenStruct.new(hash)
end

#fetch(params = {}) ⇒ Object (protected)



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/questrade_api/rest/base.rb', line 61

def fetch(params = {})
  response = @connection.get do |req|
    req.path = params[:endpoint] || self.class.endpoint

    params.fetch(:params, []).each do |key, value|
      req.params[key] = value
    end
  end

  build_attributes(response) if response.status == 200

  response
end

#urlString

Returns:

  • (String)


29
30
31
# File 'lib/questrade_api/rest/base.rb', line 29

def url
  authorization.url
end