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,79 @@
1
+ require 'fantasydata/default'
2
+ require 'fantasydata/error/configuration_error'
3
+ require 'forwardable'
4
+
5
+ module Fantasydata
6
+ module Configurable
7
+ extend Forwardable
8
+ attr_writer :api_key
9
+ attr_accessor :endpoint, :connection_options, :middleware
10
+ def_delegator :options, :hash
11
+
12
+ class << self
13
+ def keys
14
+ @keys ||= [
15
+ :api_key,
16
+ :endpoint,
17
+ :connection_options,
18
+ :middleware,
19
+ ]
20
+ end
21
+ end
22
+
23
+ # Convenience method to allow configuration options to be set in a block
24
+ #
25
+ # @raise [Fantasydata::Error::ConfigurationError] Error is raised when supplied
26
+ # echonest credentials are not a String or Symbol.
27
+ def configure
28
+ yield self
29
+ validate_credential_type!
30
+ self
31
+ end
32
+
33
+ # @return [Boolean]
34
+ def credentials?
35
+ credentials.values.all?
36
+ end
37
+
38
+ def reset!
39
+ Fantasydata::Configurable.keys.each do |key|
40
+ instance_variable_set(:"@#{key}", Fantasydata::Default.options[key])
41
+ end
42
+ self
43
+ end
44
+ alias setup reset!
45
+
46
+ private
47
+ def application_only_auth?
48
+ true
49
+ end
50
+
51
+ # @return [Hash]
52
+ def credentials
53
+ {
54
+ :api_key => @api_key
55
+ }
56
+ end
57
+
58
+ # @return [Hash]
59
+ def options
60
+ Hash[Fantasydata::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
61
+ end
62
+
63
+ # Ensures that all credentials set during configuration are of a
64
+ # valid type. Valid types are String and Symbol.
65
+ #
66
+ # @raise [Fantasydata::Error::ConfigurationError] Error is raised when
67
+ # supplied echonest credentials are not a String or Symbol.
68
+ def validate_credential_type!
69
+ credentials.each do |credential, value|
70
+ next if value.nil?
71
+
72
+ unless value.is_a?(String) || value.is_a?(Symbol)
73
+ raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,11 @@
1
+ require 'fantasydata/base'
2
+
3
+ module Fantasydata
4
+ class DailyFantasyPlayer < Fantasydata::Base
5
+ attr_reader :player_id, :date, :short_name, :name, :team, :opponent,
6
+ :position, :salary, :last_game_fantasy_points,
7
+ :projected_fantasy_points, :opponent_rank,
8
+ :opponent_position_rank, :status, :status_code, :status_color
9
+
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ require 'fantasydata/base'
2
+
3
+ module Fantasydata
4
+ class DailyFantasyPlayerStats < Fantasydata::Base
5
+ attr_reader :scoring_details, :game_key, :season_type, :season,
6
+ :week, :date, :team, :opponent, :points_allowed,
7
+ :touchdowns_scored, :solo_tackles, :assisted_tackles,
8
+ :sacks, :sack_yards, :passes_defended, :fumbles_forced,
9
+ :fumbles_recovered, :fumble_return_yards,
10
+ :fumble_return_touchdowns, :interceptions,
11
+ :interception_return_yards, :interception_return_touchdowns,
12
+ :blocked_kicks, :safeties, :punt_returns, :punt_return_yards,
13
+ :punt_return_touchdowns, :punt_return_long, :kick_returns,
14
+ :kick_return_yards, :kick_return_touchdowns, :kick_return_long,
15
+ :blocked_kick_return_touchdowns, :field_goal_return_touchdowns,
16
+ :fantasy_points_allowed, :quarterback_fantasy_points_allowed,
17
+ :runningback_fantasy_points_allowed, :wide_receiver_fantasy_points_allowed,
18
+ :tight_end_fantasy_points_allowed, :kicker_fantasy_points_allowed,
19
+ :blocked_kick_return_yards, :field_goal_return_yards,
20
+ :quarterback_hits, :tackles_for_loss, :defensive_touchdowns,
21
+ :special_teams_touchdowns, :is_game_over, :fantasy_points,
22
+ :stadium, :temperature, :humidity, :wind_speed, :third_down_attempts,
23
+ :third_down_conversions, :fourth_down_attempts, :fourth_down_conversions,
24
+ :points_allowed_by_defense_special_teams, :fan_duel_salary,
25
+ :draft_kings_salary, :fantasy_data_salary, :victiv_salary
26
+
27
+ def scoring_details
28
+ @scoring_details ||= map_collection(Fantasydata::ScoringDetail, :scoring_details)
29
+ end
30
+
31
+ end
32
+ end
33
+
34
+
35
+
@@ -0,0 +1,8 @@
1
+ require 'fantasydata/base'
2
+
3
+ module Fantasydata
4
+ class DailyFantasyPoints < Fantasydata::Base
5
+ attr_reader :player_id, :name, :fantasy_points, :has_started,
6
+ :is_in_progress, :is_over, :date
7
+ end
8
+ end
@@ -0,0 +1,82 @@
1
+ require 'faraday'
2
+ require 'faraday/request/multipart'
3
+ require 'fantasydata/configurable'
4
+ require 'fantasydata/error/client_error'
5
+ require 'fantasydata/error/server_error'
6
+ require 'fantasydata/response/parse_json'
7
+ require 'fantasydata/response/raise_error'
8
+ require 'fantasydata/version'
9
+
10
+ module Fantasydata
11
+ module Default
12
+ ENDPOINT = 'http://api.nfldata.apiphany.com' unless defined? Fantasydata::Default::ENDPOINT
13
+ CONNECTION_OPTIONS = {
14
+ :headers => {
15
+ :accept => 'application/json',
16
+ :user_agent => "FantasyData Ruby Gem #{Fantasydata::VERSION}"
17
+ },
18
+ :request => {
19
+ :open_timeout => 5,
20
+ :timeout => 10,
21
+ :params_encoder => Faraday::FlatParamsEncoder
22
+ },
23
+ :ssl => {
24
+ :verify => false
25
+ },
26
+ } unless defined? Fantasydata::Default::CONNECTION_OPTIONS
27
+ IDENTITY_MAP = false unless defined? Fantasydata::Default::IDENTITY_MAP
28
+ MIDDLEWARE = Faraday::RackBuilder.new do |builder|
29
+ # Checks for files in the payload
30
+ builder.use Faraday::Request::Multipart
31
+
32
+ # Convert request params to "www-form-urlencoded"
33
+ builder.use Faraday::Request::UrlEncoded
34
+
35
+ # Handle 4xx server responses
36
+ builder.use Fantasydata::Response::RaiseError, Fantasydata::Error::ClientError
37
+
38
+ # Parse JSON response bodies using MultiJson
39
+ builder.use Fantasydata::Response::ParseJson
40
+
41
+ # Handle 5xx server responses
42
+ builder.use Fantasydata::Response::RaiseError, Fantasydata::Error::ServerError
43
+
44
+ # builder.use Faraday::Response::Logger
45
+
46
+ # Set Faraday's HTTP adapter
47
+ builder.adapter Faraday.default_adapter
48
+ end unless defined? Fantasydata::Default::MIDDLEWARE
49
+
50
+ class << self
51
+
52
+ # @return [Hash]
53
+ def options
54
+ Hash[Fantasydata::Configurable.keys.map{|key| [key, send(key)]}]
55
+ end
56
+
57
+ # @return [String]
58
+ def api_key
59
+ ENV['FANTASYDATA_API_KEY']
60
+ end
61
+
62
+ # @note This is configurable in case you want to use a Fantasydata-compatible endpoint.
63
+ # @return [String]
64
+ def endpoint
65
+ ENDPOINT
66
+ end
67
+
68
+ def connection_options
69
+ CONNECTION_OPTIONS
70
+ end
71
+
72
+ # @note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.
73
+ # @see https://github.com/technoweenie/faraday#advanced-middleware-usage
74
+ # @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
75
+ # @return [Faraday::RackBuilder]
76
+ def middleware
77
+ MIDDLEWARE
78
+ end
79
+
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,33 @@
1
+ module Fantasydata
2
+ # Custom error class for rescuing from all Fantasydata errors
3
+ class Error < StandardError
4
+ attr_reader :wrapped_exception
5
+ # attr_reader :rate_limit, :wrapped_exception
6
+
7
+ # @return [Hash]
8
+ def self.errors
9
+ @errors ||= Hash[descendants.map{|klass| [klass.const_get(:HTTP_STATUS_CODE), klass]}]
10
+ end
11
+
12
+ # @return [Array]
13
+ def self.descendants
14
+ ObjectSpace.each_object(::Class).select{|klass| klass < self}
15
+ end
16
+
17
+ # Initializes a new Error object
18
+ #
19
+ # @param exception [Exception, String]
20
+ # @param response_headers [Hash]
21
+ # @return [Echowrap::Error]
22
+ def initialize(exception=$!, response_headers={})
23
+ # @rate_limit = Fantasydata::RateLimit.new(response_headers)
24
+ @wrapped_exception = exception
25
+ exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
26
+ end
27
+
28
+ def backtrace
29
+ @wrapped_exception.respond_to?(:backtrace) ? @wrapped_exception.backtrace : super
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'fantasydata/error/server_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Echowrap returns the HTTP status code 502
6
+ class BadGateway < Fantasydata::Error::ServerError
7
+ HTTP_STATUS_CODE = 502
8
+ MESSAGE = "Fantasydata is down or being upgraded."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Echowrap returns the HTTP status code 400
6
+ class BadRequest < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 400
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ require 'fantasydata/error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns a 4xx HTTP status code or there's an error in Faraday
6
+ class ClientError < Fantasydata::Error
7
+
8
+ # Create a new error from an HTTP environment
9
+ #
10
+ # @param response [Hash]
11
+ # @return [Echowrap::Error]
12
+ def self.from_response(response={})
13
+ new(parse_error(response[:body]), response[:response_headers])
14
+ end
15
+
16
+ private
17
+
18
+ def self.parse_error(body)
19
+ body[:statusCode]
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ require 'fantasydata/error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ class ConfigurationError < ::ArgumentError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 403
6
+ class Forbidden < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 403
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'fantasydata/error/server_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 504
6
+ class GatewayTimeout < Fantasydata::Error::ServerError
7
+ HTTP_STATUS_CODE = 504
8
+ MESSAGE = "The Fantasydata servers are up, but the request couldn't be serviced due to some failure within our stack. Try again later."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'fantasydata/error/server_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 500
6
+ class InternalServerError < Fantasydata::Error::ServerError
7
+ HTTP_STATUS_CODE = 500
8
+ MESSAGE = "Something is technically wrong."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 406
6
+ class NotAcceptable < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 406
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 404
6
+ class NotFound < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 404
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require 'fantasydata/error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Echowrap returns a 5xx HTTP status code
6
+ class ServerError < Fantasydata::Error
7
+ MESSAGE = "Server Error"
8
+
9
+ # Create a new error from an HTTP environment
10
+ #
11
+ # @param response [Hash]
12
+ # @return [Echowrap::Error]
13
+ def self.from_response(response={})
14
+ new(nil, response[:response_headers])
15
+ end
16
+
17
+ # Initializes a new ServerError object
18
+ #
19
+ # @param message [String]
20
+ # @param response_headers [Hash]
21
+ # @return [Echowrap::Error::ServerError]
22
+ def initialize(message=nil, response_headers={})
23
+ super((message || self.class.const_get(:MESSAGE)), response_headers)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'fantasydata/error/server_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 503
6
+ class ServiceUnavailable < Fantasydata::Error::ServerError
7
+ HTTP_STATUS_CODE = 503
8
+ MESSAGE = "Fantasydata is over capacity."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 429
6
+ class TooManyRequests < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 429
8
+ end
9
+ EnhanceYourCalm = TooManyRequests
10
+ RateLimited = TooManyRequests
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Echowrap returns the HTTP status code 401
6
+ class Unauthorized < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 401
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'fantasydata/error/client_error'
2
+
3
+ module Fantasydata
4
+ class Error
5
+ # Raised when Fantasydata returns the HTTP status code 404
6
+ class UnprocessableEntity < Fantasydata::Error::ClientError
7
+ HTTP_STATUS_CODE = 422
8
+ end
9
+ end
10
+ end