class NameDrop::Resources::Base

Attributes

attributes[RW]
client[R]
errors[R]

Public Class Methods

all(client, params = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/name_drop/resources/base.rb, line 33
def self.build(client, attributes = {})
  new(client, attributes)
end
find(client, id) click to toggle source
# 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
new(client, attributes = {}) click to toggle source
# File lib/name_drop/resources/base.rb, line 11
def initialize(client, attributes = {})
  @client = client
  @attributes = attributes
  @errors = []
end
response_key() click to toggle source
# File lib/name_drop/resources/base.rb, line 53
def self.response_key
  name.demodulize.downcase
end

Public Instance Methods

destroy(params = {}) click to toggle source
# File lib/name_drop/resources/base.rb, line 49
def destroy(params = {})
  client.delete("#{endpoint(params)}/#{attributes['id']}")
end
save() click to toggle source
# 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

Private Instance Methods

new_record?() click to toggle source
# File lib/name_drop/resources/base.rb, line 69
def new_record?
  !attributes['id']
end
persistence_action() click to toggle source
# File lib/name_drop/resources/base.rb, line 65
def persistence_action
  new_record? ? :post : :put
end
response_key() click to toggle source
# File lib/name_drop/resources/base.rb, line 61
def response_key
  self.class.response_key
end