class HabiticaClient::Restful

Public Class Methods

new(client, attributes = {}) click to toggle source
Calls superclass method HabiticaClient::ApiBase::new
# File lib/habitica_client/restful.rb, line 31
def initialize(client, attributes = {})
  self.attributes = attributes
  super(client)
end

Public Instance Methods

attributes=(attributes) click to toggle source
# File lib/habitica_client/restful.rb, line 36
def attributes=(attributes)
  attributes.each { |k, v| send("#{k}=", v) }
end
delete() click to toggle source
# File lib/habitica_client/restful.rb, line 52
def delete
  return nil if new?

  response = client.class.delete(url)

  response.ok?
end
new?() click to toggle source
# File lib/habitica_client/restful.rb, line 40
def new?
  id.nil?
end
save() click to toggle source
# File lib/habitica_client/restful.rb, line 44
def save
  response = put || post

  self.attributes = self.class.remap(response)

  self
end
to_json() click to toggle source
# File lib/habitica_client/restful.rb, line 60
def to_json
  to_h.to_json
end

Private Instance Methods

post() click to toggle source
# File lib/habitica_client/restful.rb, line 84
def post
  return nil unless new?

  request(:post)
end
put() click to toggle source
# File lib/habitica_client/restful.rb, line 90
def put
  return nil if new?

  request(:put)
end
request(method) click to toggle source
# File lib/habitica_client/restful.rb, line 72
def request(method)
  response = client.class.send(method,
                               url,
                               body: to_json)

  unless response.response.code =~ /2\d{2}/
    raise ServerError, response['err']
  end

  response.parsed_response['data']
end
url() click to toggle source
# File lib/habitica_client/restful.rb, line 66
def url
  return "#{endpoint}/#{id}" unless new?

  endpoint
end