Class: Belvo::APISession

Inherits:
Object
  • Object
show all
Defined in:
lib/belvo/http.rb

Overview

Describes a Belvo API session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Faraday::Connection

Parameters:

  • url (String)

    Belvo API host url



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/belvo/http.rb', line 19

def initialize(url)
  @url = format('%<url>s/api/', url: url)
  @session = Faraday::Connection.new(url: @url) do |faraday|
    faraday.adapter :typhoeus
    faraday.response :json
    faraday.headers = {
      'Content-Type' => 'application/json',
      'User-Agent' => format(
        'belvo-ruby (%<version>s)',
        version: Belvo::VERSION
      )
    }
  end
end

Instance Attribute Details

#key_idObject (readonly)

Returns the value of attribute key_id.



14
15
16
# File 'lib/belvo/http.rb', line 14

def key_id
  @key_id
end

#key_passwordObject (readonly)

Returns the value of attribute key_password.



15
16
17
# File 'lib/belvo/http.rb', line 15

def key_password
  @key_password
end

Instance Method Details

#delete(path, id) ⇒ Boolean

Delete existing resource

Parameters:

  • path (String)

    API endpoint

  • id (String)

    Resource UUID

Returns:

  • (Boolean)

    true if resource is successfully deleted else false



162
163
164
165
166
# File 'lib/belvo/http.rb', line 162

def delete(path, id)
  resource_path = format('%<path>s%<id>s/', path: path, id: id)
  response = @session.delete(resource_path)
  response.success?
end

#detail(path, id, params: nil) ⇒ Hash

Show specific resource details

Parameters:

  • path (String)

    API endpoint

  • id (String)

    Resource UUID

  • params (Hash) (defaults to: nil)

    Params to be send as querystring

Returns:

  • (Hash)

    Resource details

Raises:



113
114
115
116
117
118
119
120
121
# File 'lib/belvo/http.rb', line 113

def detail(path, id, params: nil)
  params = {} if params.nil?

  resource_path = format('%<path>s%<id>s/', path: path, id: id)
  response = get(resource_path, params)

  raise_for_status response
  response.body
end

#list(path, params: nil) {|Hash| ... } ⇒ Object

List results from an API endpoint. It will yield all results following pagination's next page, if any.

Parameters:

  • path (String)

    API endpoint

  • params (Hash) (defaults to: nil)

    Params to be send as querystring

Yields:

  • (Hash)

    Each result

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/belvo/http.rb', line 92

def list(path, params: nil)
  params = {} if params.nil?
  loop do
    response = get(path, params: params)
    response.body['results'].each do |item|
      yield item if block_given?
    end

    break unless response.body['next']

    path = response.body['next']
    params = nil
  end
end

#login(secret_key_id, secret_key_password) ⇒ Boolean

Authenticate to Belvo API using the given credentials.

Parameters:

  • secret_key_id (String)
  • secret_key_password (String)

Returns:

  • (Boolean)

    True if credentials are valid to authenticate to Belvo API else False.



79
80
81
82
83
84
# File 'lib/belvo/http.rb', line 79

def (secret_key_id, secret_key_password)
  @key_id = secret_key_id
  @key_password = secret_key_password

  authenticate
end

#patch(path, data) ⇒ Hash

Perform a PATCH request to an API endpoint

Parameters:

  • path (String)

    API endpoint

  • data (object)

    JSON parseable object

Returns:

  • (Hash)

    Response details

Raises:



152
153
154
155
156
# File 'lib/belvo/http.rb', line 152

def patch(path, data)
  response = @session.patch(path, data.to_json)
  raise_for_status response
  response.body
end

#post(path, data) ⇒ Hash

Perform a POST request to an API endpoint

Parameters:

  • path (String)

    API endpoint

  • data (object)

    JSON parseable object

Returns:

  • (Hash)

    Response details

Raises:



128
129
130
131
132
# File 'lib/belvo/http.rb', line 128

def post(path, data)
  response = @session.post(path, data.to_json)
  raise_for_status response
  response.body
end

#put(path, id, data) ⇒ Hash

Perform a PUT request to an specific resource

Parameters:

  • path (String)

    API endpoint

  • id (String)

    Resource UUID

  • data (object)

    JSON parseable object

Returns:

  • (Hash)

    Response details

Raises:



140
141
142
143
144
145
# File 'lib/belvo/http.rb', line 140

def put(path, id, data)
  url = format('%<path>s%<id>s/', path: path, id: id)
  response = @session.put(url, data.to_json)
  raise_for_status response
  response.body
end