class Rottendesk::Model
Attributes
app[RW]
Public Class Methods
new(attrs = {})
click to toggle source
# File lib/rottendesk/models/model.rb, line 7 def initialize(attrs = {}) attrs.each do |k,v| self.send("#{k}=", v) if respond_to? "#{k}=" end end
Protected Class Methods
create(attrs = {})
click to toggle source
# File lib/rottendesk/models/model.rb, line 75 def create(attrs = {}) model = self.new(attrs) model.save end
endpoint(endpoint)
click to toggle source
# File lib/rottendesk/models/model.rb, line 110 def endpoint(endpoint) @endpoint ||= endpoint end
field(field, options = {})
click to toggle source
# File lib/rottendesk/models/model.rb, line 98 def field(field, options = {}) @_fields ||= [] @_fields << field attr_reader field attr_writer field unless options[:readonly] end
fields()
click to toggle source
# File lib/rottendesk/models/model.rb, line 106 def fields @_fields end
find(id)
click to toggle source
# File lib/rottendesk/models/model.rb, line 80 def find(id) parse(app.client.get("#{@endpoint}/#{id}")) end
get_endpoint()
click to toggle source
# File lib/rottendesk/models/model.rb, line 118 def get_endpoint @endpoint end
get_json_key()
click to toggle source
# File lib/rottendesk/models/model.rb, line 122 def get_json_key @json_key end
json_key(key)
click to toggle source
# File lib/rottendesk/models/model.rb, line 114 def json_key(key) @json_key = key end
parse(json)
click to toggle source
# File lib/rottendesk/models/model.rb, line 88 def parse(json) remote_fields = JSON.parse(json) if remote_fields.is_a? Array remote_fields.map{|f| self.new.from_json(f)} else self.new.from_json(remote_fields) end end
where(filters = {})
click to toggle source
# File lib/rottendesk/models/model.rb, line 84 def where(filters = {}) parse(app.client.get(@endpoint, params: filters)) end
Public Instance Methods
errors()
click to toggle source
# File lib/rottendesk/models/model.rb, line 28 def errors @errors end
fields()
click to toggle source
# File lib/rottendesk/models/model.rb, line 45 def fields self.class.fields end
from_json(json)
click to toggle source
# File lib/rottendesk/models/model.rb, line 36 def from_json(json) json = JSON.parse(json) if json.is_a? String json = json[json_key] if json[json_key] fields.each do |f| self.instance_variable_set("@#{f}", json[f.to_s]) end self end
persisted?()
click to toggle source
# File lib/rottendesk/models/model.rb, line 13 def persisted? !self.id.nil? end
save()
click to toggle source
# File lib/rottendesk/models/model.rb, line 17 def save persisted? ? update : create_new end
to_h()
click to toggle source
# File lib/rottendesk/models/model.rb, line 24 def to_h fields.reduce({}){|h, f| h[f] = self.send(f); h}.compact end
update()
click to toggle source
# File lib/rottendesk/models/model.rb, line 21 def update end
valid?()
click to toggle source
# File lib/rottendesk/models/model.rb, line 32 def valid? errors.nil? || errors.empty? end
Protected Instance Methods
client()
click to toggle source
# File lib/rottendesk/models/model.rb, line 67 def client self.class.app.client end
create_new()
click to toggle source
# File lib/rottendesk/models/model.rb, line 50 def create_new json = {json_key => to_h}.to_json response = client.post(endpoint, json) self.from_json response rescue RestClient::UnprocessableEntity => ex @errors = Hash[JSON.parse(ex.response)] self end
endpoint()
click to toggle source
# File lib/rottendesk/models/model.rb, line 59 def endpoint self.class.get_endpoint end
json_key()
click to toggle source
# File lib/rottendesk/models/model.rb, line 63 def json_key self.class.get_json_key end