mysportsfeeds-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4088910e542a2221ee24cb5b57d2ae99d3a3720b
4
+ data.tar.gz: 3c886b43fed4f6c550d2508d3990e45a396c219b
5
+ SHA512:
6
+ metadata.gz: ee6cef774e07efe209514ea4b5ccad87bb90a64893c667933d3a3dd04c3703d04cd2d90b9d3d79f6a24ca7aeea36510d3eae4b3a08f6798359f6d1675ff09db4
7
+ data.tar.gz: 4c1694de7bccccf7aa59e727b3f03ae1ce7e81b5a3079b293246caf0c7c9899d2200394c9e71bef9c5e968fe1b465d548df2c8e7e6fda0c039659de6239ad69a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mysportsfeeds-ruby.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012-2017 MySportsFeeds
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ # mysportsfeeds-ruby
2
+
3
+ MySportsFeeds Ruby Gem brought to you by [@MySportsFeeds](https://twitter.com/MySportsFeeds).
4
+
5
+ Makes use of the [MySportsFeeds API](https://www.mysportsfeeds.com) - a flexible, developer-friendly Sports Data API.
6
+
7
+ Free for Non-Commercial Use.
8
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mysportsfeeds/ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ require "mysportsfeeds/api/API_v1_0"
2
+
3
+ ### Main class for all interaction with the MySportsFeeds API
4
+ class MySportsFeeds
5
+
6
+ # Constructor
7
+ def initialize(version='1.0', verbose=False, store_type='file', store_location='results/')
8
+ __verify_version(version)
9
+ __verify_store(store_type, store_location)
10
+
11
+ @version = version
12
+ @verbose = verbose
13
+ @store_type = store_type
14
+ @store_location = store_location
15
+
16
+ # Instantiate an instance of the appropriate API depending on version
17
+ if @version == '1.0'
18
+ @api_instance = Mysportsfeeds::Api::API_v1_0.new(@verbose, @store_type, @store_location)
19
+ end
20
+ end
21
+
22
+ # Make sure the version is supported
23
+ def __verify_version(version)
24
+ if version != '1.0'
25
+ raise Exception.new("Unrecognized version specified. Supported versions are: '1.0'")
26
+ end
27
+ end
28
+
29
+ # Verify the type and location of the stored data
30
+ def __verify_store(store_type, store_location)
31
+ if !store_type.nil? and store_type != 'file'
32
+ raiseException.new("Unrecognized storage type specified. Supported values are: nil,'file'")
33
+ end
34
+
35
+ if store_type == 'file'
36
+ if store_location.nil?
37
+ raise Exception.new("Must specify a location for stored data.")
38
+ end
39
+ end
40
+ end
41
+
42
+ # Authenticate against the API (for v1.0)
43
+ def authenticate(username, password)
44
+ if !@api_instance.supports_basic_auth()
45
+ raise Exception.new("BASIC authentication not supported for version " + @version)
46
+ end
47
+
48
+ @api_instance.set_auth_credentials(username, password)
49
+ end
50
+
51
+ # Request data (and store it if applicable)
52
+ def msf_get_data(league, season, feed, output_format, *kwargs)
53
+ return @api_instance.get_data(league, season, feed, output_format, kwargs)
54
+ end
55
+ end
@@ -0,0 +1,251 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'openssl'
4
+ require 'json'
5
+ require 'fileutils'
6
+
7
+ require 'mysportsfeeds/version'
8
+
9
+ module Mysportsfeeds
10
+ module Api
11
+ # API class for dealing with v1.0 of the API
12
+ class API_v1_0
13
+
14
+ # Constructor
15
+ def initialize(verbose, store_type=nil, store_location=nil)
16
+ @base_uri = URI("https://www.mysportsfeeds.com/api/feed/pull")
17
+ @headers = {
18
+ "Accept-Encoding" => "gzip",
19
+ "User-Agent" => "MySportsFeeds Ruby/#{Mysportsfeeds::Ruby::VERSION} (#{RUBY_PLATFORM})"
20
+ }
21
+
22
+ @verbose = verbose
23
+ @store_type = store_type
24
+ @store_location = store_location
25
+
26
+ @valid_feeds = [
27
+ 'current_season',
28
+ 'cumulative_player_stats',
29
+ 'full_game_schedule',
30
+ 'daily_game_schedule',
31
+ 'daily_player_stats',
32
+ 'game_playbyplay',
33
+ 'game_boxscore',
34
+ 'scoreboard',
35
+ 'player_gamelogs',
36
+ 'team_gamelogs',
37
+ 'roster_players',
38
+ 'game_startinglineup',
39
+ 'active_players',
40
+ 'player_injuries',
41
+ 'latest_updates',
42
+ 'daily_dfs',
43
+ 'overall_team_standings',
44
+ 'conference_team_standings',
45
+ 'division_team_standings',
46
+ 'playoff_team_standings'
47
+ ]
48
+ end
49
+
50
+ # Verify a feed
51
+ def __verify_feed_name(feed)
52
+ is_valid = false
53
+
54
+ for value in @valid_feeds
55
+ if value == feed
56
+ is_valid = true
57
+ break
58
+ end
59
+ end
60
+
61
+ return is_valid
62
+ end
63
+
64
+ # Verify output format
65
+ def __verify_format(format)
66
+ is_valid = true
67
+
68
+ if format != 'json' and format != 'xml' and format != 'csv'
69
+ is_valid = false
70
+ end
71
+
72
+ return is_valid
73
+ end
74
+
75
+ # Feed URL (with only a league specified)
76
+ def __league_only_url(league, feed, output_format, params)
77
+ url "#{@base_uri.to_s}/#{league}/#{feed}.#{output_format}"
78
+
79
+ delim = "?"
80
+ params.each do |key, value|
81
+ url << delim << key << "=" << value
82
+ delim = "&"
83
+ end
84
+
85
+ return url
86
+ end
87
+
88
+ # Feed URL (with league + season specified)
89
+ def __league_and_season_url(league, season, feed, output_format, params)
90
+ url = "#{@base_uri.to_s}/#{league}/#{season}/#{feed}.#{output_format}"
91
+
92
+ delim = "?"
93
+ params.each do |key, value|
94
+ url << delim << key << "=" << value
95
+ delim = "&"
96
+ end
97
+
98
+ return url
99
+ end
100
+
101
+ # Generate the appropriate filename for a feed request
102
+ def __make_output_filename(league, season, feed, output_format, params)
103
+ filename = "#{feed}-#{league.downcase}-#{season}"
104
+
105
+ if params.key?("gameid")
106
+ filename << "-" << params["gameid"]
107
+ end
108
+
109
+ if params.key?("fordate")
110
+ filename << "-" << params["fordate"]
111
+ end
112
+
113
+ filename << "." << output_format
114
+
115
+ return filename
116
+ end
117
+
118
+ # Save a feed response based on the store_type
119
+ def __save_feed(response, league, season, feed, output_format, params)
120
+ # Save to memory regardless of selected method
121
+ if output_format == "json"
122
+ store_output = JSON.parse(response)
123
+ elsif output_format == "xml"
124
+ store_output = response
125
+ elsif output_format == "csv"
126
+ store_output = response
127
+ end
128
+
129
+ if @store_type == "file"
130
+ if !File.exist?(@store_location)
131
+ FileUtils::mkdir_p(@store_location)
132
+ end
133
+
134
+ filename = __make_output_filename(league, season, feed, output_format, params)
135
+
136
+ outfile = File.open(@store_location + filename, "w")
137
+ if output_format == "json" # This is JSON
138
+ json_out = response.to_json.gsub('\\', '').strip
139
+ outfile.write(json_out[1..json_out.size-2])
140
+
141
+ elsif output_format == "xml" # This is xml
142
+ outfile.write(store_output)
143
+
144
+ elsif output_format == "csv" # This is csv
145
+ outfile.write(store_output)
146
+
147
+ else
148
+ raise Exception.new("Could not interpret feed output format")
149
+ end
150
+ end
151
+ end
152
+
153
+ # Indicate this version does support BASIC auth
154
+ def supports_basic_auth()
155
+ return true
156
+ end
157
+
158
+ # Establish BASIC auth credentials
159
+ def set_auth_credentials(username, password)
160
+ @auth = {"username" => username, "password" => password}
161
+ end
162
+
163
+ # Request data (and store it if applicable)
164
+ def get_data(league, season, feed, output_format, kwargs)
165
+ if !@auth
166
+ raise Exception.new("You must authenticate() before making requests.")
167
+ end
168
+
169
+ # establish defaults for all variables
170
+ params = {}
171
+
172
+ # add force=false parameter (helps prevent unnecessary bandwidth use)
173
+ kwargs.each do |kwarg|
174
+ kwarg.each do |key, value|
175
+ params[key] = value
176
+ end
177
+ end
178
+
179
+ if !params.key?("force")
180
+ params['force'] = 'false'
181
+ end
182
+
183
+ if __verify_feed_name(feed) == false
184
+ raise Exception.new("Unknown feed '" + feed + "'.")
185
+ end
186
+
187
+ if __verify_format(output_format) == false
188
+ raise Exception.new("Unsupported format '" + output_format + "'.")
189
+ end
190
+
191
+ if feed == 'current_season'
192
+ url = __league_only_url(league, feed, output_format, params)
193
+ else
194
+ url = __league_and_season_url(league, season, feed, output_format, params)
195
+ end
196
+
197
+ if @verbose
198
+ puts "Making API request to '#{url}'."
199
+ puts " with headers:"
200
+ puts @headers
201
+ end
202
+
203
+ http = Net::HTTP.new(@base_uri.host, @base_uri.port)
204
+ http.use_ssl = true
205
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
206
+ request = Net::HTTP::Get.new(url)
207
+ request.basic_auth(@auth["username"], @auth["password"])
208
+
209
+ r = http.request(request)
210
+ if @verbose
211
+ puts "response = #{r}"
212
+ end
213
+
214
+ if r.code == "200"
215
+ if @store_type != nil
216
+ __save_feed(r.body, league, season, feed, output_format, params)
217
+ end
218
+
219
+ if output_format == "json"
220
+ data = JSON.parse(r.body)
221
+ elsif output_format == "xml"
222
+ data = r.body
223
+ else
224
+ data = r.body
225
+ end
226
+
227
+ elsif r.code == "304"
228
+ if @verbose
229
+ puts "Data hasn't changed since last call"
230
+ end
231
+
232
+ filename = __make_output_filename(league, season, feed, output_format, params)
233
+
234
+ f = File.read(@store_location + filename)
235
+ if output_format == "json"
236
+ data = JSON.parse(f)
237
+ elsif output_format == "xml"
238
+ data = f
239
+ else
240
+ data = f
241
+ end
242
+
243
+ else
244
+ puts "API call failed with error: #{r.code}"
245
+ end
246
+
247
+ return data
248
+ end
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,5 @@
1
+ module Mysportsfeeds
2
+ module Ruby
3
+ VERSION = "0.1.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mysportsfeeds/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysportsfeeds-ruby"
8
+ spec.version = Mysportsfeeds::Ruby::VERSION
9
+ spec.authors = ["MySportsFeeds", "Brad Barkhouse"]
10
+ spec.email = ["brad.barkhouse@mysportsfeeds.com"]
11
+ spec.required_ruby_version = '>= 2.2.0'
12
+
13
+ spec.summary = "MySportsFeeds API Ruby Client"
14
+ spec.description = "Easily retrieve sports data from the MySportsFeeds API"
15
+ spec.homepage = "https://github.com/MySportsFeeds/mysportsfeeds-ruby"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ # spec.add_runtime_dependency "fileutils", "~> 0.7"
24
+ # spec.add_runtime_dependency "json", "~> 2.1"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.2"
29
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysportsfeeds-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - MySportsFeeds
8
+ - Brad Barkhouse
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-06-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.15'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.15'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.2'
56
+ description: Easily retrieve sports data from the MySportsFeeds API
57
+ email:
58
+ - brad.barkhouse@mysportsfeeds.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/mysportsfeeds.rb
71
+ - lib/mysportsfeeds/api/API_v1_0.rb
72
+ - lib/mysportsfeeds/version.rb
73
+ - mysportsfeeds-ruby.gemspec
74
+ homepage: https://github.com/MySportsFeeds/mysportsfeeds-ruby
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.2.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.7
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: MySportsFeeds API Ruby Client
98
+ test_files: []