Skip to content

Commit

Permalink
Adding RunList and Run classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fixlr committed Jul 13, 2008
1 parent b3ee511 commit dd8d03e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/nikeplus.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/nikeplus/base.rb
Expand Up @@ -23,5 +23,9 @@ def initialize(login, password)
def whoami
@session.profile.login
end

def runs
@runs ||= RunList.new(@session)
end
end
end
14 changes: 10 additions & 4 deletions lib/nikeplus/http_response.rb
Expand Up @@ -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
12 changes: 12 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
15 changes: 8 additions & 7 deletions lib/nikeplus/session.rb
Expand Up @@ -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?
Expand All @@ -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)
Expand Down

0 comments on commit dd8d03e

Please sign in to comment.