nike_v2 0.3.4 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,8 +19,8 @@ module NikeV2
19
19
  define_method(method_var_name){ ivar = instance_variable_get('@' + method_var_name); ivar ||= sum_of(method_var_name)}
20
20
 
21
21
 
22
- define_method("total_#{type.downcase}_during") do |start_date, end_date|
23
- @activities_array.reject{|a| ((a.started_at..a.ended_at) & (start_date..end_date)).nil?}.collect{|a| a.send("total_#{type.downcase}_during", start_date, end_date)}.inject(:+)
22
+ define_method("total_#{type.downcase}_during") do |start_date, end_date, convert = false|
23
+ @activities_array.reject{|a| ((a.started_at..a.ended_at) & (start_date..end_date)).nil?}.collect{|a| a.send("total_#{type.downcase}_during", start_date, end_date, convert)}.inject(:+)
24
24
  end
25
25
  end
26
26
  end
@@ -22,6 +22,10 @@ module NikeV2
22
22
  super(attributes)
23
23
  end
24
24
 
25
+ def tz
26
+ TZInfo::Timezone.get(self.activity_time_zone)
27
+ end
28
+
25
29
  def gps_data
26
30
  @gps_data ||= NikeV2::GpsData.new(:activity => self)
27
31
  end
@@ -40,11 +44,20 @@ module NikeV2
40
44
  end
41
45
 
42
46
  def started_at
43
- @started_at ||= Time.parse(self.start_time.gsub(/Z$/,''))
47
+ @started_at ||= Time.parse(self.start_time.gsub('Z', '-') + '00:00')
44
48
  end
45
49
 
50
+ # some reason activities aren't always complete so we need metrics to figure out how long they are
46
51
  def ended_at
47
- @ended_at ||= Time.parse(self.end_time.gsub(/Z$/,''))
52
+ @ended_at ||= self.respond_to?(:end_time) ? Time.parse(self.end_time.gsub('Z', '-') + '00:00') : started_at + (metrics.durations.to.seconds).to_f
53
+ end
54
+
55
+ def to_tz(time)
56
+ if time.respond_to?(:strftime)
57
+ return Time.parse(time.strftime('%Y-%m-%d %H:%M:%S ' + self.tz.to_s))
58
+ else
59
+ return Time.parse(time + ' ' + self.tz.to_s)
60
+ end
48
61
  end
49
62
 
50
63
  private
@@ -21,12 +21,22 @@ module NikeV2
21
21
  @total ||= values.inject(:+)
22
22
  end
23
23
 
24
- def total_during(start, stop)
24
+ def total_during(start, stop, convert_to_local = false)
25
+ if convert_to_local
26
+ start = @activity.to_tz(start)
27
+ stop = @activity.to_tz(stop)
28
+ end
25
29
  during(start, stop).collect(&:to_f).inject(:+) rescue 0
26
30
  end
27
31
 
28
32
  def during(start, stop)
29
- @values[time_to_index(start)..time_to_index(stop)]
33
+ start_point = time_to_index(start)
34
+ duration = time_to_index(stop) - start_point
35
+ @values[start_point, duration]
36
+ end
37
+
38
+ def duration
39
+ @values.length.send(@unit.downcase)
30
40
  end
31
41
 
32
42
  private
@@ -23,6 +23,10 @@ module NikeV2
23
23
  define_method(method_var_name + '_during'){|*args| sum_of_type_during(type, *args)}
24
24
  end
25
25
 
26
+ def durations
27
+ @metrics_array.collect(&:duration).sort.first
28
+ end
29
+
26
30
  private
27
31
  def build_metrics(data_set)
28
32
  data_set.each do |metric|
@@ -1,3 +1,3 @@
1
1
  module NikeV2
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.7'
3
3
  end
data/lib/nike_v2.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'alchemist'
2
2
  require 'forwardable'
3
3
  require 'ext/core_ext'
4
+ require 'tzinfo'
4
5
 
5
6
  module NikeV2
6
7
  def self.configuration
data/nike_v2.gemspec CHANGED
@@ -15,9 +15,10 @@ Gem::Specification.new do |s|
15
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
16
  s.require_paths = ['lib']
17
17
 
