fantasydata 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. data/.gitignore +19 -0
  2. data/.travis.yml +13 -0
  3. data/Gemfile +17 -0
  4. data/Guardfile +70 -0
  5. data/LICENSE +22 -0
  6. data/README.md +32 -0
  7. data/Rakefile +2 -0
  8. data/fantasydata.gemspec +22 -0
  9. data/lib/fantasydata.rb +47 -0
  10. data/lib/fantasydata/api/box_score.rb +104 -0
  11. data/lib/fantasydata/api/bye_weeks.rb +24 -0
  12. data/lib/fantasydata/api/fantasy.rb +68 -0
  13. data/lib/fantasydata/api/game.rb +31 -0
  14. data/lib/fantasydata/api/news.rb +22 -0
  15. data/lib/fantasydata/api/player_details.rb +26 -0
  16. data/lib/fantasydata/api/player_stat.rb +33 -0
  17. data/lib/fantasydata/api/schedule.rb +13 -0
  18. data/lib/fantasydata/api/stadium.rb +14 -0
  19. data/lib/fantasydata/api/standings.rb +14 -0
  20. data/lib/fantasydata/api/team.rb +18 -0
  21. data/lib/fantasydata/api/utils.rb +65 -0
  22. data/lib/fantasydata/api/week.rb +22 -0
  23. data/lib/fantasydata/base.rb +91 -0
  24. data/lib/fantasydata/bye_week.rb +8 -0
  25. data/lib/fantasydata/client.rb +77 -0
  26. data/lib/fantasydata/configurable.rb +79 -0
  27. data/lib/fantasydata/daily_fantasy_player.rb +11 -0
  28. data/lib/fantasydata/daily_fantasy_player_stats.rb +35 -0
  29. data/lib/fantasydata/daily_fantasy_points.rb +8 -0
  30. data/lib/fantasydata/default.rb +82 -0
  31. data/lib/fantasydata/error.rb +33 -0
  32. data/lib/fantasydata/error/bad_gateway.rb +11 -0
  33. data/lib/fantasydata/error/bad_request.rb +10 -0
  34. data/lib/fantasydata/error/client_error.rb +24 -0
  35. data/lib/fantasydata/error/configuration_error.rb +8 -0
  36. data/lib/fantasydata/error/forbidden.rb +10 -0
  37. data/lib/fantasydata/error/gateway_timeout.rb +11 -0
  38. data/lib/fantasydata/error/internal_server_error.rb +11 -0
  39. data/lib/fantasydata/error/not_acceptable.rb +10 -0
  40. data/lib/fantasydata/error/not_found.rb +10 -0
  41. data/lib/fantasydata/error/server_error.rb +28 -0
  42. data/lib/fantasydata/error/service_unavailable.rb +11 -0
  43. data/lib/fantasydata/error/too_many_requests.rb +12 -0
  44. data/lib/fantasydata/error/unauthorized.rb +10 -0
  45. data/lib/fantasydata/error/unprocessable_entity.rb +10 -0
  46. data/lib/fantasydata/fantasy_player_adp.rb +10 -0
  47. data/lib/fantasydata/game_stat.rb +93 -0
  48. data/lib/fantasydata/news.rb +8 -0
  49. data/lib/fantasydata/player_detail.rb +27 -0
  50. data/lib/fantasydata/player_game_stat.rb +46 -0
  51. data/lib/fantasydata/player_season_stat.rb +54 -0
  52. data/lib/fantasydata/response/parse_json.rb +25 -0
  53. data/lib/fantasydata/response/raise_error.rb +32 -0
  54. data/lib/fantasydata/schedule.rb +14 -0
  55. data/lib/fantasydata/scoring_detail.rb +10 -0
  56. data/lib/fantasydata/stadium.rb +9 -0
  57. data/lib/fantasydata/standings.rb +13 -0
  58. data/lib/fantasydata/team_detail.rb +20 -0
  59. data/lib/fantasydata/version.rb +3 -0
  60. data/spec/fantasydata/api/box_score_spec.rb +17 -0
  61. data/spec/fantasydata/api/bye_weeks_spec.rb +31 -0
  62. data/spec/fantasydata/api/daily_fantasy_spec.rb +189 -0
  63. data/spec/fantasydata/api/game_spec.rb +84 -0
  64. data/spec/fantasydata/api/news_spec.rb +66 -0
  65. data/spec/fantasydata/api/player_details_spec.rb +107 -0
  66. data/spec/fantasydata/api/player_stat_spec.rb +138 -0
  67. data/spec/fantasydata/api/schedule_spec.rb +37 -0
  68. data/spec/fantasydata/api/stadium_spec.rb +32 -0
  69. data/spec/fantasydata/api/standings_spec.rb +30 -0
  70. data/spec/fantasydata/api/team_spec.rb +56 -0
  71. data/spec/fantasydata/api/week_spec.rb +69 -0
  72. data/spec/fantasydata/box_score_spec.rb +64 -0
  73. data/spec/fantasydata/client_spec.rb +77 -0
  74. data/spec/fantasydata/error_spec.rb +37 -0
  75. data/spec/fantasydata_spec.rb +64 -0
  76. data/spec/fixtures/bye_weeks/bye_weeks.json +1 -0
  77. data/spec/fixtures/daily_fantasy/adp.json +1 -0
  78. data/spec/fixtures/daily_fantasy/daily_defense_game.json +1 -0
  79. data/spec/fixtures/daily_fantasy/daily_defense_seasons_proj.json +1 -0
  80. data/spec/fixtures/daily_fantasy/daily_defense_seasons_stats.json +1 -0
  81. data/spec/fixtures/daily_fantasy/daily_players.json +1 -0
  82. data/spec/fixtures/daily_fantasy/daily_points.json +1 -0
  83. data/spec/fixtures/daily_fantasy/defense_game_stats_projected.json +1 -0
  84. data/spec/fixtures/error_response.json +4 -0
  85. data/spec/fixtures/game/game_stats_by_season.json +1 -0
  86. data/spec/fixtures/game/game_stats_by_week.json +1 -0
  87. data/spec/fixtures/game/in_progress.json +1 -0
  88. data/spec/fixtures/game/in_progress_false.json +1 -0
  89. data/spec/fixtures/game/in_progress_true.json +1 -0
  90. data/spec/fixtures/news/by_player.json +1 -0
  91. data/spec/fixtures/news/by_team.json +1 -0
  92. data/spec/fixtures/news/recent.json +1 -0
  93. data/spec/fixtures/player_details/active.json +1 -0
  94. data/spec/fixtures/player_details/by_team.json +1 -0
  95. data/spec/fixtures/player_details/free_agents.json +1 -0
  96. data/spec/fixtures/player_details/player_find.json +1 -0
  97. data/spec/fixtures/player_stat/stat_by_delta.json +1 -0
  98. data/spec/fixtures/player_stat/stat_by_game_projection.json +1 -0
  99. data/spec/fixtures/player_stat/stat_by_player_id.json +1 -0
  100. data/spec/fixtures/player_stat/stat_by_week.json +1 -0
  101. data/spec/fixtures/player_stat/stat_by_week_and_team.json +1 -0
  102. data/spec/fixtures/player_stat/stat_by_week_and_team_projected.json +1 -0
  103. data/spec/fixtures/schedule/for_year.json +1 -0
  104. data/spec/fixtures/stadium/index.json +1 -0
  105. data/spec/fixtures/standings/by_year.json +1 -0
  106. data/spec/fixtures/team/active.json +1 -0
  107. data/spec/fixtures/team/by_year.json +1 -0
  108. data/spec/fixtures/week/current.json +1 -0
  109. data/spec/fixtures/week/last_completed.json +1 -0
  110. data/spec/fixtures/week/upcoming.json +1 -0
  111. data/spec/helper.rb +42 -0
  112. metadata +242 -0
