Class: Runkeeper::Activity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, attrs) ⇒ Activity

Returns a new instance of Activity.



7
8
9
10
11
12
13
# File 'lib/runkeeper/activity.rb', line 7

def initialize(connection, attrs)
  @connection = connection
  @attrs = attrs
  attrs.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#average_heart_rateObject

Returns the value of attribute average_heart_rate.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def average_heart_rate
  @average_heart_rate
end

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/runkeeper/activity.rb', line 4

def connection
  @connection
end

#durationObject

Returns the value of attribute duration.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def duration
  @duration
end

#notesObject

Returns the value of attribute notes.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def notes
  @notes
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def path
  @path
end

#post_to_facebookObject

Returns the value of attribute post_to_facebook.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def post_to_facebook
  @post_to_facebook
end

#post_to_twitterObject

Returns the value of attribute post_to_twitter.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def post_to_twitter
  @post_to_twitter
end

#start_timeObject

Returns the value of attribute start_time.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def start_time
  @start_time
end

#total_caloriesObject

Returns the value of attribute total_calories.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def total_calories
  @total_calories
end

#total_distanceObject

Returns the value of attribute total_distance.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def total_distance
  @total_distance
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/runkeeper/activity.rb', line 5

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/runkeeper/activity.rb', line 4

def url
  @url
end

Instance Method Details

#formatted_start_timeObject



23
24
25
# File 'lib/runkeeper/activity.rb', line 23

def formatted_start_time
  start_time.strftime("%a, %d %b %Y %H:%M:%S %Z")
end

#idObject



19
20
21
# File 'lib/runkeeper/activity.rb', line 19

def id
  @save_response && @save_response.headers['location'][/\d+$/]
end

#optional_attrsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/runkeeper/activity.rb', line 36

def optional_attrs
  h = {}
  [
    :total_distance,
    :average_heart_rate,
    :total_calories,
    :notes,
    :path,
    :post_to_facebook,
    :post_to_twitter
  ].each do |method_name|
    v = send(method_name)
    h[method_name] = v if v
  end
  h
end

#saveObject



15
16
17
# File 'lib/runkeeper/activity.rb', line 15

def save
  @save_response = connection.save_activity(to_hash)
end

#to_hashObject



27
28
29
30
31
32
33
34
# File 'lib/runkeeper/activity.rb', line 27

def to_hash
  main = {
    :type => type,
    :start_time => formatted_start_time,
    :duration => duration
  }
  main.merge! optional_attrs
end