Class: Placed::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/placed/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  raise "No address no fun!" if options[:address].nil?
  @api_key  = Placed.config['api_key']
  @address  = options[:address]
  @types    = options[:types] ||= Placed.config['default_types']
  @name     = options[:name] ||= Placed.config['default_name']
  @accuracy = options[:accuracy] ||= Placed.config['default_accuracy']
  @language = options[: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

#accuracyObject (readonly)

Returns the value of attribute accuracy.



4
5
6
# File 'lib/placed/location.rb', line 4

def accuracy
  @accuracy
end

#addressObject (readonly)

Returns the value of attribute address.



4
5
6
# File 'lib/placed/location.rb', line 4

def address
  @address
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



4
5
6
# File 'lib/placed/location.rb', line 4

def api_key
  @api_key
end

#languageObject (readonly)

Returns the value of attribute language.



4
5
6
# File 'lib/placed/location.rb', line 4

def language
  @language
end

#latObject (readonly)

Returns the value of attribute lat.



4
5
6
# File 'lib/placed/location.rb', line 4

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



4
5
6
# File 'lib/placed/location.rb', line 4

def lng
  @lng
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/placed/location.rb', line 4

def name
  @name
end

#publicObject (readonly)

Returns the value of attribute public.



4
5
6
# File 'lib/placed/location.rb', line 4

def public
  @public
end

#refObject (readonly)

Returns the value of attribute ref.



4
5
6
# File 'lib/placed/location.rb', line 4

def ref
  @ref
end

#typesObject (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_coordsObject



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

#locationObject



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

#placedObject



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

#putObject



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

#referenceObject



50
51
52
# File 'lib/placed/location.rb', line 50

def reference
  { reference: self.ref }.to_json
end

#removeObject



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