smashrun 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/smashrun.rb +183 -0
  2. metadata +62 -0
@@ -0,0 +1,183 @@
1
+ # vim: filetype=ruby shiftwidth=2 tabstop=2 expandtab
2
+ #
3
+ # Copyright (c) 2016, Jon Nall
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # * Neither the name of tradervue-utils nor the names of its
17
+ # contributors may be used to endorse or promote products derived from
18
+ # this software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ require 'date'
32
+ require 'oauth2'
33
+ require 'json'
34
+
35
+ class Smashrun
36
+ ID = 'id'
37
+ BRIEF = 'brief'
38
+ FULL = 'full'
39
+
40
+ def initialize(client_id, client_secret, token=nil, refresh_token=nil)
41
+ @client_id = client_id
42
+ @client_secret = client_secret
43
+ @base_url = 'https://api.smashrun.com/v1'
44
+
45
+ oauth_params = { :site => 'https://api.smashrun.com',
46
+ :authorize_url => 'https://secure.smashrun.com/oauth2/authenticate',
47
+ :token_url => 'https://secure.smashrun.com/oauth2/token' }
48
+ @oauth = OAuth2::Client.new(@client_id, @client_secret, oauth_params)
49
+
50
+ if not token.nil?
51
+ @token = OAuth2::AccessToken.from_hash(@oauth, {:access_token => token, :refresh_token => refresh_token})
52
+ end
53
+ end
54
+
55
+ def get_auth_url(scope='read_activity', redirect_uri='urn:ietf:wg:oauth:2.0:oob')
56
+ return @oauth.auth_code.authorize_url(:redirect_uri => redirect_uri, :scope => scope)
57
+ end
58
+
59
+ def get_token(code)
60
+ @token = @oauth.auth_code.get_token(code)
61
+ return @token.token
62
+ end
63
+
64
+ def get_refresh_token()
65
+ raise "No valid token found for client" if @token.nil?
66
+ return @token.refresh_token
67
+ end
68
+
69
+ def revoke_token(token=nil, all_tokens=false)
70
+ raise "revoke_token isn't implemented yet"
71
+ end
72
+
73
+ def token_status()
74
+ raise "No valid token found for client" if @token.nil?
75
+
76
+ components = ['v1', 'auth', @token.token]
77
+ s = @token.get(components.join('/'))
78
+ if s.status == 200
79
+ return JSON.parse(s.body)
80
+ else
81
+ return nil
82
+ end
83
+ end
84
+
85
+ def activities(options={})
86
+ defaults = { :activity_id => nil, :count => nil, :page => nil, :fromDate => nil, :level => nil }
87
+ options = defaults.merge(options)
88
+
89
+ raise "No valid token found for client" if @token.nil?
90
+ raise "Page can't be specified without a count" if not options[:page].nil? and options[:count].nil?
91
+
92
+ # Don't allow searching options if activity_id is specified
93
+ raise "fromDate not allowed with activity_id" if not options[:activity_id].nil? and not options[:fromDate].nil?
94
+ raise "level not allowed with activity_id" if not options[:activity_id].nil? and not options[:level].nil?
95
+ raise "page not allowed with activity_id" if not options[:activity_id].nil? and not options[:page].nil?
96
+ raise "count not allowed with activity_id" if not options[:activity_id].nil? and not options[:count].nil?
97
+
98
+ # Allow fromDate to be a date or a string/int representing epoch secs
99
+ fromDate = options[:fromDate]
100
+ if fromDate.is_a? Date or fromDate.is_a? DateTime
101
+ fromDate = fromDate.to_time.utc.to_i
102
+ end
103
+
104
+ params = {}
105
+ params[:page] = options[:page] unless options[:page].nil?
106
+ params[:count] = options[:count] unless options[:count].nil?
107
+ params[:fromDate] = fromDate.to_s unless fromDate.nil?
108
+
109
+ components = ['v1', 'my', 'activities']
110
+ if not options[:activity_id].nil?
111
+ components << options[:activity_id]
112
+ else
113
+ components << 'search'
114
+ components << 'ids' if options[:level].nil? or options[:level] == ID
115
+ components << 'briefs' if options[:level] == BRIEF
116
+ end
117
+
118
+ s = @token.get(components.join('/'), {:params => params})
119
+ if s.status == 200
120
+ return JSON.parse(s.body)
121
+ else
122
+ return nil
123
+ end
124
+ end
125
+
126
+ def weights(latest=false)
127
+ raise "No valid token found for client" if @token.nil?
128
+
129
+ components = ['v1', 'my', 'body', 'weight']
130
+ components << 'latest' if latest
131
+
132
+ s = @token.get(components.join('/'))
133
+ if s.status == 200
134
+ return JSON.parse(s.body)
135
+ else
136
+ return nil
137
+ end
138
+ end
139
+
140
+ def userinfo()
141
+ raise "No valid token found for client" if @token.nil?
142
+
143
+ components = ['v1', 'my', 'userinfo']
144
+ s = @token.get(components.join('/'))
145
+ if s.status == 200
146
+ return JSON.parse(s.body)
147
+ else
148
+ return nil
149
+ end
150
+ end
151
+
152
+ def badges(new=false)
153
+ raise "No valid token found for client" if @token.nil?
154
+
155
+ components = ['v1', 'my', 'badges']
156
+ components << 'new' if new
157
+
158
+ s = @token.get(components.join('/'))
159
+ if s.status == 200
160
+ return JSON.parse(s.body)
161
+ else
162
+ return nil
163
+ end
164
+ end
165
+
166
+ def stats(year=nil, month=nil)
167
+ raise "No valid token found for client" if @token.nil?
168
+ raise "Must specify a non-nil year if month is not nil" if year.nil? and not month.nil?
169
+
170
+ components = ['v1', 'my', 'stats']
171
+ components << year.to_s unless year.nil?
172
+ components << month.to_s unless month.nil?
173
+
174
+ s = @token.get(components.join('/'))
175
+ if s.status == 200
176
+ return JSON.parse(s.body)
177
+ else
178
+ return nil
179
+ end
180
+ end
181
+
182
+ end
183
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smashrun
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon Nall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth2
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ description: Implements authentication, reading and writing for the Smashrun API (http://smashrun.com)
31
+ email: jon.nall@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/smashrun.rb
37
+ homepage: https://github.com/nall/smashrun-ruby
38
+ licenses:
39
+ - BSD-3-Clause
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.23
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Ruby wrapper for the Smashrun API
62
+ test_files: []