Class: GeoAPI::Entity
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from GeoObject
api_key_from_parameters, delete, entity_type, get, post, search
Constructor Details
#initialize(attrs) ⇒ Entity
162
163
164
165
|
# File 'lib/geoapi/entity.rb', line 162
def initialize(attrs)
super attrs
self.setup(attrs) unless attrs.blank?
end
|
Instance Attribute Details
#entity_type ⇒ Object
Returns the value of attribute entity_type.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def entity_type
@entity_type
end
|
#errors ⇒ Object
Returns the value of attribute errors.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def errors
@errors
end
|
#geom ⇒ Object
Also known as:
geometry
Returns the value of attribute geom.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def geom
@geom
end
|
#guid ⇒ Object
Returns the value of attribute guid.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def guid
@guid
end
|
#id ⇒ Object
Returns the value of attribute id.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def id
@id
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def name
@name
end
|
#raw_json ⇒ Object
Returns the value of attribute raw_json.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def raw_json
@raw_json
end
|
#shorturl ⇒ Object
Returns the value of attribute shorturl.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def shorturl
@shorturl
end
|
#url ⇒ Object
Returns the value of attribute url.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def url
@url
end
|
#userviews ⇒ Object
Returns the value of attribute userviews.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def userviews
@userviews
end
|
#views ⇒ Object
Returns the value of attribute views.
4
5
6
|
# File 'lib/geoapi/entity.rb', line 4
def views
@views
end
|
Class Method Details
.create(params) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/geoapi/entity.rb', line 24
def self.create(params)
puts "GEOAPI::Entity.create #{params.to_json}"
raise ArgumentError, "A name is required (pass :name in parameters)" unless params.has_key?(:name)
raise ArgumentError, "A geometry is required (pass :geom in parameters)" unless params.has_key?(:geom)
api_key = self.api_key_from_parameters(params)
raise ArgumentError, "An API Key is required" if api_key.blank?
id = params[:id] || UUIDTools::UUID.timestamp_create().to_s
post_url = "/e"
post_url = "/e/user-#{api_key}-#{id}?apikey=#{api_key}" unless id.blank?
post_url = "/e/#{params[:guid]}?apikey=#{api_key}" unless params[:guid].blank?
puts post_url
params.delete(:id) if params.has_key?(:id)
params.delete(:guid) if params.has_key?(:guid)
begin
results = Entity.post(post_url, {:body=> params.to_json})
rescue
raise BadRequest, "There was a problem communicating with the API"
end
raise BadRequest, results['error'] unless results['error'].blank?
guid = results['query']['params']['guid']
Entity.new(results['result'].merge({'guid'=>guid, 'id'=>GeoAPI::Client.id_from_guid(guid,api_key)}))
end
|
.create_at_lat_lng(*args) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/geoapi/entity.rb', line 81
def self.create_at_lat_lng(*args)
params = args.
params = params == {} ? nil : params
puts "GEOAPI::Entity.create_at_lat_lng #{params.to_json}"
raise ArgumentError, ":lat must be sent as a parameter" unless params.has_key?(:lat)
raise ArgumentError, ":lng must be sent as a parameter" unless params.has_key?(:lng)
puts "test API key #{api_key}"
raise ArgumentError, "An API Key is required" if api_key.blank?
p = GeoAPI::Point.new(:lat => params[:lat],:lng => params[:lng])
params.delete(:lat)
params.delete(:lng)
self.create(params.merge({:geom=>p}))
end
|
.destroy(params) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/geoapi/entity.rb', line 61
def self.destroy(params)
puts "GEOAPI::Entity.destroy #{params.to_json}"
raise ArgumentError, "An id or guid is required (pass :id or :guid in parameters)" unless params.has_key?(:id) || params.has_key?(:guid)
raise ArgumentError, "An API Key is required" if api_key.blank?
begin
unless params[:guid].blank?
delete("/e/#{params[:guid]}?apikey=#{api_key}")
else
delete("/e/user-#{api_key}-#{params[:id]}?apikey=#{api_key}") unless params[:id].blank?
end
rescue
raise BadRequest, "There was a problem communicating with the API"
end
end
|
.find(*args) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/geoapi/entity.rb', line 101
def self.find(*args)
puts "GEOAPI::Entity.find #{args.to_s}"
raise ArgumentError, "First argument must be symbol (:all or :get)" unless args.first.kind_of?(Symbol)
params = args.
params = params == {} ? nil : params
api_key = self.api_key_from_parameters(params)
raise ArgumentError, "An API Key is required" if api_key.blank?
case args.first
when :all
results = []
else
results = nil
raise ArgumentError, "Arguments should include a :guid or :id" if params[:guid].blank? && params[:id].blank?
params[:guid] = "user-#{api_key}-#{the_id}" unless params[:id].blank?
response = get("/e/#{params[:guid]}?apikey=#{api_key}")
begin
rescue
raise BadRequest, "There was a problem communicating with the API"
end
results = Entity.new(response['result'].merge({'guid'=>params[:guid]})) unless response['result'].blank? end
results
end
|
.find_by_guid(the_guid, options = {}) ⇒ Object
150
151
152
153
154
155
156
157
158
|
# File 'lib/geoapi/entity.rb', line 150
def self.find_by_guid(the_guid, options={})
puts "GEOAPI::Entity.find_by_guid #{the_guid}"
api_key = self.api_key_from_parameters(options)
raise ArgumentError, "An API Key is required" if api_key.blank?
self.find(:get, :guid=>the_guid)
end
|
.find_by_id(the_id, options = {}) ⇒ Object
140
141
142
143
144
145
146
147
148
|
# File 'lib/geoapi/entity.rb', line 140
def self.find_by_id(the_id, options={})
puts "GEOAPI::Entity.find_by_id #{the_id}"
api_key = self.api_key_from_parameters(options)
raise ArgumentError, "An API Key is required" if api_key.blank?
self.find(:get, :guid=>"user-#{api_key}-#{the_id}")
end
|
Instance Method Details
#delete ⇒ Object
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/geoapi/entity.rb', line 265
def delete
puts "GEOAPI::Entity.delete #{self.guid}"
raise ArgumentError, "An API Key is required" if api_key.blank?
raise ArgumentError, "Object has no :guid" if self.guid.blank?
begin
Entity.destroy(:guid=>self.guid)
rescue
raise BadRequest, "There was a problem communicating with the API"
end
end
|
#destroy ⇒ Object
279
280
281
|
# File 'lib/geoapi/entity.rb', line 279
def destroy
self.delete
end
|
#find_view(view_name) ⇒ Object
297
298
299
300
301
|
# File 'lib/geoapi/entity.rb', line 297
def find_view view_name
views.each do |view|
return view if view.name == view_name
end
end
|
#find_view_entries(view_name) ⇒ Object
303
304
305
|
# File 'lib/geoapi/entity.rb', line 303
def find_view_entries view_name
find_view(view_name).load.entries
end
|
#latitude ⇒ Object
Also known as:
lat
9
10
11
|
# File 'lib/geoapi/entity.rb', line 9
def latitude
@latitude ||= geometry.latitude unless geometry.blank?
end
|
#load ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/geoapi/entity.rb', line 244
def load
puts "GEOAPI::Entity.load #{self.guid}"
raise ArgumentError, "An API Key is required" if api_key.blank?
raise ArgumentError, "Properties should include a .guid or .id" if self.guid.blank? && self.id.blank?
the_guid = self.guid
the_guid ||= "user-#{api_key}-#{self.id}"
begin
response = self.class.get("/e/#{the_guid}?apikey=#{api_key}")
rescue
raise BadRequest, "There was a problem communicating with the API"
end
self.setup(response['result'].merge({'guid'=>self.guid }))
self
end
|
#longitude ⇒ Object
Also known as:
lon, lng
13
14
15
|
# File 'lib/geoapi/entity.rb', line 13
def longitude
@longitude ||= geometry.longitude unless geometry.blank?
end
|
#save ⇒ Object
283
284
285
|
# File 'lib/geoapi/entity.rb', line 283
def save
update
end
|
#setup(attrs) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/geoapi/entity.rb', line 167
def setup(attrs)
puts "Setup Entity with attributes: #{attrs.to_json}"
api_key = @api_key
api_key ||= self.api_key_from_parameters(params)
self.guid = attrs['guid'] if attrs.has_key?('guid')
self.guid = "user-#{@api_key}-#{attrs['id']}" if attrs.has_key?('id')
puts "GEOAPI::Entity.setup #{self.guid}"
self.id = attrs['id'] if attrs.has_key?('id')
self.id = GeoAPI::Client.id_from_guid(self.guid,api_key) if self.id.blank?
self.errors = attrs['error']
self.name = attrs['name']
self.name ||= attrs['meta']['name'] unless attrs['meta'].blank?
self.entity_type = attrs['type']
self.shorturl = attrs['shorturl']
self.geom = GeoAPI::Geometry.from_hash(attrs['geom']) unless attrs['geom'].blank?
self.geom ||= GeoAPI::Geometry.from_hash(attrs['meta']['geom']) unless attrs['meta'].blank?
self.views = []
unless attrs['views'].blank?
if attrs['views'].size > 0
attrs['views'].each do |view|
self.views << GeoAPI::View.new({'name'=>view, 'guid'=>self.guid})
(class <<self; self; end).send :define_method, :"#{view}_view" do
find_view("#{view}")
end
(class <<self; self; end).send :define_method, :"#{view}_view_entries" do
find_view_entries("#{view}")
end
end
end
end
self.userviews = []
unless attrs['userviews'].blank?
if attrs['userviews'].size > 0
attrs['userviews'].each do |view|
self.userviews << GeoAPI::UserView.new({'name'=>view, 'guid'=>self.guid})
class << self
define_method "#{view}_userview" do
find_view("#{view}")
end
define_method :"#{view}_entries" do
find_view("#{view}").entries
end
end
end
end
end
self
end
|
#to_json(options = nil) ⇒ Object
291
292
293
|
# File 'lib/geoapi/entity.rb', line 291
def to_json options=nil
{:name=>name, :guid=>guid, :type=>entity_type, :geom=>geom, :views=>views, :userviews=>userviews, :shorturl=>shorturl}.to_json
end
|
#to_s ⇒ Object
287
288
289
|
# File 'lib/geoapi/entity.rb', line 287
def to_s
self.name
end
|
#type ⇒ Object
232
233
234
|
# File 'lib/geoapi/entity.rb', line 232
def type self.entity_type
end
|
#update ⇒ Object
236
237
238
239
240
241
242
|
# File 'lib/geoapi/entity.rb', line 236
def update
puts "GEOAPI::Entity.update #{self.guid}"
raise ArgumentError, "An API Key is required" if api_key.blank?
self.setup(post("/e/#{guid}?apikey=#{api_key}", {:body=>self.to_json}))
end
|