@@ -0,0 +1,31 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Game
6
+ include Fantasydata::API::Utils
7
+
8
+ # Returns true if there is at least one game being played at the time of the request or false if there are none.
9
+ #
10
+ # @see https://developer.fantasydata.com/docs/services/556a892e35491a1d30c7a8ba/operations/556a892f35491a02d05ebf3d
11
+ # @authentication Requires api key
12
+ # @raise [Fantasydata::Error::Unauthorized] Error raised when supplied api key is not valid.
13
+ # @raise [Fantasydata::Error::Unauthorized] Error raised when supplied user credentials are not valid.
14
+ # @return [boolean]
15
+ #
16
+ # @example games_in_progress
17
+ # Fantasydata.games_in_progress?
18
+ def games_in_progress?()
19
+ boolean_from_response(:get, '/nfl/v2/JSON/AreAnyGamesInProgress')
20
+ end
21
+
22
+ def game_stats_by_week(year, week)
23
+ objects_from_response(Fantasydata::GameStat, :get, "/nfl/v2/JSON/GameStatsByWeek/#{year}/#{week}")
24
+ end
25
+
26
+ def game_stats_by_season(year)
27
+ objects_from_response(Fantasydata::GameStat, :get, "/nfl/v2/JSON/GameStats/#{year}")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module News
6
+ include Fantasydata::API::Utils
7
+
8
+ def news_by_player_id(player_id)
9
+ objects_from_response(Fantasydata::News, :get, "/nfl/v2/JSON/NewsByPlayerID/#{player_id}")
10
+ end
11
+
12
+ def news_by_team(team_name)
13
+ objects_from_response(Fantasydata::News, :get, "/nfl/v2/JSON/NewsByTeam/#{team_name}")
14
+ end
15
+
16
+ def news_recent
17
+ objects_from_response(Fantasydata::News, :get, "/nfl/v2/JSON/News")
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module PlayerDetails
6
+ include Fantasydata::API::Utils
7
+
8
+ def player_details_available
9
+ objects_from_response(Fantasydata::PlayerDetail, :get, "/nfl/v2/JSON/Players")
10
+ end
11
+
12
+ def player_details_free_agents
13
+ objects_from_response(Fantasydata::PlayerDetail, :get, "/nfl/v2/JSON/FreeAgents")
14
+ end
15
+
16
+ def player_details_by_team(team_name)
17
+ objects_from_response(Fantasydata::PlayerDetail, :get, "/nfl/v2/JSON/Players/#{team_name}")
18
+ end
19
+
20
+ def player_details_by_player_id(player_id)
21
+ object_from_response(Fantasydata::PlayerDetail, :get, "/nfl/v2/JSON/Player/#{player_id.to_s}")
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module PlayerStat
6
+ include Fantasydata::API::Utils
7
+
8
+ def player_game_stat_by_player year, week, player_id
9
+ object_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameStatsByPlayerID/#{year}/#{week}/#{player_id}")
10
+ end
11
+
12
+ def player_game_stat_by_player_projection year, week, player_id
13
+ object_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameProjectionStatsByPlayerID/#{year}/#{week}/#{player_id}")
14
+ end
15
+
16
+ def player_game_stats_by_week_and_team year, week, team_name
17
+ objects_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameStatsByTeam/#{year}/#{week}/#{team_name}")
18
+ end
19
+
20
+ def player_game_stats_by_week_and_team_projection year, week, team_name
21
+ objects_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameProjectionStatsByTeam/#{year}/#{week}/#{team_name}")
22
+ end
23
+
24
+ def player_game_stats_by_week year, week
25
+ objects_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameStatsByWeek/#{year}/#{week}")
26
+ end
27
+
28
+ def player_game_stats_by_delta minutes
29
+ objects_from_response(Fantasydata::PlayerGameStat, :get, "/nfl/v2/JSON/PlayerGameStatsDelta/#{minutes}")
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Schedule
6
+ include Fantasydata::API::Utils
7
+
8
+ def schedule_by_year year
9
+ objects_from_response(Fantasydata::Schedule, :get, "/nfl/v2/JSON/Schedules/#{year}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Stadium
6
+ include Fantasydata::API::Utils
7
+
8
+ def stadiums
9
+ objects_from_response(Fantasydata::Stadium, :get, "/nfl/v2/JSON/Stadiums")
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Standings
6
+ include Fantasydata::API::Utils
7
+
8
+ def standings_by_year year
9
+ objects_from_response(Fantasydata::Standings, :get, "/nfl/v2/JSON/Standings/#{year}")
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Team
6
+ include Fantasydata::API::Utils
7
+
8
+ def teams_active
9
+ objects_from_response(Fantasydata::TeamDetail, :get, "/nfl/v2/JSON/Teams")
10
+ end
11
+
12
+ def teams_by_year year
13
+ objects_from_response(Fantasydata::TeamDetail, :get, "/nfl/v2/JSON/Teams/#{year}")
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ module Fantasydata
2
+ module API
3
+ module Utils
4
+
5
+ SUCCESS = true
6
+ MISSING_OR_INVALID_KEY = 1
7
+ API_KEY_CANNOT_CALL_THIS_METHOD = 2
8
+ RATE_LIMIT_EXCEEDED = 3
9
+ MISSING_PARAMETER = 4
10
+ INVALID_PARAMETER = 5
11
+
12
+ private
13
+
14
+ # @param klass [Class]
15
+ # @param request_method [Symbol]
16
+ # @param path [String]
17
+ # @param options [Hash]
18
+ # @return [Array]
19
+ def objects_from_response(klass, request_method, path)
20
+ response = send(request_method.to_sym, path)[:body] || []
21
+ objects_from_array(klass, response)
22
+ end
23
+
24
+ # @param klass [Class]
25
+ # @param array [Array]
26
+ # @return [Array]
27
+ def objects_from_array(klass, array)
28
+ array.map do |element|
29
+ klass.new(element)
30
+ end
31
+ end
32
+
33
+ # @param klass [Class]
34
+ # @param request_method [Symbol]
35
+ # @param path [String]
36
+ # @param object_key [Symbol]
37
+ # @param options [Hash]
38
+ # @return [Object]
39
+ def object_from_response(klass, request_method, path)
40
+ response = send(request_method.to_sym, path)[:body]
41
+ klass.new(response)
42
+ end
43
+
44
+ # @param klass [Class]
45
+ # @param request_method [Symbol]
46
+ # @param path [String]
47
+ # @param options [Hash]
48
+ # @return [Object]
49
+ def boolean_from_response(request_method, path)
50
+ response = send(request_method.to_sym, path)
51
+ response[:body] == SUCCESS
52
+ end
53
+
54
+ # @param klass [Class]
55
+ # @param request_method [Symbol]
56
+ # @param path [String]
57
+ # @param options [Hash]
58
+ # @return [Object]
59
+ def integer_from_response(request_method, path)
60
+ response = send(request_method.to_sym, path)
61
+ response[:body].to_i
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ require 'fantasydata/api/utils'
2
+
3
+ module Fantasydata
4
+ module API
5
+ module Week
6
+ include Fantasydata::API::Utils
7
+
8
+ def week_current
9
+ integer_from_response(:get, '/nfl/v2/JSON/CurrentWeek')
10
+ end
11
+
12
+ def week_last_completed
13
+ integer_from_response(:get, '/nfl/v2/JSON/LastCompletedWeek')
14
+ end
15
+
16
+ def week_upcoming
17
+ integer_from_response(:get, '/nfl/v2/XML/UpcomingWeek')
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,91 @@
1
+ module Fantasydata
2
+ class Base
3
+ # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
4
+ #
5
+ # @param attrs [Array, Set, Symbol]
6
+ def self.attr_reader(*attrs)
7
+ #@attr_readers ||= []
8
+ #@attr_readers.concat attrs
9
+ mod = Module.new do
10
+ attrs.each do |attribute|
11
+ define_method attribute do
12
+ @attrs[attribute.to_sym] if @attrs
13
+ end
14
+ define_method "#{attribute}?" do
15
+ !!@attrs[attribute.to_sym]
16
+ end
17
+ end
18
+ end
19
+ const_set(:Attributes, mod)
20
+ include mod
21
+ end
22
+
23
+ # @param other [Fantasydata::Base]
24
+ # @return [Boolean]
25
+ def ==(other)
26
+ super || attr_equal(:id, other) || attrs_equal(other)
27
+ end
28
+
29
+ # Initializes a new object
30
+ #
31
+ # @param attrs [Hash]
32
+ # @return [Fantasydata::Base]
33
+ def initialize(attrs={})
34
+ new_attrs = {}
35
+ attrs.to_hash.each_pair do |k,v|
36
+ new_attrs.merge!({underscore_key(k) => v})
37
+ end
38
+ @attrs = new_attrs
39
+ end
40
+
41
+ # Retrieve the attributes of an object
42
+ #
43
+ # @return [Hash]
44
+ def attrs
45
+ @attrs
46
+ end
47
+ alias to_hash attrs
48
+
49
+ # Update the attributes of an object
50
+ #
51
+ # @param attrs [Hash]
52
+ # @return [Fantasydata::Base]
53
+ def update(attrs)
54
+ @attrs.update(attrs)
55
+ self
56
+ end
57
+
58
+ protected
59
+
60
+ def underscore_key(str)
61
+ str.to_s.gsub(/::/, '/').
62
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
63
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
64
+ tr("-", "_").
65
+ downcase.to_sym
66
+ end
67
+
68
+ # @param attr [Symbol]
69
+ # @param other [Fantasydata::Base]
70
+ # @return [Boolean]
71
+ def attr_equal(attr, other)
72
+ self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
73
+ end
74
+
75
+ # @param other [Fantasydata::Base]
76
+ # @return [Boolean]
77
+ def attrs_equal(other)
78
+ self.class == other.class && !other.attrs.empty? && attrs == other.attrs
79
+ end
80
+
81
+ # @param klass [Class]
82
+ # @param key [Symbol]
83
+ # @return [Array]
84
+ def map_collection(klass, key)
85
+ Array(@attrs[key.to_sym]).map do |entity|
86
+ klass.new(entity)
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,8 @@
1
+ require 'fantasydata/base'
2
+
3
+ module Fantasydata
4
+ class ByeWeek < Fantasydata::Base
5
+ attr_reader :season, :week, :team
6
+
7
+ end
8
+ end
@@ -0,0 +1,77 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+ require 'fantasydata/configurable'
4
+ require 'fantasydata/api/game'
5
+ require 'fantasydata/api/box_score'
6
+ require 'fantasydata/api/bye_weeks'
7
+ require 'fantasydata/api/player_details'
8
+ require 'fantasydata/api/news'
9
+ require 'fantasydata/api/week'
10
+ require 'fantasydata/api/team'
11
+ require 'fantasydata/api/schedule'
12
+ require 'fantasydata/api/stadium'
13
+ require 'fantasydata/api/standings'
14
+ require 'fantasydata/api/player_stat'
15
+ require 'fantasydata/api/fantasy'
16
+
17
+ module Fantasydata
18
+ class Client
19
+ include Fantasydata::API::Game
20
+ include Fantasydata::API::ByeWeeks
21
+ include Fantasydata::API::BoxScore
22
+ include Fantasydata::API::Fantasy
23
+ include Fantasydata::API::News
24
+ include Fantasydata::API::PlayerDetails
25
+ include Fantasydata::API::PlayerStat
26
+ include Fantasydata::API::Team
27
+ include Fantasydata::API::Schedule
28
+ include Fantasydata::API::Stadium
29
+ include Fantasydata::API::Standings
30
+ include Fantasydata::API::Week
31
+ include Fantasydata::Configurable
32
+
33
+ # Initializes a new Client object
34
+ #
35
+ # @param options [Hash]
36
+ # @return [Fantasydata::Client]
37
+ def initialize(options={})
38
+ Fantasydata::Configurable.keys.each do |key|
39
+ instance_variable_set(:"@#{key}", options[key] || Fantasydata.instance_variable_get(:"@#{key}"))
40
+ end
41
+ end
42
+
43
+ # Perform an HTTP GET request
44
+ def get(path, params={})
45
+ request(:get, path, params)
46
+ end
47
+
48
+ # Perform an HTTP POST request
49
+ # def post(path, params={})
50
+ # signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
51
+ # request(:post, path, params, signature_params)
52
+ #end
53
+
54
+ private
55
+
56
+ def request(method, path, params={}, signature_params=params)
57
+ connection.headers['Ocp-Apim-Subscription-Key'] = @api_key
58
+ connection.send(method.to_sym, path, params).env
59
+ rescue Faraday::Error::ClientError
60
+ raise Fantasydata::Error::ClientError
61
+ rescue MultiJson::DecodeError
62
+ raise Fantasydata::Error::DecodeError
63
+ end
64
+
65
+ # Returns a Faraday::Connection object
66
+ #
67
+ # @return [Faraday::Connection]
68
+ def connection
69
+ @connection ||= begin
70
+ connection_options = {:builder => @middleware}
71
+ connection_options[:ssl] = {:verify => true} if @endpoint[0..4] == 'https'
72
+ Faraday.new(@endpoint, @connection_options.merge(connection_options))
73
+ end
74
+ end
75
+
76
+ end
77
+ end