18
- s.add_runtime_dependency 'httparty', '>= 0.10.0'
19
- s.add_runtime_dependency "httparty_sober", "~> 0.2.1"
20
- s.add_runtime_dependency 'alchemist', '>= 0.1.3'
18
+ s.add_runtime_dependency 'httparty', '0.10.2'
19
+ s.add_runtime_dependency "httparty_sober", "0.2.1"
20
+ s.add_runtime_dependency 'alchemist', '0.1.3'
21
+ s.add_runtime_dependency 'tzinfo', '0.3.36'
21
22
 
22
23
  s.add_development_dependency 'factory_girl', '~> 4.2.0'
23
24
  s.add_development_dependency 'rake', '~> 10.0.3'
@@ -331,8 +331,40 @@ http_interactions:
331
331
  "calories": 581, "fuel": 1653, "distance": 2.0936965942382812, "steps": 2659,
332
332
  "duration": "7:02:00.000", "activityType": "ALL_DAY", "startTime": "2013-02-26T07:00:00Z",
333
333
  "activityTimeZone": "America/Phoenix", "status": "IN_PROGRESS", "deviceType":
334
- "FUELBAND", "tags": [], "metrics": [] } ], "paging": {
335
- } }'
334
+ "FUELBAND", "tags": [], "metrics": [] } ], "paging": { } }'
336
335
  http_version:
337
336
  recorded_at: Thu, 14 Mar 2013 00:45:14 GMT
337
+ - request:
338
+ method: get
339
+ uri: https://api.nike.com/me/sport/activities?access_token=foobar&count=1&endDate=2013-03-29&startDate=2013-03-29
340
+ body:
341
+ encoding: US-ASCII
342
+ string: ''
343
+ headers:
344
+ Accept:
345
+ - application/json
346
+ Appid:
347
+ - nike
348
+ response:
349
+ status:
350
+ code: 200
351
+ message: OK
352
+ headers:
353
+ Content-Type:
354
+ - application/json;charset=UTF-8
355
+ Date:
356
+ - Sun, 31 Mar 2013 00:42:39 GMT
357
+ Server:
358
+ - Apache
359
+ X-Swooshlet:
360
+ - app-msp-api-0 prd sin-165-app-msp-api-0
361
+ Content-Length:
362
+ - '472'
363
+ Connection:
364
+ - keep-alive
365
+ body:
366
+ encoding: US-ASCII
367
+ string: ! '{"data":[{"activityId":"1b4f5b2c-26c4-45cc-952a-d62c61cf506a","activityType":"ALL_DAY","startTime":"2013-03-30T07:00:00Z","activityTimeZone":"America/Phoenix","status":"NONE","deviceType":"FUELBAND","metricSummary":{"calories":1462,"fuel":4158,"distance":6.641719,"steps":8435,"duration":"8:48:00.000"},"tags":[],"metrics":[]}],"paging":{"next":"/me/sport/activities?count=1&startDate=2013-03-29&endDate=2013-03-29&access_token=foobar&offset=2"}}'
368
+ http_version:
369
+ recorded_at: Sun, 31 Mar 2013 00:42:43 GMT
338
370
  recorded_with: VCR 2.4.0
@@ -32,8 +32,8 @@ http_interactions:
32
32
  encoding: US-ASCII
33
33
  string: ! '{ "activityId": "91b501dc-4a38-44aa-b537-6095418713d8", "calories":
34
34
  257, "fuel": 662, "distance": 3.1317999362945557, "steps": 10, "duration":
