Class: Pincaster
- Inherits:
-
Object
- Object
- Pincaster
- Defined in:
- lib/pincaster.rb
Class Method Summary collapse
-
.add_layer(layer) ⇒ Object
adds a new layer with name of the given string.
-
.add_record(record) ⇒ Object
adds a new record as well as creates a layer for it if the latter does not exist already.
-
.client ⇒ Object
returns an existing http client properly configured or creates a new one once.
-
.config ⇒ Object
returns config.
-
.delete_layer!(layer) ⇒ Object
deletes the layer with the given name.
-
.delete_record(record) ⇒ Object
deletes the Pincaster record for the given ActiveRecord object.
-
.get_record(record) ⇒ Object
returns a pin object for the given ActiveRecord object.
-
.has_layer?(layer) ⇒ Boolean
returns true if Pincaster already has the given layer.
-
.is_alive? ⇒ Boolean
Pincaster server is still alive?.
-
.layer(layer) ⇒ Object
return a layer object for the layer that was searched for.
-
.layers ⇒ Object
returns all known layers.
-
.load_config ⇒ Object
loads local config, or users one if he provided any.
-
.nearby(record, radius, limit) ⇒ Object
returns all pins nearby given record, maximum radius meters away, returns limit number of pins.
-
.shutdown! ⇒ Object
shutdown Pincaster server immediately.
Class Method Details
.add_layer(layer) ⇒ Object
adds a new layer with name of the given string
45 46 47 48 |
# File 'lib/pincaster.rb', line 45 def self.add_layer(layer) raise "Layer has to be a string" if not layer.is_a?(String) self.client.send_request('POST', "/layers/#{layer}.json").code == "200" ? true : false rescue false end |
.add_record(record) ⇒ Object
adds a new record as well as creates a layer for it if the latter does not exist already
63 64 65 66 67 68 69 70 71 |
# File 'lib/pincaster.rb', line 63 def self.add_record(record) raise "Can't add a record without geocoordinates lng, lat" if record.pin_lng.nil? or record.pin_lat.nil? Pincaster.add_layer(record.class.to_s) if not Pincaster.has_layer?(record.class.to_s) self.client.send_request('PUT', "/records/#{record.class.to_s}/#{record.id}.json", nil, nil, {:_loc => "#{record.pin_lat},#{record.pin_lng}"}.merge(record.additional_attributes)).code == "200" ? true : false end |
.client ⇒ Object
returns an existing http client properly configured or creates a new one once
10 11 12 |
# File 'lib/pincaster.rb', line 10 def self.client @@http_client ||= HttpClient.new(self.config.protocol, self.config.host, self.config.port, self.config.namespace) end |
.config ⇒ Object
returns config
105 106 107 |
# File 'lib/pincaster.rb', line 105 def self.config @@config ||= self.load_config end |
.delete_layer!(layer) ⇒ Object
deletes the layer with the given name
51 52 53 54 |
# File 'lib/pincaster.rb', line 51 def self.delete_layer!(layer) raise "Layer has to be a string" if not layer.is_a?(String) self.client.send_request('DELETE', "/layers/#{layer}.json").code == "200" ? true : false rescue false end |
.delete_record(record) ⇒ Object
deletes the Pincaster record for the given ActiveRecord object
79 80 81 |
# File 'lib/pincaster.rb', line 79 def self.delete_record(record) self.client.send_request('DELETE', "/records/#{record.class.to_s}/#{record.id}.json").code == "200" ? true : false end |
.get_record(record) ⇒ Object
returns a pin object for the given ActiveRecord object
74 75 76 |
# File 'lib/pincaster.rb', line 74 def self.get_record(record) PincasterPin.new(JSON.parse(self.client.send_request('GET', "/records/#{record.class.to_s}/#{record.id}.json").body)) rescue nil end |
.has_layer?(layer) ⇒ Boolean
returns true if Pincaster already has the given layer
39 40 41 42 |
# File 'lib/pincaster.rb', line 39 def self.has_layer?(layer) raise "Layer has to be a string" if not layer.is_a?(String) self.layers.detect{|l| l["name"] == layer}.nil? ? false : true rescue false end |
.is_alive? ⇒ Boolean
Pincaster server is still alive?
15 16 17 |
# File 'lib/pincaster.rb', line 15 def self.is_alive? self.client.send_request('GET', '/system/ping.json').code == "200" ? true : false rescue false end |
.layer(layer) ⇒ Object
return a layer object for the layer that was searched for
57 58 59 60 |
# File 'lib/pincaster.rb', line 57 def self.layer(layer) raise "Layer has to be a string" if not layer.is_a?(String) PincasterLayer.new(self.layers.select{|l| l["name"] == layer}) end |
.layers ⇒ Object
returns all known layers
34 35 36 |
# File 'lib/pincaster.rb', line 34 def self.layers JSON.parse(self.client.send_request('GET', '/layers/index.json').body)["layers"] end |
.load_config ⇒ Object
loads local config, or users one if he provided any
95 96 97 98 99 100 101 102 |
# File 'lib/pincaster.rb', line 95 def self.load_config begin config = YAML.load_file(Rails.root.to_s + "/config/kingpin.yml") rescue config = YAML.load_file(File.dirname(__FILE__).to_s + "/../config/kingpin.yml") end PincasterConfig.new(config) end |
.nearby(record, radius, limit) ⇒ Object
returns all pins nearby given record, maximum radius meters away, returns limit number of pins
84 85 86 87 88 89 90 91 92 |
# File 'lib/pincaster.rb', line 84 def self.nearby(record, radius, limit) limit ||= 2000 raise "Given #{record.class.to_s} has not lng or lat." if record.pin_lng.nil? or record.pin_lat.nil? self.client.send_request('GET', "/search/#{record.class.to_s}/nearby/#{record.pin_lat.to_s},#{record.pin_lng.to_s}.json", nil, nil, {:radius => radius.to_s, :limit => limit}).body end |
.shutdown! ⇒ Object
shutdown Pincaster server immediately
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pincaster.rb', line 20 def self.shutdown! begin self.client.send_request('POST', '/system/shutdown.json') rescue Exception => e case e. when "end of file reached" return true else return false end end end |