jawbone-up 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ libdir = File.dirname(__FILE__)
2
+ require 'json'
3
+ require 'faraday'
4
+ require './lib/jawbone-up/error.rb'
5
+ require './lib/jawbone-up/config.rb'
6
+ require './lib/jawbone-up/response.rb'
7
+ require './lib/jawbone-up/session.rb'
8
+
9
+ module JawboneUP
10
+ @@adapter = :net_http
11
+
12
+ class << self
13
+ def api_url
14
+ 'https://jawbone.com'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module JawboneUP
2
+ class Config
3
+ attr_accessor :adapter, :logger, :use_hashie_mash, :throw_exceptions
4
+ def initialize(opts={})
5
+ self.use_hashie_mash ||= false
6
+ self.throw_exceptions ||= true
7
+ opts.each {|k,v| send("#{k}=", v)}
8
+ begin
9
+ require 'hashie' if self.use_hashie_mash && !defined?(Hashie::Mash)
10
+ rescue LoadError
11
+ raise Error, "You've requested Hashie::Mash, but the gem is not available. Don't set use_hashie_mash in your config, or install the hashie gem"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module JawboneUP
2
+ class ApiError < StandardError
3
+ attr_reader :status, :type, :reason
4
+ def initialize(status, type, reason=nil)
5
+ @status = status
6
+ @type = type
7
+ @reason = reason
8
+ message = type
9
+ message += " - #{reason}" if reason
10
+ message += " (#{status})"
11
+ super message
12
+ end
13
+ end
14
+
15
+ class Error < StandardError; end
16
+ class ArgumentError < ArgumentError; end
17
+ end
@@ -0,0 +1,10 @@
1
+ module JawboneUP
2
+ class Response
3
+ attr_reader :status, :headers, :body
4
+ def initialize(status, headers, body)
5
+ @status = status
6
+ @headers = headers
7
+ @body = body
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,142 @@
1
+ module JawboneUP
2
+ class Session
3
+ attr_reader :auth
4
+ attr_accessor :config
5
+
6
+ def initialize(opts={})
7
+ opts[:config] = JawboneUP::Config.new opts[:config] if opts[:config].is_a? Hash
8
+ @config = opts[:config] || JawboneUP::Config.new
9
+ self.auth = opts[:auth] || {}
10
+ self.auth[:token] = opts[:token] if opts[:token]
11
+ self.auth[:xid] = opts[:xid] if opts[:xid]
12
+
13
+ @connection = Faraday.new(:url => JawboneUP.api_url) do |builder|
14
+ builder.adapter :net_http
15
+ end
16
+ end
17
+
18
+ def auth=(hash)
19
+ @auth = hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
20
+ end
21
+
22
+ def token
23
+ @auth[:token]
24
+ end
25
+
26
+ def token?
27
+ !token.nil?
28
+ end
29
+
30
+ def xid
31
+ @auth[:xid]
32
+ end
33
+
34
+ def require_token
35
+ raise ApiError.new(400, "no_token", "You have not logged in yet") if token.nil?
36
+ end
37
+
38
+ #
39
+ # API methods
40
+ # See http://eric-blue.com/projects/up-api/ for more information
41
+ #
42
+
43
+ def signin(email, password)
44
+ result = self.post "/user/signin/login", {
45
+ :email => email,
46
+ :pwd => password,
47
+ :service => "nudge"
48
+ }
49
+ if !result['error'].nil? && !result['error']['msg'].nil?
50
+ msg = result['error']['msg']
51
+ else
52
+ msg = "Error logging in"
53
+ end
54
+ raise ApiError.new(400, "error", msg) if result['token'].nil?
55
+ @auth[:token] = result['token']
56
+ @auth[:xid] = result['user']['xid']
57
+ @auth[:user] = result['user']
58
+ return_response result
59
+ end
60
+
61
+ def get_sleep_summary(limit=nil, start_time=nil, end_time=nil)
62
+ require_token
63
+ params = {}
64
+ params[:limit] = limit unless limit.nil?
65
+ params[:start_time] = start_time unless start_time.nil?
66
+ result = self.get "/nudge/api/users/"+xid+"/sleeps", params
67
+ return_response result['data']
68
+ end
69
+
70
+ # Return either a Hashie::Mash object or the raw hash depending on config variable
71
+ def return_response(hash)
72
+ @config.use_hashie_mash ? Hashie::Mash.new(hash) : hash
73
+ end
74
+
75
+ #
76
+ # Raw HTTP methods
77
+ #
78
+
79
+ def get(path, query=nil, headers={})
80
+ response = execute :get, path, query, headers
81
+ hash = JSON.parse response.body
82
+ end
83
+
84
+ def post(path, query=nil, headers={})
85
+ response = execute :post, path, query, headers
86
+ hash = JSON.parse response.body
87
+ end
88
+
89
+ def execute(meth, path, query=nil, headers={})
90
+ query = Rack::Utils.parse_query query if query.is_a?(String)
91
+ headers = default_headers.merge! headers
92
+
93
+ if @config.logger
94
+ @config.logger.print "## PATH: #{path}\n\n"
95
+ @config.logger.print query.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
96
+ @config.logger.print "\n\n"
97
+ @config.logger.print headers.map{|k,v| "-H \"#{k}: #{v}\""}.join(" ")
98
+ @config.logger.print "\n\n"
99
+ end
100
+
101
+ raw = @connection.send(meth) do |req|
102
+ req.url "/#{path.gsub(/^\//, '')}"
103
+ req.headers = headers
104
+ if query
105
+ meth == :get ? req.params = query : req.body = URI.encode_www_form(query)
106
+ end
107
+ end
108
+
109
+ if raw.status != 200
110
+ begin
111
+ error = JSON.parse raw.body
112
+ raise JSON::ParserError.new if error['meta'].nil? || error['meta']['error_type'].nil?
113
+ raise ApiError.new(raw.status, error['meta']['error_type'], error['meta']['error_detail'])
114
+ rescue JSON::ParserError => e
115
+ raise ApiError.new(raw.status, "error", "Unknown API error") if raw.status != 200
116
+ end
117
+ end
118
+
119
+ if @config.logger
120
+ @config.logger.print "### JawboneUp::Session - #{meth.to_s.upcase} #{path}"
121
+ @config.logger.print "?#{Rack::Utils.build_query query}" unless query.nil?
122
+ @config.logger.print "\n### Request Headers: #{headers.inspect}"
123
+ @config.logger.print "### Status: #{raw.status}\n### Headers: #{raw.headers.inspect}\n###"
124
+ # @config.logger.puts "Body: #{raw.body}"
125
+ end
126
+
127
+ Response.new raw.status, raw.headers, raw.body
128
+ end
129
+
130
+ def default_headers
131
+ headers = {
132
+ 'User-Agent' => "Nudge/2.5.6 CFNetwork/609.1.4 Darwin/13.0.0",
133
+ # 'Content-Type' => 'application/json',
134
+ 'Accept' => 'application/json',
135
+ 'x-nudge-platform' => 'iPhone 5,2; 6.1.3',
136
+ 'Accept-Encoding' => 'plain'
137
+ }
138
+ headers['x-nudge-token'] = token if token
139
+ headers
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jawbone-up
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Parecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A client for the Jawbone UP service, as discovered by http://eric-blue.com/projects/up-api/
15
+ email: aaron@parecki.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/jawbone-up.rb
21
+ - lib/jawbone-up/response.rb
22
+ - lib/jawbone-up/session.rb
23
+ - lib/jawbone-up/error.rb
24
+ - lib/jawbone-up/config.rb
25
+ homepage: http://eric-blue.com/projects/up-api/
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.23
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Client for the Jawbone UP service
49
+ test_files: []
50
+ has_rdoc: