Class: Smashrun

Inherits:
Object
  • Object
show all
Defined in:
lib/smashrun.rb

Constant Summary collapse

ID =
'id'
BRIEF =
'brief'
FULL =
'full'

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, token = nil, refresh_token = nil) ⇒ Smashrun

Returns a new instance of Smashrun.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#activities(options = {}) ⇒ Object



85
86
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
124
# 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) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# 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') ⇒ Object



55
56
57
# 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_tokenObject



64
65
66
67
# 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) ⇒ Object



59
60
61
62
# 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) ⇒ Object



69
70
71
# 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) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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_statusObject



73
74
75
76
77
78
79
80
81
82
83
# 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

#userinfoObject



140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# 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