jawbone 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ module Jawbone
2
+
3
+ class DailySummary
4
+
5
+ attr_accessor :data
6
+
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ def steps
12
+ @data["move_steps"]
13
+ end
14
+
15
+ def kilometers
16
+ @data["move_kilometers"]
17
+ end
18
+
19
+ def workout?
20
+ !@data["move_summary"]["workouts"].empty?
21
+ end
22
+
23
+ def sleep_total
24
+ @data["sleep_summary"]["total"]
25
+ end
26
+
27
+ def sleep_quality
28
+ sleeps = @data["sleep_summary"]["sleeps"].map { |s| [s["details"]["duration"], s["details"]["quality"]]}
29
+ sleeps.sort_by { |s| s[0] }.first[1]
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), 'session')
2
+ require File.join(File.dirname(__FILE__), 'sleep')
3
+ require File.join(File.dirname(__FILE__), 'daily_summary')
4
+
5
+ module Jawbone
6
+
7
+ end
@@ -0,0 +1,52 @@
1
+ require 'httparty'
2
+
3
+ module Jawbone
4
+
5
+ class Session
6
+
7
+ attr_accessor :token
8
+
9
+ include HTTParty
10
+
11
+ def initialize email, password
12
+ response = self.class.post "https://jawbone.com/user/signin/login", { query:
13
+ { service: "nudge", email: email, pwd: password } }
14
+ @token = response["token"]
15
+ end
16
+
17
+ def feed
18
+ response = self.class.get "https://jawbone.com/nudge/api/users/@me/social", { query:
19
+ { after: "null", limit: 100, _token: @token } }
20
+ response["data"]["feed"]
21
+ end
22
+
23
+ def sleeps(feed = feed)
24
+ feed.select { |e| e["type"] == "sleep" }
25
+ end
26
+
27
+ def full_sleeps
28
+ local_sleeps = sleeps
29
+ local_sleeps.each do |sleep|
30
+ response = self.class.get "https://jawbone.com/nudge/api/sleeps/#{sleep["xid"]}/snapshot", { query: { _token: @token } }
31
+ data = response["data"]
32
+ sleep[:full_data] = data
33
+ end
34
+ end
35
+
36
+ def average_time_to_sleep
37
+ times = full_sleeps.map { |s| Sleep.new(s).time_to_sleep }
38
+ times.inject{ |sum, el| sum + el }.to_f / times.size # times.average
39
+ end
40
+
41
+ # requires date to be in yyyy-mm-dd format
42
+ def daily_summary date
43
+ # assumes Pacific Time
44
+ response = self.class.get "https://jawbone.com/nudge/api/users/@me/healthCredits", { query:
45
+ { _token: @token, check_levels: 1, eat_goal: 0, sleep_goal: 0, move_goal: 0,
46
+ timezone: "-28800", date: date } }
47
+ response["data"]
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,56 @@
1
+ module Jawbone
2
+
3
+ class Sleep
4
+ attr_accessor :data
5
+
6
+ # expects a hash
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ # note that these methods are also available in the sleep summary API call
12
+ [:awake, :deep, :light].each do |type|
13
+ define_method "#{type}_sleep" do
14
+ totals[type]
15
+ end
16
+ end
17
+
18
+ def quality
19
+ @data["quality"]
20
+ end
21
+
22
+ def duration
23
+ @data["duration"]
24
+ end
25
+
26
+ def time_to_sleep
27
+ @data[:full_data][1][0] - @data[:full_data][0][0]
28
+ end
29
+
30
+ def cycle_changes
31
+ @data[:full_data].size
32
+ end
33
+
34
+ private
35
+
36
+ def totals
37
+ # State codes: 1 = awake, 2 = light, 3 = deep
38
+ light, deep, awake = 0, 0, 0
39
+ state = 1
40
+ @data[:full_data].each_with_index do |set, i|
41
+ next if i == @data[:full_data].size - 1 # skip the last element
42
+
43
+ bucket_size = @data[:full_data][i + 1][0] - set[0]
44
+
45
+ case set[1]
46
+ when 1 then awake += bucket_size
47
+ when 2 then light += bucket_size
48
+ when 3 then deep += bucket_size
49
+ end
50
+ end
51
+
52
+ { light: light, deep: deep, awake: awake }
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,39 @@
1
+ Dir[File.dirname(__FILE__) + '/../lib/*.rb'].each { |file| require file }
2
+
3
+ describe Sleep do
4
+
5
+ let(:sleep) { Sleep.new({ :full_data => [[1325314448, 1], [1325315468, 3], [1325317748, 2], [1325318948, 3], [1325321948, 2], [1325323148, 3], [1325324048, 2], [1325325848, 3], [1325326748, 2], [1325328848, 3], [1325329448, 2], [1325332148, 3], [1325333348, 2], [1325334848, 3], [1325337848, 2], [1325339348, 3], [1325339948, 2], [1325342048, 3], [1325342888, 1]]}) }
6
+
7
+ describe "#deep_sleep" do
8
+
9
+ it "returns the right amount of deep sleep" do
10
+ sleep.deep_sleep.should == 13320
11
+ end
12
+
13
+ end
14
+
15
+ describe "#light_sleep" do
16
+
17
+ it "returns the right amount of light sleep" do
18
+ sleep.light_sleep.should == 14100
19
+ end
20
+
21
+ end
22
+
23
+ describe "#awake_sleep" do
24
+
25
+ it "returns the right amount of awake sleep" do
26
+ sleep.awake_sleep.should == 1020
27
+ end
28
+
29
+ end
30
+
31
+ describe "#time_to_sleep" do
32
+
33
+ it "returns the right amount of time it took to fall asleep" do
34
+ sleep.time_to_sleep.should == 1020
35
+ end
36
+
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jawbone
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andy Brett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70243209449600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70243209449600
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70243209446580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70243209446580
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70243209444220 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70243209444220
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70243209442280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70243209442280
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70243209439460 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70243209439460
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: &70243209438300 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70243209438300
80
+ description: A Ruby wrapper for the unofficial Jawbone UP API.
81
+ email:
82
+ - andy@andybrett.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - lib/jawbone.rb
88
+ - lib/session.rb
89
+ - lib/sleep.rb
90
+ - lib/daily_summary.rb
91
+ - spec/sleep_spec.rb
92
+ homepage: https://github.com/andrewpbrett/jawbone-up-api
93
+ licenses: []
94
+ post_install_message: ! "********************************************************************************\n\n
95
+ \ This is an **Unoffical** API - use at your own risk. See \n http://eric-blue.com/projects/up-api/
96
+ for more info and full documentation. \n\n********************************************************************************\n"
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.13
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Jawbone UP API wrapper
118
+ test_files:
119
+ - spec/sleep_spec.rb