Class: Nylas::RestfulModel

Inherits:
Object
  • Object
show all
Extended by:
TimeAttrAccessor
Includes:
Parameters
Defined in:
lib/restful_model.rb

Direct Known Subclasses

APIAccount, Account, Calendar, Contact, Event, File, Folder, Message, Thread

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeAttrAccessor

time_attr_accessor

Methods included from Parameters

included, #parameters

Constructor Details

#initialize(api, account_id = nil) ⇒ RestfulModel

Returns a new instance of RestfulModel.

Raises:

  • (StandardError)


19
20
21
22
23
# File 'lib/restful_model.rb', line 19

def initialize(api,  = nil)
  raise StandardError.new unless api.class <= Nylas::API
  @account_id = 
  @_api = api
end

Instance Attribute Details

#raw_jsonObject (readonly)

Returns the value of attribute raw_json.



13
14
15
# File 'lib/restful_model.rb', line 13

def raw_json
  @raw_json
end

Class Method Details

.collection_nameObject



15
16
17
# File 'lib/restful_model.rb', line 15

def self.collection_name
  "#{self.to_s.downcase}s".split('::').last
end

Instance Method Details

#==(comparison_object) ⇒ Object



25
26
27
# 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 = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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 = {}) ⇒ Object



75
76
77
78
79
# 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) ⇒ Object



29
30
31
32
33
34
# 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 = {}) ⇒ Object



36
37
38
39
40
41
42
# 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 = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# 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 = "") ⇒ Object



44
45
46
47
# 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