class Loco::RESTAdapter

Constants

JSON_OPTIONS

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/motion-loco/rest_adapter.rb, line 9
def initialize(*args)
  self.url = args.first if args && args.first
  super
end

Public Instance Methods

create_record(record, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 14
def create_record(record, &block)
  type = record.class
  BW::HTTP.post("#{self.url}/#{type.to_s.underscore.pluralize}.json", { payload: record.serialize }) do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      load(type, record, data)
      block.call(record) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  record
end
delete_record(record, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 30
def delete_record(record, &block)
  type = record.class
  BW::HTTP.delete("#{self.url}/#{type.to_s.underscore.pluralize}/#{record.id}.json") do |response|
    if response.ok?
      block.call(record) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  record
end
find(record, id, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 43
def find(record, id, &block)
  type = record.class
  BW::HTTP.get("#{self.url}/#{type.to_s.underscore.pluralize}/#{id}.json") do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      load(type, record, data)
      block.call(record) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  record
end
find_all(type, records, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 59
def find_all(type, records, &block)
  BW::HTTP.get("#{self.url}/#{type.to_s.underscore.pluralize}.json") do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      load(type, records, data)
      block.call(records) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  records
end
find_many(type, records, ids, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 74
def find_many(type, records, ids, &block)
  BW::HTTP.get("#{self.url}/#{type.to_s.underscore.pluralize}.json", { payload: { ids: ids } }) do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      load(type, records, data)
      block.call(records) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  records
end
find_query(type, records, query, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 89
def find_query(type, records, query, &block)
  BW::HTTP.get("#{self.url}/#{type.to_s.underscore.pluralize}.json", { payload: { query: query } }) do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      load(type, records, data)
      block.call(records) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  records
end
serialize(record, options={}) click to toggle source
Calls superclass method Loco::Adapter#serialize
# File lib/motion-loco/rest_adapter.rb, line 117
def serialize(record, options={})
  json = {}
  record.class.get_class_relationships.each do |relationship|
    if relationship[:belongs_to]
      key = "#{relationship[:belongs_to]}_id".to_sym
    elsif relationship[:has_many]
      key = "#{relationship[:has_many].to_s.singularize}_ids".to_sym
    end
    value = record.valueForKey(key)
    json[key] = value if value
  end
  super(record, options, json)
end
update_record(record, &block) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 104
def update_record(record, &block)
  type = record.class
  BW::HTTP.put("#{self.url}/#{type.to_s.underscore.pluralize}/#{record.id}.json", { payload: record.serialize }) do |response|
    if response.ok?
      block.call(record) if block.is_a? Proc
    else
      Loco.debug("Responded with #{response.status_code}")
      Loco.debug(response.error_message)
    end
  end
  record
end
url() click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 131
def url
  unless @url.nil?
    @url
  else
    raise ArgumentError, "Loco::RESTAdapter needs a base URL when using in a model. Ex. `adapter 'Loco::RESTAdapter', 'http://mydomain.com'`"
  end
end
url=(url) click to toggle source
# File lib/motion-loco/rest_adapter.rb, line 139
def url=(url)
  url.slice!(-1) if url.slice(-1) == '/'
  @url = url
end