class Smashrun

Constants

BRIEF
FULL
ID

Public Class Methods

new(client_id, client_secret, token=nil, refresh_token=nil) click to toggle source
# File lib/smashrun.rb, line 40
def initialize(client_id, client_secret, token=nil, refresh_token=nil)
  @client_id = client_id
  @client_secret = client_secret
  @base_url = 'https://api.smashrun.com/v1'

  oauth_params = { :site => 'https://api.smashrun.com',
          :authorize_url => 'https://secure.smashrun.com/oauth2/authenticate',
              :token_url => 'https://secure.smashrun.com/oauth2/token' }
  @oauth = OAuth2::Client.new(@client_id, @client_secret, oauth_params)

  if not token.nil?
    @token = OAuth2::AccessToken.from_hash(@oauth, {:access_token => token, :refresh_token => refresh_token})
  end
end

Public Instance Methods

activities(options={}) click to toggle source
# File lib/smashrun.rb, line 85
def activities(options={})
  defaults = { :activity_id => nil, :count => nil, :page => nil, :fromDate => nil, :level => nil }
  options = defaults.merge(options)

  raise "No valid token found for client" if @token.nil?
  raise "Page can't be specified without a count" if not options[:page].nil? and options[:count].nil?

  # Don't allow searching options if activity_id is specified
  raise "fromDate not allowed with activity_id" if not options[:activity_id].nil? and not options[:fromDate].nil?
  raise "level not allowed with activity_id" if not options[:activity_id].nil? and not options[:level].nil?
  raise "page not allowed with activity_id" if not options[:activity_id].nil? and not options[:page].nil?
  raise "count not allowed with activity_id" if not options[:activity_id].nil? and not options[:count].nil?

  # Allow fromDate to be a date or a string/int representing epoch secs
  fromDate = options[:fromDate]
  if fromDate.is_a? Date or fromDate.is_a? DateTime
    fromDate = fromDate.to_time.utc.to_i
  end

  params = {}
  params[:page] = options[:page] unless options[:page].nil?
  params[:count] = options[:count] unless options[:count].nil?
  params[:fromDate] = fromDate.to_s unless fromDate.nil?

  components = ['v1', 'my', 'activities']
  if not options[:activity_id].nil?
    components << options[:activity_id]
  else
    components << 'search'
    components << 'ids' if options[:level].nil? or options[:level] == ID
    components << 'briefs' if options[:level] == BRIEF
  end

  s = @token.get(components.join('/'), {:params => params})
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end
badges(new=false) click to toggle source
# File lib/smashrun.rb, line 152
def badges(new=false)
  raise "No valid token found for client" if @token.nil?

  components = ['v1', 'my', 'badges']
  components << 'new' if new

  s = @token.get(components.join('/'))
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end
get_auth_url(scope='read_activity', redirect_uri='urn:ietf:wg:oauth:2.0:oob') click to toggle source
# File lib/smashrun.rb, line 55
def get_auth_url(scope='read_activity', redirect_uri='urn:ietf:wg:oauth:2.0:oob')
  return @oauth.auth_code.authorize_url(:redirect_uri => redirect_uri, :scope => scope)
end
get_refresh_token() click to toggle source
# File lib/smashrun.rb, line 64
def get_refresh_token()
  raise "No valid token found for client" if @token.nil?
  return @token.refresh_token
end
get_token(code) click to toggle source
# File lib/smashrun.rb, line 59
def get_token(code)
  @token = @oauth.auth_code.get_token(code)
  return @token.token
end
revoke_token(token=nil, all_tokens=false) click to toggle source
# File lib/smashrun.rb, line 69
def revoke_token(token=nil, all_tokens=false)
  raise "revoke_token isn't implemented yet"
end
stats(year=nil, month=nil) click to toggle source
# File lib/smashrun.rb, line 166
def stats(year=nil, month=nil)
  raise "No valid token found for client" if @token.nil?
  raise "Must specify a non-nil year if month is not nil" if year.nil? and not month.nil?

  components = ['v1', 'my', 'stats']
  components << year.to_s unless year.nil?
  components << month.to_s unless month.nil?

  s = @token.get(components.join('/'))
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end
token_status() click to toggle source
# File lib/smashrun.rb, line 73
def token_status()
  raise "No valid token found for client" if @token.nil?

  components = ['v1', 'auth', @token.token]
  s = @token.get(components.join('/'))
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end
userinfo() click to toggle source
# File lib/smashrun.rb, line 140
def userinfo()
  raise "No valid token found for client" if @token.nil?

  components = ['v1', 'my', 'userinfo']
  s = @token.get(components.join('/'))
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end
weights(latest=false) click to toggle source
# File lib/smashrun.rb, line 126
def weights(latest=false)
  raise "No valid token found for client" if @token.nil?

  components = ['v1', 'my', 'body', 'weight']
  components << 'latest' if latest

  s = @token.get(components.join('/'))
  if s.status == 200
    return JSON.parse(s.body)
  else
    return nil
  end
end