Class: Belvo::APISession
- Inherits:
-
Object
- Object
- Belvo::APISession
- Defined in:
- lib/belvo/http.rb
Overview
Describes a Belvo API session
Instance Attribute Summary collapse
-
#key_id ⇒ Object
readonly
Returns the value of attribute key_id.
-
#key_password ⇒ Object
readonly
Returns the value of attribute key_password.
Instance Method Summary collapse
-
#delete(path, id) ⇒ Boolean
Delete existing resource.
-
#detail(path, id, params: nil) ⇒ Hash
Show specific resource details.
- #initialize(url) ⇒ Faraday::Connection constructor
-
#list(path, params: nil) {|Hash| ... } ⇒ Object
List results from an API endpoint.
-
#login(secret_key_id, secret_key_password) ⇒ Boolean
Authenticate to Belvo API using the given credentials.
-
#patch(path, data) ⇒ Hash
Perform a PATCH request to an API endpoint.
-
#post(path, data) ⇒ Hash
Perform a POST request to an API endpoint.
-
#put(path, id, data) ⇒ Hash
Perform a PUT request to an specific resource.
-
#token(path, id, data) ⇒ Hash
Perform a POST request to an API endpoint.
Constructor Details
#initialize(url) ⇒ Faraday::Connection
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_id ⇒ Object (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_password ⇒ Object (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
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
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.
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.
79 80 81 82 83 84 |
# File 'lib/belvo/http.rb', line 79 def login(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
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
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
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
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 |