class Geolocation

Attributes

report[RW]

Public Class Methods

new(token, &report_type) click to toggle source
# File lib/geolocation.rb, line 8
def initialize(token, &report_type)
  @report = report_type
  @token = token
end

Public Instance Methods

get_location_by_coordenates(latitude, longitude) click to toggle source
# File lib/geolocation.rb, line 17
def get_location_by_coordenates(latitude, longitude)
  retrieve_data("https://us1.locationiq.com/v1/reverse.php?key=#{@token}&lat=#{latitude}&lon=#{longitude}&format=json")
end
get_location_by_place(place) click to toggle source
# File lib/geolocation.rb, line 13
def get_location_by_place(place)
  retrieve_data("https://us1.locationiq.com/v1/search.php?key=#{@token}&q=#{place}&format=json")
end
get_nerby_countries(latitude, longitude, radius) click to toggle source
# File lib/geolocation.rb, line 21
def get_nerby_countries(latitude, longitude, radius)
  retrieve_data("https://us1.locationiq.com/v1/nearby.php?key=#{@token}&lat=#{latitude}&lon=#{longitude}&tag=countries&radius=#{radius}&format=json")
end
get_points_of_interest(latitude, longitude, radius, point) click to toggle source
# File lib/geolocation.rb, line 25
def get_points_of_interest(latitude, longitude, radius, point)
  retrieve_data("https://us1.locationiq.com/v1/nearby.php?key=#{@token}&lat=#{latitude}&lon=#{longitude}&tag=#{point}&radius=#{radius}&format=json")
end
give_format_to_response(data, position = 0) click to toggle source
# File lib/geolocation.rb, line 29
def give_format_to_response(data, position = 0)
  position == 0 ? @report.call(data) : @report.call(data,position)
end
save_data_in_file(filename, text) click to toggle source
# File lib/geolocation.rb, line 39
def save_data_in_file(filename, text)
  file = File.new(filename, "w")
  file.puts text
  file.close   
end

Private Instance Methods

parseData(data) click to toggle source
# File lib/geolocation.rb, line 45
def parseData(data)
  JSON.parse(data)
end
retrieve_data(url) click to toggle source
# File lib/geolocation.rb, line 33
def retrieve_data(url)
  uri = URI(url)
  data = Net::HTTP.get_response(uri)
  parseData(data.body)
end