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

get_transforms, register_transform

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



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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 = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# 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

#urlObject



131
132
133
134
135
136
137
# 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) ⇒ Object



139
140
141
142
# File 'lib/motion-loco/rest_adapter.rb', line 139

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