Class: RunKeeperActivities::Activity

Inherits:
Object
  • Object
show all
Defined in:
lib/runkeeper_activities/activity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Activity

Returns a new instance of Activity.



20
21
22
# File 'lib/runkeeper_activities/activity.rb', line 20

def initialize(options = {})
  options.each { |k,v| instance_variable_set("@#{k}".to_sym, v) }
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/runkeeper_activities/activity.rb', line 3

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/runkeeper_activities/activity.rb', line 3

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/runkeeper_activities/activity.rb', line 3

def user
  @user
end

Class Method Details

.activities_from_user(user) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/runkeeper_activities/activity.rb', line 8

def activities_from_user(user)
  user.page.css('#activityHistoryMenu .menuItem').collect do |_feed_item|
    RunKeeperActivities::Activity.new({
      :user => user,
      :url => Utils.runkeeper_url(_feed_item['link']),
      :distance => _feed_item.at_css('.distance').text,
      :type => _feed_item.at_css('.mainText').text
    })
  end
end

Instance Method Details

#caloriesObject



52
53
54
# File 'lib/runkeeper_activities/activity.rb', line 52

def calories
  @calories ||= data[:statsCalories]
end

#dataObject



68
69
70
# File 'lib/runkeeper_activities/activity.rb', line 68

def data
  @data ||= Yajl::Parser.parse(raw_json_data, :symbolize_keys => true)
end

#distanceObject



36
37
38
# File 'lib/runkeeper_activities/activity.rb', line 36

def distance
  @distance ||= data[:statsDistance]
end

#durationObject



40
41
42
# File 'lib/runkeeper_activities/activity.rb', line 40

def duration
  @duration ||= data[:statsDuration]
end

#elevationObject



56
57
58
# File 'lib/runkeeper_activities/activity.rb', line 56

def elevation
  @elevation ||= data[:statsElevation]
end

#end_timeObject



32
33
34
# File 'lib/runkeeper_activities/activity.rb', line 32

def end_time
  @end_time ||= (data[:points] and data[:points].any?) ? Time.parse(data[:points].last[:time]) : nil
end

#idObject



24
25
26
# File 'lib/runkeeper_activities/activity.rb', line 24

def id
  @id ||= (_match = @url.match(/\/(\d+)$/)) ? _match[1] : (raise "Need profile url to find user id")
end

#json_endpointObject



72
73
74
# File 'lib/runkeeper_activities/activity.rb', line 72

def json_endpoint
  @json_endpoint ||= Utils.runkeeper_url("/ajax/pointData?activityId=#{id}")
end

#messageObject



60
61
62
# File 'lib/runkeeper_activities/activity.rb', line 60

def message
  @message ||= data[:feedData][:message]
end

#paceObject



44
45
46
# File 'lib/runkeeper_activities/activity.rb', line 44

def pace
  @pace ||= data[:statsPace]
end

#raw_json_dataObject



76
77
78
# File 'lib/runkeeper_activities/activity.rb', line 76

def raw_json_data
  @raw_json_data ||= open(json_endpoint).readlines.join("\n").encode('UTF-8')
end

#speedObject



48
49
50
# File 'lib/runkeeper_activities/activity.rb', line 48

def speed
  @speed ||= data[:statsSpeed]
end

#start_timeObject



28
29
30
# File 'lib/runkeeper_activities/activity.rb', line 28

def start_time
  @start_time ||= (data[:points] and data[:points].any?) ? Time.parse(data[:points][0][:time]) : nil
end

#summeryObject



64
65
66
# File 'lib/runkeeper_activities/activity.rb', line 64

def summery
  @summery ||= "#{user.name} completed a #{distance} #{user.distance_unit} #{type.downcase} activity"
end