Class: FidorApi::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/fidor_api/resource.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#clientObject

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_nameObject



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/fidor_api/resource.rb', line 52

def persisted?
  id.present?
end