Class: Switchboard::Commands::PEP::Location

Inherits:
Switchboard::Command show all
Defined in:
lib/switchboard/commands/pep/location.rb

Constant Summary

Constants inherited from Switchboard::Command

Switchboard::Command::DEFAULT_OPTIONS, Switchboard::Command::OPTIONS

Class Method Summary collapse

Methods inherited from Switchboard::Command

description, help, options, to_command, to_command_name

Class Method Details

.run!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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/switchboard/commands/pep/location.rb', line 10

def self.run!
  begin
    require 'fire_hydrant'
  rescue LoadError => e
    gem = e.message.split("--").last.strip
    puts "The #{gem} gem is required for this command to work."
    exit 1
  end

  switchboard = Switchboard::Client.new
  switchboard.plug!(AutoAcceptJack, FireEagleJack)

  switchboard.on_startup do
    @location_helper = Jabber::UserLocation::Helper.new(client, nil)
  end

  switchboard.on_location_update do |user|
    # for some reason the Fire Eagle gem doesn't work when I access #name directly
    name = user.locations[0].instance_eval { @doc.at("//name").innerText }
    timestamp = Time.parse(user.locations[0].instance_eval { @doc.at("//located-at").innerText })

    area, postalcode, locality, region, country = nil

    user.locations.each do |loc|
      level = loc.instance_eval { @doc.at("//level-name").innerText }
      normal_name = loc.instance_eval { @doc.at("//normal-name").innerText }
      case level
      when "exact"
        street = normal_name
      when "postal"
        postalcode = normal_name
      when "neighborhood"
        area = normal_name
      when "city"
        locality = normal_name
      when "state"
        region = normal_name
      when "country"
        country = normal_name
      end
      puts "level: #{level}"
    end

    puts "Current location: #{name}."

    geom = user.locations[0].geom
    if geom.is_a?(GeoRuby::SimpleFeatures::Envelope)
      pt = geom.center
      accuracy = geom.upper_corner.spherical_distance(geom.lower_corner) / 2
    else
      pt = geom
      accuracy = 0
    end

    location = Jabber::UserLocation::Location.new \
      "accuracy"    => accuracy,
      "area"        => area,
      "country"     => country,
      "description" => name,
      "lat"         => pt.lat,
      "locality"    => locality,
      "lon"         => pt.lon,
      "postalcode"  => postalcode,
      "region"      => region,
      "street"      => street,
      "timestamp"   => timestamp

    # parsing thread is still running, so the send needs to be deferred
    defer :location_sent do
      @location_helper.current_location(location)
    end
  end

  switchboard.on_shutdown do
    begin
      @location_helper.stop_publishing
    rescue Jabber::ServerError => e
      puts e
    end
  end

  switchboard.run!
end