Class: Placed::Location
- Inherits:
-
Object
- Object
- Placed::Location
- Defined in:
- lib/placed/location.rb
Instance Attribute Summary collapse
-
#accuracy ⇒ Object
readonly
Returns the value of attribute accuracy.
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#lat ⇒ Object
readonly
Returns the value of attribute lat.
-
#lng ⇒ Object
readonly
Returns the value of attribute lng.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#public ⇒ Object
readonly
Returns the value of attribute public.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #get_coords ⇒ Object
-
#initialize(options = {}) ⇒ Location
constructor
A new instance of Location.
- #location ⇒ Object
- #placed ⇒ Object
- #put ⇒ Object
- #reference ⇒ Object
- #remove ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Location
Returns a new instance of Location.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/placed/location.rb', line 6 def initialize( = {}) raise "No address no fun!" if [:address].nil? @api_key = Placed.config['api_key'] @address = [:address] @types = [:types] ||= Placed.config['default_types'] @name = [:name] ||= Placed.config['default_name'] @accuracy = [:accuracy] ||= Placed.config['default_accuracy'] @language = [:language] ||= Placed.config['default_language'] @lat = get_coords[:lat] rescue "Unknown" @lng = get_coords[:lng] rescue "Unknown" @ref = placed['results'][0]['reference'] rescue nil end |
Instance Attribute Details
#accuracy ⇒ Object (readonly)
Returns the value of attribute accuracy.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def accuracy @accuracy end |
#address ⇒ Object (readonly)
Returns the value of attribute address.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def address @address end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def api_key @api_key end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def language @language end |
#lat ⇒ Object (readonly)
Returns the value of attribute lat.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def lat @lat end |
#lng ⇒ Object (readonly)
Returns the value of attribute lng.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def lng @lng end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def name @name end |
#public ⇒ Object (readonly)
Returns the value of attribute public.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def public @public end |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def ref @ref end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
4 5 6 |
# File 'lib/placed/location.rb', line 4 def types @types end |
Instance Method Details
#get_coords ⇒ Object
19 20 21 22 |
# File 'lib/placed/location.rb', line 19 def get_coords data = HTTParty.get("http://maps.google.com/maps/api/geocode/json?address=#{ERB::Util.u(self.address)}&sensor=false") return { lat: data['results'][0]['geometry']['location']['lat'], lng: data['results'][0]['geometry']['location']['lng'] } if data['results'] end |
#location ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/placed/location.rb', line 41 def location { location: { lat: self.lat, lng: self.lng }, accuracy: self.accuracy, name: self.name, types: [ self.types ], language: self.language }.to_json end |
#placed ⇒ Object
36 37 38 39 |
# File 'lib/placed/location.rb', line 36 def placed data = HTTParty.get("https://maps.googleapis.com/maps/api/place/search/json?location=#{self.lat},#{self.lng}&radius=3&name=#{ERB::Util.u(self.name)}&sensor=false&key=#{self.api_key}") return data end |
#put ⇒ Object
24 25 26 27 |
# File 'lib/placed/location.rb', line 24 def put url = "https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key=#{self.api_key}" @result = HTTParty.post(url.to_str, :body => location, :headers => { 'Content-Type' => 'application/json' } ) end |
#reference ⇒ Object
50 51 52 |
# File 'lib/placed/location.rb', line 50 def reference { reference: self.ref }.to_json end |
#remove ⇒ Object
29 30 31 32 33 34 |
# File 'lib/placed/location.rb', line 29 def remove url = "https://maps.googleapis.com/maps/api/place/delete/json?sensor=false&key=#{self.api_key}" @result = HTTParty.post(url.to_str, :body => reference, :headers => { 'Content-Type' => 'application/json' } ) @ref = nil if @result['status'] == "OK" return @result end |