Class: Smashrun
- Inherits:
-
Object
- Object
- Smashrun
- Defined in:
- lib/smashrun.rb
Constant Summary collapse
- ID =
'id'
- BRIEF =
'brief'
- FULL =
'full'
Instance Method Summary collapse
- #activities(options = {}) ⇒ Object
- #badges(new = false) ⇒ Object
- #get_auth_url(scope = 'read_activity', redirect_uri = 'urn:ietf:wg:oauth:2.0:oob') ⇒ Object
- #get_refresh_token ⇒ Object
- #get_token(code) ⇒ Object
-
#initialize(client_id, client_secret, token = nil, refresh_token = nil) ⇒ Smashrun
constructor
A new instance of Smashrun.
- #revoke_token(token = nil, all_tokens = false) ⇒ Object
- #stats(year = nil, month = nil) ⇒ Object
- #token_status ⇒ Object
- #userinfo ⇒ Object
- #weights(latest = false) ⇒ Object
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(={}) defaults = { :activity_id => nil, :count => nil, :page => nil, :fromDate => nil, :level => nil } = defaults.merge() raise "No valid token found for client" if @token.nil? raise "Page can't be specified without a count" if not [:page].nil? and [:count].nil? # Don't allow searching options if activity_id is specified raise "fromDate not allowed with activity_id" if not [:activity_id].nil? and not [:fromDate].nil? raise "level not allowed with activity_id" if not [:activity_id].nil? and not [:level].nil? raise "page not allowed with activity_id" if not [:activity_id].nil? and not [:page].nil? raise "count not allowed with activity_id" if not [:activity_id].nil? and not [:count].nil? # Allow fromDate to be a date or a string/int representing epoch secs fromDate = [:fromDate] if fromDate.is_a? Date or fromDate.is_a? DateTime fromDate = fromDate.to_time.utc.to_i end params = {} params[:page] = [:page] unless [:page].nil? params[:count] = [:count] unless [:count].nil? params[:fromDate] = fromDate.to_s unless fromDate.nil? components = ['v1', 'my', 'activities'] if not [:activity_id].nil? components << [:activity_id] else components << 'search' components << 'ids' if [:level].nil? or [:level] == ID components << 'briefs' if [: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.(:redirect_uri => redirect_uri, :scope => scope) end |
#get_refresh_token ⇒ Object
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_status ⇒ Object
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 |
#userinfo ⇒ Object
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 |