Class: Wms::Widget::Location1::Main

Inherits:
Base
  • Object
show all
Includes:
Api::Analytic, Api::Event
Defined in:
lib/wms_location1/main.rb

Overview

Run the widget

Open rails console
>Widget.load_widgets
>widget.run_widgets

Enjoy!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



37
38
39
40
# File 'lib/wms_location1/main.rb', line 37

def initialize
  super
  @logger.debug "Init widget [#{self.class.name}]"
end

Instance Attribute Details

#widgetObject

Returns the value of attribute widget.



35
36
37
# File 'lib/wms_location1/main.rb', line 35

def widget
  @widget
end

Instance Method Details

#register(options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/wms_location1/main.rb', line 43

def register(options={})
  @widget = options[:widget] 
  @begin = options[:begin]
  @end = options[:end]
end

#runObject



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
# File 'lib/wms_location1/main.rb', line 50

def run
  # Call api
  @logger.debug "Running widget [#{self.class.name}]" 

  @logger.debug @widget

  options = {
    #:device_id => "12345678",
    :type => "location",
    :begin => @begin,
    :end => @end
  }

  @events = get_events(options)

  (@events.count.to_i - 1).times do |i|
    cur = @events[i]
    nxt = @events[i + 1]

    analytic = {
      :device_id => "123456789",
      :widget_id => @widget.id,
      :user_id => @widget.user.id,
      :timestamp => Time.now,
      :src => {
        :latitude => cur["latitude"],
        :longitude => cur["longitude"]
      },
      :dest => {
        :latitude => nxt["latitude"],
        :longitude => nxt["longitude"]
      },
      :time_spent => (nxt["timestamp"].to_f - cur["timestamp"].to_f) * 1000.0
    }
    save_analytics(analytic)
  end
end