Skip to content

Instantly share code, notes, and snippets.

@noam87
Created September 3, 2015 19:03
Show Gist options
  • Save noam87/f445ac60aa7c30a20d16 to your computer and use it in GitHub Desktop.
Save noam87/f445ac60aa7c30a20d16 to your computer and use it in GitHub Desktop.
diff --git a/Gemfile b/Gemfile
index d735be0..be79ed8 100644
--- a/Gemfile
+++ b/Gemfile
@@ -21,6 +21,7 @@ gem 'unicorn'
group :development, :test, :staging do
gem 'httplog', require: false
gem 'pry-byebug'
+ gem 'pry-doc'
end
group :development do
diff --git a/Gemfile.lock b/Gemfile.lock
index ad970bf..7529671 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -88,6 +88,9 @@ GEM
pry-byebug (3.0.1)
byebug (~> 3.4)
pry (~> 0.10)
+ pry-doc (0.8.0)
+ pry (~> 0.9)
+ yard (~> 0.8)
rack (1.5.2)
rack-contrib (1.1.0)
rack (>= 0.9.1)
@@ -157,6 +160,7 @@ GEM
kgio (~> 2.6)
rack
raindrops (~> 0.7)
+ yard (0.8.7.6)
PLATFORMS
ruby
@@ -173,6 +177,7 @@ DEPENDENCIES
kgio
oj
pry-byebug
+ pry-doc
rack-contrib
rack-cors
rack-test
diff --git a/README.md b/README.md
index 2219c22..6ae0742 100644
--- a/README.md
+++ b/README.md
@@ -341,7 +341,7 @@ POST http://connect-staging.thescore.com/api/v1/player_recommendations
### Body
-`count` is optional and defaults to 10.
+`size` is optional and defaults to 10.
If `team_uris` is empty, it will return the globally most popular players.
@@ -354,7 +354,7 @@ If `team_uris` is empty, it will return the globally most popular players.
"/soccer/teams/900"
],
- "count": 100
+ "size": 100
}
```
diff --git a/lib/tasks/console.rake b/lib/tasks/console.rake
index f497286..b2b8049 100644
--- a/lib/tasks/console.rake
+++ b/lib/tasks/console.rake
@@ -11,6 +11,6 @@ namespace :zing do
task console: [:environment] do
require 'pry-byebug'
- binding.pry
+ Pry.start
end
end
diff --git a/lib/zing.rb b/lib/zing.rb
index 353a603..c732518 100644
--- a/lib/zing.rb
+++ b/lib/zing.rb
@@ -41,6 +41,7 @@ require 'zing/queries/esports_team_query'
require 'zing/queries/sports_query_builder'
require 'zing/queries/recommended_teams_by_leagues_query'
require 'zing/util/elastic_search_client'
+require 'zing/util/federation_mapping'
require 'zing/util/league_slug'
require 'zing/util/league_tokens'
require 'zing/util/sport_slug'
diff --git a/lib/zing/application/sports_routes.rb b/lib/zing/application/sports_routes.rb
index 1e0278f..58c1618 100644
--- a/lib/zing/application/sports_routes.rb
+++ b/lib/zing/application/sports_routes.rb
@@ -106,7 +106,8 @@ module Zing
end
get '/search/recommended-teams' do
- league_names = params[LEAGUES_PARAM]
+ leagues = params.fetch(LEAGUES_PARAM)
+ league_names = Zing::FederationMapping.new(leagues).leagues
size = compute_size(params[SIZE_PARAM])
query = RecommendedTeamsByLeaguesQuery.new(league_names)
diff --git a/lib/zing/constants.rb b/lib/zing/constants.rb
index 807951e..60552cd 100644
--- a/lib/zing/constants.rb
+++ b/lib/zing/constants.rb
@@ -154,5 +154,22 @@ module Zing
wcquefa
worldcup
).freeze
+
+ # I don't get updated often... But when I do, I do it through
+ # `rake generate:federations_hash` in TheScoreData.
+ FEDERATIONS_LEAGUES_MAP = {
+ wolymh_fed: ["wolymhm", "wolymhw"],
+ fifa_fed: ["worldcup"],
+ eng_fed: ["epl", "engfa", "engcc"],
+ uefa_fed: ["chlg", "uefa"],
+ ita_fed: ["seri", "itaci"],
+ deu_fed: ["bund", "gerdfbp"],
+ esp_fed: ["liga", "espcr"],
+ fra_fed: ["fran"],
+ mex_fed: ["fmf"],
+ us_fed: ["mls"],
+ euro_fed: ["eurc", "eurcq"],
+ nascar_fed: ["nascar", "nasnw"]
+ }.freeze
end
end
diff --git a/lib/zing/util/federation_mapping.rb b/lib/zing/util/federation_mapping.rb
new file mode 100644
index 0000000..7f95e55
--- /dev/null
+++ b/lib/zing/util/federation_mapping.rb
@@ -0,0 +1,31 @@
+module Zing
+ class FederationMapping
+ MAPPING = Constants::FEDERATIONS_LEAGUES_MAP
+ private_constant :MAPPING
+
+
+ def initialize(slugs, mapping: MAPPING)
+ @mapping = mapping
+ @slugs = slugs
+ end
+
+ # Given league or federation slugs:
+ #
+ # 1. Return league slug if it's a leagues slug.
+ # 2. Return multiple league slugs if it's a federation slug.
+ def leagues
+ @slugs.split(' ').map { |slug| from_slug(slug) }.flatten.uniq.join(' ')
+ end
+
+ private
+
+ def from_slug(slug)
+ return slug unless is_a_federation?(slug)
+ @mapping.fetch(slug.to_sym)
+ end
+
+ def is_a_federation?(slug)
+ @mapping.has_key?(slug.to_sym)
+ end
+ end
+end
diff --git a/spec/util/federation_mapping_spec.rb b/spec/util/federation_mapping_spec.rb
new file mode 100644
index 0000000..db18e14
--- /dev/null
+++ b/spec/util/federation_mapping_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe Zing::FederationMapping do
+ let(:mapping) do
+ {
+ federation_one: ['league_one', 'league_two'],
+ federation_two: ['league_three', 'league_four']
+ }
+ end
+
+
+ describe '#from_slugs' do
+ context 'when given proper league names' do
+ subject { described_class.new('league_1 league_2', mapping: mapping) }
+
+ it 'should return the leagues' do
+ expect(subject.leagues).to eq('league_1 league_2')
+ end
+ end
+
+ context 'when given a federation name' do
+ subject { described_class.new('federation_one', mapping: mapping) }
+
+ it 'returns the leagues in that federation' do
+ expect(subject.leagues).to eq('league_one league_two')
+ end
+
+ context 'and a league name' do
+ subject { described_class.new('league_1 federation_one', mapping: mapping) }
+
+ it 'returns the leagues and the ones in the federation' do
+ expect(subject.leagues).to eq('league_1 league_one league_two')
+ end
+ end
+ end
+ end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment