class Nylas::RestfulModel

Attributes

raw_json[R]

Public Class Methods

collection_name() click to toggle source
# File lib/restful_model.rb, line 15
def self.collection_name
  "#{self.to_s.downcase}s".split('::').last
end
new(api, account_id = nil) click to toggle source
# File lib/restful_model.rb, line 19
def initialize(api, account_id = nil)
  raise StandardError.new unless api.class <= Nylas::API
  @account_id = account_id
  @_api = api
end

Public Instance Methods

==(comparison_object) click to toggle source
# File lib/restful_model.rb, line 25
def ==(comparison_object)
  comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id)
end
as_json(options = {}) click to toggle source
# File lib/restful_model.rb, line 49
def as_json(options = {})
  hash = {}
  parameters.each do |getter|
    unless options[:except] && options[:except].include?(getter)
      value = send(getter)
      unless value.is_a?(RestfulModelCollection)
        value = value.as_json(options) if value.respond_to?(:as_json)
        hash[getter] = value
      end
    end
  end
  hash
end
destroy(params = {}) click to toggle source
# File lib/restful_model.rb, line 75
def destroy(params = {})
  ::RestClient.send('delete', self.url, :params => params) do |response, request, result|
    Nylas.interpret_response(result, response, {:raw_response => true})
  end
end
inflate(json) click to toggle source
# File lib/restful_model.rb, line 29
def inflate(json)
  @raw_json = json
  parameters.each do |property_name|
    send("#{property_name}=", json[property_name]) if json.has_key?(property_name)
  end
end
save!(params={}) click to toggle source
# File lib/restful_model.rb, line 36
def save!(params={})
  if id
    update('PUT', '', as_json(), params)
  else
    update('POST', '', as_json(), params)
  end
end
update(http_method, action, data = {}, params = {}) click to toggle source
# File lib/restful_model.rb, line 63
def update(http_method, action, data = {}, params = {})
  http_method = http_method.downcase

  ::RestClient.send(http_method, self.url(action), data.to_json, :content_type => :json, :params => params) do |response, request, result|
    unless http_method == 'delete'
      json = Nylas.interpret_response(result, response, :expected_class => Object)
      inflate(json)
    end
  end
  self
end
url(action = "") click to toggle source
# File lib/restful_model.rb, line 44
def url(action = "")
  action = "/#{action}" unless action.empty?
  @_api.url_for_path("/#{self.class.collection_name}/#{id}#{action}")
end