Class: GoogleMapTimeline

Inherits:
Object
  • Object
show all
Defined in:
lib/google_timeline/google_map_timeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, home_coordinate) ⇒ GoogleMapTimeline

Returns a new instance of GoogleMapTimeline.



5
6
7
8
# File 'lib/google_timeline/google_map_timeline.rb', line 5

def initialize(file_path, home_coordinate)
  self.file_path = file_path
  self.home_coordinate = home_coordinate
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



3
4
5
# File 'lib/google_timeline/google_map_timeline.rb', line 3

def file_path
  @file_path
end

#home_coordinateObject

Returns the value of attribute home_coordinate.



2
3
4
# File 'lib/google_timeline/google_map_timeline.rb', line 2

def home_coordinate
  @home_coordinate
end

Instance Method Details

#get_timeline_url(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/google_timeline/google_map_timeline.rb', line 10

def get_timeline_url(args)
  #initialize flags
  overall_distance_flag = false
  transport_mode_flag = false
  places_flag = false
  if (args.empty?)==false
    overall_distance_flag = !args.select{|a| a.is_a?(Hash) and a[:distance]==true}.first.nil?
    transport_mode_flag = !args.select{|a| a.is_a?(Hash) and a[:transport_mode]==true}.first.nil?
    places_flag = !args.select{|a| a.is_a?(Hash) and a[:places]==true}.first.nil?
  end
  
  op_hash = {}
  url=default_map
  coordinates = []
  value = self.file_path
  key = Date.parse(value.split("/").last.split(".").first.gsub("history-", "")) rescue nil
  op_hash[:date] = key
  
  if key.nil? == false and File.exists?(value)
    kml = File.read(value)
    doc = Nokogiri::XML(kml)
    coordinates = doc.search('coordinates').map{|c| c.content.split(" ")}.flatten.map{|co| co.split(",")[0..1].reverse.map{|co1| co1.to_f}} rescue []
    if coordinates.is_a?(Array) 
      location_str = polylines_encoding(coordinates)

      if location_str.length > 1900
        coordinates_dup = coordinates.dup
        while location_str.length > 1900 do
          coordinates_dup = coordinates_dup.values_at(* coordinates_dup.each_index.select {|i| i.even?})
          location_str = polylines_encoding(coordinates_dup)
        end
      end
      url = location_url(location_str)  
    end

  end
  op_hash[:url] = url
  #overall distance
  if overall_distance_flag
    distance = {:value => overall_distance(coordinates), :unit=>"meter"}
    op_hash[:distance] = distance
  end
  #Transport Mode and places
  
  
  if (transport_mode_flag or places_flag)
    transport_mode_hash, places_array = transport_mode_and_places(doc, transport_mode_flag, places_flag)
    op_hash[:transport_mode] = transport_mode_hash if transport_mode_flag
    op_hash[:places] = places_array if places_flag
  end
  return op_hash
end