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/', 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



166
167
168
169
170
# File 'lib/belvo/http.rb', line 166

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:



104
105
106
107
108
109
110
111
112
# File 'lib/belvo/http.rb', line 104

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
# File 'lib/belvo/http.rb', line 92

def list(path, params: nil)
  params = {} if params.nil?
  response = get(path, params: params)
  response.body['results']
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:



156
157
158
159
160
# File 'lib/belvo/http.rb', line 156

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:



132
133
134
135
136
# File 'lib/belvo/http.rb', line 132

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:



144
145
146
147
148
149
# File 'lib/belvo/http.rb', line 144

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

#token(path, id, data) ⇒ Hash

Perform a POST request to an API endpoint

Parameters:

  • path (String)

    API endpoint

  • id (String)

    Resource UUID

  • data (object)

    JSON parseable object

Returns:

  • (Hash)

    Response details

Raises:



120
121
122
123
124
125
# File 'lib/belvo/http.rb', line 120

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