Class: JawboneUP::Session
- Inherits:
-
Object
- Object
- JawboneUP::Session
- Defined in:
- lib/jawbone-up/session.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #default_headers ⇒ Object
- #execute(meth, path, query = nil, headers = {}) ⇒ Object
-
#get(path, query = nil, headers = {}) ⇒ Object
Raw HTTP methods.
- #get_sleep_summary(limit = nil, start_time = nil, end_time = nil) ⇒ Object
-
#initialize(opts = {}) ⇒ Session
constructor
A new instance of Session.
- #post(path, query = nil, headers = {}) ⇒ Object
- #require_token ⇒ Object
-
#return_response(hash) ⇒ Object
Return either a Hashie::Mash object or the raw hash depending on config variable.
-
#signin(email, password) ⇒ Object
API methods See eric-blue.com/projects/up-api/ for more information.
- #token ⇒ Object
- #token? ⇒ Boolean
- #xid ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Session
Returns a new instance of Session.
8 9 10 11 12 13 14 |
# File 'lib/jawbone-up/session.rb', line 8 def initialize(opts={}) opts[:config] = JawboneUP::Config.new opts[:config] if opts[:config].is_a? Hash @config = opts[:config] || JawboneUP::Config.new self.auth = opts[:auth] || {} self.auth[:token] = opts[:token] if opts[:token] self.auth[:xid] = opts[:xid] if opts[:xid] end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
5 6 7 |
# File 'lib/jawbone-up/session.rb', line 5 def auth @auth end |
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/jawbone-up/session.rb', line 6 def config @config end |
Instance Method Details
#default_headers ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/jawbone-up/session.rb', line 125 def default_headers headers = { 'User-Agent' => "Nudge/2.5.6 CFNetwork/609.1.4 Darwin/13.0.0", # 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'x-nudge-platform' => 'iPhone 5,2; 6.1.3', 'Accept-Encoding' => 'plain' } headers['x-nudge-token'] = token if token headers end |
#execute(meth, path, query = nil, headers = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jawbone-up/session.rb', line 87 def execute(meth, path, query=nil, headers={}) query = CGI::parse(query) if query.is_a?(String) headers = default_headers.merge! headers if @config.logger @config.logger.print "## PATH: #{path}\n\n" @config.logger.print query.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&") @config.logger.print "\n\n" @config.logger.print headers.map{|k,v| "-H \"#{k}: #{v}\""}.join(" ") @config.logger.print "\n\n" end if meth == :get response = RestClient.get "#{JawboneUP.api_url}/#{path.gsub(/^\//, '')}", headers.merge({:params => query}) else response = RestClient.post "#{JawboneUP.api_url}/#{path.gsub(/^\//, '')}", query, headers end if response.code != 200 begin error = JSON.parse response.to_str raise JSON::ParserError.new if error['meta'].nil? || error['meta']['error_type'].nil? raise ApiError.new(response.code, error['meta']['error_type'], error['meta']['error_detail']) rescue JSON::ParserError => e raise ApiError.new(response.code, "error", "Unknown API error") if response.code != 200 end end if @config.logger @config.logger.print "### JawboneUp::Session - #{meth.to_s.upcase} #{path}" @config.logger.print query.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&") @config.logger.print "\n### Request Headers: #{headers.inspect}" @config.logger.print "### Status: #{response.code}\n### Headers: #{response.headers.inspect}\n###" end Response.new response.code, response.headers, response.to_str end |
#get(path, query = nil, headers = {}) ⇒ Object
Raw HTTP methods
77 78 79 80 |
# File 'lib/jawbone-up/session.rb', line 77 def get(path, query=nil, headers={}) response = execute :get, path, query, headers hash = JSON.parse response.body end |
#get_sleep_summary(limit = nil, start_time = nil, end_time = nil) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/jawbone-up/session.rb', line 59 def get_sleep_summary(limit=nil, start_time=nil, end_time=nil) require_token params = {} params[:limit] = limit unless limit.nil? params[:start_time] = start_time unless start_time.nil? result = self.get "/nudge/api/users/"+xid+"/sleeps", params return_response result['data'] end |
#post(path, query = nil, headers = {}) ⇒ Object
82 83 84 85 |
# File 'lib/jawbone-up/session.rb', line 82 def post(path, query=nil, headers={}) response = execute :post, path, query, headers hash = JSON.parse response.body end |
#require_token ⇒ Object
32 33 34 |
# File 'lib/jawbone-up/session.rb', line 32 def require_token raise ApiError.new(400, "no_token", "You have not logged in yet") if token.nil? end |
#return_response(hash) ⇒ Object
Return either a Hashie::Mash object or the raw hash depending on config variable
69 70 71 |
# File 'lib/jawbone-up/session.rb', line 69 def return_response(hash) @config.use_hashie_mash ? Hashie::Mash.new(hash) : hash end |
#signin(email, password) ⇒ Object
API methods See eric-blue.com/projects/up-api/ for more information
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jawbone-up/session.rb', line 41 def signin(email, password) result = self.post "/user/signin/login", { :email => email, :pwd => password, :service => "nudge" } if !result['error'].nil? && !result['error']['msg'].nil? msg = result['error']['msg'] else msg = "Error logging in" end raise ApiError.new(400, "error", msg) if result['token'].nil? @auth[:token] = result['token'] @auth[:xid] = result['user']['xid'] @auth[:user] = result['user'] return_response result end |
#token ⇒ Object
20 21 22 |
# File 'lib/jawbone-up/session.rb', line 20 def token @auth[:token] end |
#token? ⇒ Boolean
24 25 26 |
# File 'lib/jawbone-up/session.rb', line 24 def token? !token.nil? end |
#xid ⇒ Object
28 29 30 |
# File 'lib/jawbone-up/session.rb', line 28 def xid @auth[:xid] end |