Class: NameDrop::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/name_drop/resources/base.rb

Direct Known Subclasses

Alert, Mention, Share

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/name_drop/resources/base.rb', line 11

def initialize(client, attributes = {})
  @client = client
  @attributes = attributes
  @errors = []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/name_drop/resources/base.rb', line 8

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/name_drop/resources/base.rb', line 9

def errors
  @errors
end

Class Method Details

.all(client, params = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/name_drop/resources/base.rb', line 17

def self.all(client, params = {})
  response = client.get(endpoint(params))
  response[response_key.pluralize].map do |attributes|
    new(client, attributes)
  end
end

.build(client, attributes = {}) ⇒ Object



33
34
35
# File 'lib/name_drop/resources/base.rb', line 33

def self.build(client, attributes = {})
  new(client, attributes)
end

.find(client, id) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/name_drop/resources/base.rb', line 24

def self.find(client, id)
  response = client.get("#{endpoint}/#{id}")
  if response[response_key].present?
    new(client, response[response_key])
  else
    response
  end
end

.response_keyObject



53
54
55
# File 'lib/name_drop/resources/base.rb', line 53

def self.response_key
  name.demodulize.downcase
end

Instance Method Details

#destroy(params = {}) ⇒ Object



49
50
51
# File 'lib/name_drop/resources/base.rb', line 49

def destroy(params = {})
  client.delete("#{endpoint(params)}/#{attributes['id']}")
end

#saveObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/name_drop/resources/base.rb', line 37

def save
  path = new_record? ? endpoint : "#{endpoint}/#{attributes['id']}"
  response = client.send(persistence_action, path, attributes)
  if response[response_key].present?
    self.attributes = response[response_key]
    true
  else
    @errors = response
    false
  end
end