Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding RunList and Run classes

  • Loading branch information...
commit dd8d03e28919d84559328d4ff105eb1ec34ec7e5 1 parent b3ee511
@fixlr authored
View
2  lib/nikeplus.rb
@@ -4,6 +4,6 @@
require 'rexml/document'
require 'cgi'
-%w{base session http_response profile}.each do |file|
+%w{base session http_response profile run_list run}.each do |file|
require File.join(File.dirname(__FILE__), 'nikeplus', file)
end
View
4 lib/nikeplus/base.rb
@@ -23,5 +23,9 @@ def initialize(login, password)
def whoami
@session.profile.login
end
+
+ def runs
+ @runs ||= RunList.new(@session)
+ end
end
end
View
14 lib/nikeplus/http_response.rb
@@ -6,24 +6,30 @@ class HttpResponse
RESP_FAILURE = 'failure'
def initialize(response)
- @set_cookie = response.fetch('set-cookie')
+ @set_cookie = response.fetch('set-cookie') rescue IndexError
@body = REXML::Document.new(response.body)
end
def error_code
- @body.root.elements["exceptions/error/errorcode"].text
+ fetch("exceptions/error/errorcode").text
end
def error_message
- @body.root.elements["exceptions/error/message"].text
+ fetch("exceptions/error/message").text
end
def response_status
- @body.root.elements["status"].text
+ fetch("status").text
end
def success?
response_status == RESP_SUCCESS
end
+
+ # Return the text from an XML node in the response
+ def fetch(key)
+ raise "Node #{key} was not found" if @body.root.elements["#{key}"].nil?
+ @body.root.elements["#{key}"]
+ end
end
end
View
12 lib/nikeplus/run.rb
@@ -0,0 +1,12 @@
+module NikePlus
+ class Run
+ attr_accessor :id, :workoutType, :startTime, :distance, :duration,
+ :syncTime, :calories, :name, :description
+
+ def initialize(run)
+ run.each_element_with_text do |e|
+ send("#{e.name}=", e.text)
+ end
+ end
+ end
+end
View
15 lib/nikeplus/run_list.rb
@@ -0,0 +1,15 @@
+module NikePlus
+ class RunList
+ RUNS_URL = "https://secure-nikeplus.nike.com/nikeplus/v1/services/app/run_list.jhtml"
+
+ def initialize(session)
+ @session = session
+ @runs = []
+
+ response = @session.send_request(RUNS_URL)
+ response.fetch('runList').each_element_with_text do |run_xml|
+ @runs << Run.new(run_xml)
+ end
+ end
+ end
+end
View
15 lib/nikeplus/session.rb
@@ -12,15 +12,16 @@ def initialize
end
def authenticate(login, password)
- response = send_request(AUTH_URL, 'login', {:login=>login, :password=>password, :locale=>'en&5FUS'})
+ response = send_request(AUTH_URL, {:action => 'login', :login=>login, :password=>password, :locale=>'en&5FUS'})
@cookie = response.set_cookie
- @profile = NikePlus::Profile.new(response.body.root.elements["profile"])
+ @profile = NikePlus::Profile.new(response.fetch("profile"))
end
- def send_request(endpoint, action, options = {})
- uri = parse(endpoint, options.merge(:action => action))
+ def send_request(endpoint, options = {})
+ uri = URI.parse("#{endpoint}#{parse_opts(options)}")
+ params = @cookie.nil? ? [uri.request_uri] : [uri.request_uri, {'Cookie' => @cookie}]
resp = http(uri).start do |sess|
- NikePlus::HttpResponse.new(sess.get(uri.request_uri))
+ NikePlus::HttpResponse.new(sess.get(*params))
end
if resp.success?
@@ -31,8 +32,8 @@ def send_request(endpoint, action, options = {})
end
private
- def parse(endpoint, options)
- URI.parse("#{endpoint}?#{options.collect{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')}")
+ def parse_opts(options)
+ options.length > 0 ? "?#{options.collect{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')}" : ""
end
def http(uri)
Please sign in to comment.
Something went wrong with that request. Please try again.