Class: FidorApi::Resource
- Inherits:
-
Object
- Object
- FidorApi::Resource
- Includes:
- ActiveModel::Model
- Defined in:
- lib/fidor_api/resource.rb
Direct Known Subclasses
Account, Card, CardLimits, Customer, Message, Preauth, Transaction, Transfer::Base, User
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .model_name ⇒ Object
- .request(method: :get, access_token: nil, endpoint: nil, query_params: {}, body: {}, htauth: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
- #persisted? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
22 23 24 |
# File 'lib/fidor_api/resource.rb', line 22 def initialize(attributes = {}) set_attributes(attributes) end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
6 7 8 |
# File 'lib/fidor_api/resource.rb', line 6 def client @client end |
Class Method Details
.model_name ⇒ Object
48 49 50 |
# File 'lib/fidor_api/resource.rb', line 48 def self.model_name ActiveModel::Name.new(self, nil, self.name.sub("FidorApi::", "")) end |
.request(method: :get, access_token: nil, endpoint: nil, query_params: {}, body: {}, htauth: false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/fidor_api/resource.rb', line 26 def self.request(method: :get, access_token: nil, endpoint: nil, query_params: {}, body: {}, htauth: false) response = connection(htauth: htauth).public_send(method, endpoint) do |request| request.params = query_params request.headers = {} request.headers["Authorization"] = "Bearer #{access_token}" if access_token request.headers["Accept"] = "application/vnd.fidor.de; version=1,text/json" request.headers["Content-Type"] = "application/json" request.body = body.to_json unless body.empty? end Response.new(headers: response.headers, body: response.body) rescue Faraday::Error::ClientError => e case e.response[:status] when 401 raise UnauthorizedTokenError when 422 body = JSON.parse(e.response[:body]) raise ValidationError.new(body["message"], body["errors"]) else raise ClientError end end |
Instance Method Details
#persisted? ⇒ Boolean
52 53 54 |
# File 'lib/fidor_api/resource.rb', line 52 def persisted? id.present? end |