Class: GeoAPI::GeoObject
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/geoapi/geo_object.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs) ⇒ GeoObject
Allow users to set the API key explicitly, or gain it from other sources. Makes for more readable code, and allows multiple keys to be used in the one application.
19
20
21
22
23
24
|
# File 'lib/geoapi/geo_object.rb', line 19
def initialize(attrs)
@api_key = self.class.api_key_from_parameters(attrs)
end
|
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
9
10
11
|
# File 'lib/geoapi/geo_object.rb', line 9
def api_key
@api_key
end
|
Class Method Details
.api_key_from_parameters(attrs) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/geoapi/geo_object.rb', line 33
def self.api_key_from_parameters(attrs)
unless attrs.blank?
the_api_key = attrs[:client].api_key if attrs.has_key?(:client)
the_api_key ||= attrs[:apikey] if attrs.has_key?(:apikey)
the_api_key ||= attrs[:api_key] if attrs.has_key?(:api_key)
the_api_key ||= attrs[:api_key] if attrs.has_key?(:api_key)
end
the_api_key ||= self.api_key
puts "API Key from parameters: #{the_api_key}"
@api_key = the_api_key
the_api_key
end
|
.delete(path, options = {}) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/geoapi/geo_object.rb', line 70
def self.delete path, options={}
puts "Delete: #{path} : #{options.to_s}"
timeout = options[:timeout] || 5
options.delete(:timeout)
retryable( :tries => 2 ) do
Timeout::timeout(timeout) do |t|
super path, options
end
end
end
|
.entity_type ⇒ Object
120
121
122
|
# File 'lib/geoapi/geo_object.rb', line 120
def self.entity_type
"user-entity"
end
|
.find(params) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/geoapi/geo_object.rb', line 112
def self.find params
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)
self.search({:lat=>params[:lat], :lng=>params[:lng],:type=>self.entity_type})
end
|
.get(path, options = {}) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/geoapi/geo_object.rb', line 57
def self.get path, options={}
puts "Get: #{path} : #{options.to_s}"
timeout = options[:timeout] || 5
options.delete(:timeout)
puts options.to_s
puts "Timeout #{timeout}"
retryable( :tries => 2 ) do
Timeout::timeout(timeout) do |t|
super path, options
end
end
end
|
.post(path, options = {}) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/geoapi/geo_object.rb', line 46
def self.post path, options={}
puts "Post: #{path} : #{options.to_s}"
timeout = options[:timeout] || 5
options.delete(:timeout)
retryable( :tries => 2 ) do
Timeout::timeout(timeout) do |t|
super path, options
end
end
end
|
.search(conditions, options = {}) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/geoapi/geo_object.rb', line 81
def self.search(conditions, options={})
puts "GEOAPI::Entity.search #{conditions.to_s}"
raise ArgumentError, ":lat and :lng are required for search" unless conditions.has_key?(:lat) && conditions.has_key?(:lng)
api_key = self.api_key_from_parameters(options)
raise ArgumentError, "An API Key is required" if api_key.blank?
conditions.merge!({:lat=>conditions[:lat],:lon=>conditions[:lng],:apikey=>api_key})
conditions.delete(:lng)
response = get("/search", {:timeout=>60, :query=>conditions})
begin
rescue
raise BadRequest, "There was a problem communicating with the API"
end
results = []
unless response['result'].blank?
response['result'].each do |result|
results << self.new(result)
end
results.reverse!
end
end
|