Class: Loco::RESTAdapter

Inherits:
Adapter show all
Defined in:
lib/motion-loco/rest_adapter.rb

Defined Under Namespace

Classes: RecordNotFound

Constant Summary collapse

JSON_OPTIONS =
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves | NSJSONReadingAllowFragments

Instance Method Summary collapse

Methods inherited from Adapter

#save_record

Constructor Details

#initialize(*args) ⇒ RESTAdapter

Returns a new instance of RESTAdapter.



9
10
11
12
# File 'lib/motion-loco/rest_adapter.rb', line 9

def initialize(*args)
  self.url = args.first if args && args.first
  super
end

Instance Method Details

#create_record(record, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/motion-loco/rest_adapter.rb', line 14

def create_record(record, &block)
  BW::HTTP.post("#{self.url}/#{record.class.to_s.underscore.pluralize}.json", { payload: record.serialize(root: true) }) do |response|
    if response.ok?
      error = Pointer.new(:id)
      data = NSJSONSerialization.JSONObjectWithData(response.body, options:JSON_OPTIONS, error:error)
      record.load(data[record.class.to_s.underscore][:id], data[record.class.to_s.underscore])
      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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/motion-loco/rest_adapter.rb', line 29

def delete_record(record, &block)
  BW::HTTP.delete("#{self.url}/#{record.class.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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/motion-loco/rest_adapter.rb', line 41

def find(record, id, &block)
  BW::HTTP.get("#{self.url}/#{record.class.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)
      record.load(id, data[record.class.to_s.underscore])
      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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/motion-loco/rest_adapter.rb', line 56

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)
      records.load(type, data[type.to_s.underscore.pluralize])
      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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/motion-loco/rest_adapter.rb', line 71

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)
      records.load(type, data[type.to_s.underscore.pluralize])
      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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/motion-loco/rest_adapter.rb', line 86

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)
      records.load(type, data[type.to_s.underscore.pluralize])
      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

#update_record(record, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/motion-loco/rest_adapter.rb', line 101

def update_record(record, &block)
  BW::HTTP.put("#{self.url}/#{record.class.to_s.underscore.pluralize}/#{record.id}.json", { payload: record.serialize(root: true) }) 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

#urlObject



113
114
115
116
117
118
119
# File 'lib/motion-loco/rest_adapter.rb', line 113

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) ⇒ Object



121
122
123
124
# File 'lib/motion-loco/rest_adapter.rb', line 121

def url=(url)
  url.slice!(-1) if url.slice(-1) == '/'
  @url = url
end