35
- "0:14:31.000", "activityType": "RUN", "startTime": "2011-08-11T00:00:00Z",
36
- "activityTimeZone": "GMT-08:00", "status": "COMPLETE", "deviceType": "SPORTWATCH",
35
+ "0:14:31.000", "activityType": "RUN", "startTime": "2011-08-11T07:00:00Z",
36
+ "activityTimeZone": "America/Phoenix", "status": "COMPLETE", "deviceType": "SPORTWATCH",
37
37
  "tags": [ { "tagType": "NOTE", "value": "text string" }, { "tagType": "COURT",
38
38
  "value": "DUNK" }, { "tagType": "TERRAIN", "value": "TRAIL" }, { "tagType":
39
39
  "EMOTION", "value": "HAPPY" }, { "tagType": "SHOES", "value": "AirMax" },
@@ -26,10 +26,9 @@ describe NikeV2::Activities do
26
26
  end
27
27
 
28
28
  it 'should accept the start_date parameter, camelcase it and return an array of Nike::Activity' do
29
- pending
30
- # person.activities(:count => 1).should be_kind_of(NikeV2::Activities)
31
- # person.activities(:count => 1).first.should be_kind_of(NikeV2::Activity)
32
- # person.activities(:count => 1).length.should == 1
29
+ person.activities(:count => 1, :start_date => '2013-03-29', :end_date => '2013-03-29').should be_kind_of(NikeV2::Activities)
30
+ person.activities(:count => 1).first.should be_kind_of(NikeV2::Activity)
31
+ person.activities(:count => 1).length.should == 1
33
32
  end
34
33
 
35
34
  it 'should calculate all the fuels' do
@@ -18,7 +18,7 @@ describe NikeV2::Activity do
18
18
 
19
19
  it 'should sum the metrics during a time period' do
20
20
  activity.load_data.should be_true
21
- activity.total_fuel_during(Time.parse('2011-08-11T00:00:00 '+ activity.activity_time_zone), Time.parse('2011-08-11T01:00:00 ' + activity.activity_time_zone)).should == 905.0
21
+ activity.total_fuel_during(Time.parse('2011-08-11T00:00:00-07:00'), Time.parse('2011-08-11T01:00:00-07:00'), true).should == 883.0
22
22
  end
23
23
  end
24
24
  end
@@ -18,12 +18,12 @@ describe NikeV2::Metric do
18
18
 
19
19
  it 'should give an array of values during a time range' do
20
20
  metric = activity.metrics.last
21
- metric.during(Time.at(activity.started_at.to_i + (60 * 60)), Time.at(activity.started_at.to_i + (60 * 120) - 1)).length.should == 60
21
+ metric.during(Time.at(activity.started_at.to_i + (60 * 60)), Time.at(activity.started_at.to_i + (60 * 120))).length.should == 60
22
22
  end
23
23
 
24
24
  it 'should total the values during a time range' do
25
25
  metric = activity.metrics.last
26
- metric.total_during(Time.at(activity.started_at.to_i + (60 * 60)), Time.at(activity.started_at.to_i + (60 * 120))).should == 831
26
+ metric.total_during(Time.at(activity.started_at.to_i + (60 * 60)), Time.at(activity.started_at.to_i + (60 * 120))).should == 806
27
27
  end
28
28
  end
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nike_v2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,30 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.10.0
21
+ version: 0.10.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.10.0
29
+ version: 0.10.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: httparty_sober
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - '='
36
36
  - !ruby/object:Gem::Version
37
37
  version: 0.2.1
38
38
  type: :runtime
@@ -40,7 +40,7 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - '='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.2.1
46
46
  - !ruby/object:Gem::Dependency
@@ -48,7 +48,7 @@ dependencies:
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - '='
52
52
  - !ruby/object:Gem::Version
53
53
  version: 0.1.3
54
54
  type: :runtime
@@ -56,9 +56,25 @@ dependencies:
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.1.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: tzinfo
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.3.36
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.3.36
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: factory_girl
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -170,7 +186,7 @@ files:
170
186
  - spec/fixtures/nike_api_cassettes/activity.yml
171
187
  - spec/fixtures/nike_api_cassettes/gps_data.yml
172
188
  - spec/fixtures/nike_api_cassettes/summary.yml
173
- - spec/nike_v2/acitivites_spec.rb
189
+ - spec/nike_v2/activites_spec.rb
174
190
  - spec/nike_v2/activity_spec.rb
175
191
  - spec/nike_v2/base_spec.rb
176
192
  - spec/nike_v2/gps_data_spec.rb
@@ -211,7 +227,7 @@ test_files:
211
227
  - spec/fixtures/nike_api_cassettes/activity.yml
212
228
  - spec/fixtures/nike_api_cassettes/gps_data.yml
213
229
  - spec/fixtures/nike_api_cassettes/summary.yml
214
- - spec/nike_v2/acitivites_spec.rb
230
+ - spec/nike_v2/activites_spec.rb
215
231
  - spec/nike_v2/activity_spec.rb
216
232
  - spec/nike_v2/base_spec.rb
217
233
  - spec/nike_v2/gps_data_spec.rb