From ccc63abb370db1901c6577c736046289ed6691a6 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 12:36:07 -0800 Subject: [PATCH 01/11] teams migration created --- Gemfile.lock | 54 +++++++++++------------ db/migrate/20201205203527_create_teams.rb | 4 ++ 2 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20201205203527_create_teams.rb diff --git a/Gemfile.lock b/Gemfile.lock index 9589226d..76d29eab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,47 +1,47 @@ GEM remote: https://rubygems.org/ specs: - activemodel (6.0.3.1) - activesupport (= 6.0.3.1) - activerecord (6.0.3.1) - activemodel (= 6.0.3.1) - activesupport (= 6.0.3.1) - activesupport (6.0.3.1) + activemodel (6.0.3.4) + activesupport (= 6.0.3.4) + activerecord (6.0.3.4) + activemodel (= 6.0.3.4) + activesupport (= 6.0.3.4) + activesupport (6.0.3.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) - coderay (1.1.1) - concurrent-ruby (1.1.6) - i18n (1.8.2) + coderay (1.1.3) + concurrent-ruby (1.1.7) + i18n (1.8.5) concurrent-ruby (~> 1.0) - method_source (0.8.2) - minitest (5.14.1) - mustermann (1.0.3) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (1.0.0) + minitest (5.14.2) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rack (2.2.3) - rack-protection (2.0.7) + rack-protection (2.1.0) rack - require_all (1.3.3) - sinatra (2.0.7) + require_all (3.0.0) + ruby2_keywords (0.0.2) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.12) - activerecord (>= 3.2) + sinatra-activerecord (2.0.21) + activerecord (>= 4.1) sinatra (>= 1.0) - slop (3.6.0) - sqlite3 (1.3.13) + sqlite3 (1.4.2) thread_safe (0.3.6) tilt (2.0.10) - tzinfo (1.2.7) + tzinfo (1.2.8) thread_safe (~> 0.1) - zeitwerk (2.3.0) + zeitwerk (2.4.2) PLATFORMS ruby diff --git a/db/migrate/20201205203527_create_teams.rb b/db/migrate/20201205203527_create_teams.rb new file mode 100644 index 00000000..6bc1f580 --- /dev/null +++ b/db/migrate/20201205203527_create_teams.rb @@ -0,0 +1,4 @@ +class CreateTeams < ActiveRecord::Migration[6.0] + def change + end +end From c3fc67e2cb7cbc71d07bcd7e09f52ec67ae646e9 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 12:40:25 -0800 Subject: [PATCH 02/11] models and migrations created --- app/models/make_request.rb | 57 ++++++++++++++++++++ app/models/match.rb | 0 app/models/stadium.rb | 0 app/models/team.rb | 0 db/migrate/20201205203750_create_stadiums.rb | 4 ++ db/migrate/20201205203806_create_matches.rb | 4 ++ 6 files changed, 65 insertions(+) create mode 100644 app/models/make_request.rb create mode 100644 app/models/match.rb create mode 100644 app/models/stadium.rb create mode 100644 app/models/team.rb create mode 100644 db/migrate/20201205203750_create_stadiums.rb create mode 100644 db/migrate/20201205203806_create_matches.rb diff --git a/app/models/make_request.rb b/app/models/make_request.rb new file mode 100644 index 00000000..1b66b6f0 --- /dev/null +++ b/app/models/make_request.rb @@ -0,0 +1,57 @@ +require ‘uri’ +require ‘net/http’ +require ‘openssl’ +require ‘json’ +urls = [ + # URI(“https://api-football-v1.p.rapidapi.com/v2/topscorers/2”), #scorers in Premier League + URI(“https://api-football-v1.p.rapidapi.com/v2/teams/league/2”), #teams in Premier League + # URI(“https://api-football-v1.p.rapidapi.com/v2/leagueTable/2”), #team standings in Premier League + URI(“https://api-football-v1.p.rapidapi.com/v2/fixtures/league/2?timezone=Europe%2FLondon”) #fixtures in Premier League +] +http = [] +urls.each do |url| + h = Net::HTTP.new(url.host, url.port) + h.use_ssl = true + h.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.push(h) +end +# http = [Net::HTTP.new(url1.host, url1.port), , Net::HTTP.new(url2.host, url2.port), Net::HTTP.new(url3.host, url3.port)] +# http.each do |h| +# h.use_ssl = true +# h.verify_mode = OpenSSL::SSL::VERIFY_NONE +# end +# http.use_ssl = true +#http.each {|h| h.verify_mode = OpenSSL::SSL::VERIFY_NONE} +# http.verify_mode = OpenSSL::SSL::VERIFY_NONE +requests = [] +urls.each do |url| + r = Net::HTTP::Get.new(url) + r[“x-rapidapi-key”] = ‘680adbf113mshff91628eec25474p194702jsnd1c76f97e364’ + r[“x-rapidapi-host”] = ‘api-football-v1.p.rapidapi.com’ + requests.push(r) +end +# request = [Net::HTTP::Get.new(url1), Net::HTTP::Get.new(url2), Net::HTTP::Get.new(url3)] +# requests.each {|r| r[“x-rapidapi-key”] = ‘680adbf113mshff91628eec25474p194702jsnd1c76f97e364’} +# requests.each {|r| r[“x-rapidapi-host”] = ‘api-football-v1.p.rapidapi.com’} +responses = [] +count = 0 +http.each do |h| + responses.push(h.request(requests[count])) + count += 1 +end +# response1 = http[0].request(requests[0]) +# response2 = http[1].request(requests[1]) +# response3 = http[2].request(requests[2]) +standings = JSON.parse(responses[0].read_body) +pp standings +# repeats = standings[‘api’][‘leagues’].map {|league| league[‘name’]} +# #pp repeats.uniq +# players = JSON.parse(responses[1].read_body) +# player_stats = players[‘api’][‘topscorers’].map {|player| [player[‘player_name’], player[‘goals’][‘total’], player[‘team_name’], player[‘nationality’]]} +# #pp player_stats +# # pp players +# # teams = JSON.parse(responses[2].read_body) +# # pp teams +# champs = JSON.parse(responses[3].read_body) +# #pp champs[‘api’][‘standings’][0].map {|team| [team[‘teamName’], team[‘description’] ]} +# national_teams = JSON.parse(responses[4].read_body) \ No newline at end of file diff --git a/app/models/match.rb b/app/models/match.rb new file mode 100644 index 00000000..e69de29b diff --git a/app/models/stadium.rb b/app/models/stadium.rb new file mode 100644 index 00000000..e69de29b diff --git a/app/models/team.rb b/app/models/team.rb new file mode 100644 index 00000000..e69de29b diff --git a/db/migrate/20201205203750_create_stadiums.rb b/db/migrate/20201205203750_create_stadiums.rb new file mode 100644 index 00000000..0763b77c --- /dev/null +++ b/db/migrate/20201205203750_create_stadiums.rb @@ -0,0 +1,4 @@ +class CreateStadiums < ActiveRecord::Migration[6.0] + def change + end +end diff --git a/db/migrate/20201205203806_create_matches.rb b/db/migrate/20201205203806_create_matches.rb new file mode 100644 index 00000000..d50411cb --- /dev/null +++ b/db/migrate/20201205203806_create_matches.rb @@ -0,0 +1,4 @@ +class CreateMatches < ActiveRecord::Migration[6.0] + def change + end +end From a41ad0c01f49575649ea92ceb12390138353ed02 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 12:48:21 -0800 Subject: [PATCH 03/11] create_tables updated --- app/models/match.rb | 4 ++++ app/models/stadium.rb | 4 ++++ app/models/team.rb | 4 ++++ db/migrate/20201205203527_create_teams.rb | 3 +++ db/migrate/20201205203750_create_stadiums.rb | 3 +++ db/migrate/20201205203806_create_matches.rb | 8 ++++++++ 6 files changed, 26 insertions(+) diff --git a/app/models/match.rb b/app/models/match.rb index e69de29b..0ced91a9 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -0,0 +1,4 @@ +class Match < ActiveRecord::Base + belongs_to :stadium + belongs_to :teams +end diff --git a/app/models/stadium.rb b/app/models/stadium.rb index e69de29b..36c97175 100644 --- a/app/models/stadium.rb +++ b/app/models/stadium.rb @@ -0,0 +1,4 @@ +class Stadium < ActiveRecord::Base + has_many :matches + has_many :teams, through: :matches +end diff --git a/app/models/team.rb b/app/models/team.rb index e69de29b..bfa72f5e 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -0,0 +1,4 @@ +class Team < ActiveRecord::Base + has_many :matches + has_many :stadiums, through: :matches +end diff --git a/db/migrate/20201205203527_create_teams.rb b/db/migrate/20201205203527_create_teams.rb index 6bc1f580..3eef64e9 100644 --- a/db/migrate/20201205203527_create_teams.rb +++ b/db/migrate/20201205203527_create_teams.rb @@ -1,4 +1,7 @@ class CreateTeams < ActiveRecord::Migration[6.0] def change + create_table :teams do |t| + t.string :name + end end end diff --git a/db/migrate/20201205203750_create_stadiums.rb b/db/migrate/20201205203750_create_stadiums.rb index 0763b77c..b9abebc6 100644 --- a/db/migrate/20201205203750_create_stadiums.rb +++ b/db/migrate/20201205203750_create_stadiums.rb @@ -1,4 +1,7 @@ class CreateStadiums < ActiveRecord::Migration[6.0] def change + create_table :stadiums do |t| + t.string :name + end end end diff --git a/db/migrate/20201205203806_create_matches.rb b/db/migrate/20201205203806_create_matches.rb index d50411cb..01d0f249 100644 --- a/db/migrate/20201205203806_create_matches.rb +++ b/db/migrate/20201205203806_create_matches.rb @@ -1,4 +1,12 @@ class CreateMatches < ActiveRecord::Migration[6.0] def change + create_table :matches do |t| + t.datetime :date + t.integer :home_team_id + t.integer :away_team_id + t.integer :home_team_goals + t.integer :away_team_goals + t.integer :stadium_id + end end end From 033ba89514bc161d39db5e62d367d85fb5d9700b Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 13:05:23 -0800 Subject: [PATCH 04/11] before destroying --- db/schema.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..70ce83c5 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2) do + + create_table "teams", force: :cascade do |t| + t.string "name" + end + +end From 39753275dbcd41cc06b46a154056adff94188be7 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 13:34:20 -0800 Subject: [PATCH 05/11] removed the demon teams table --- db/schema.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 70ce83c5..0dfde4fc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,20 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2) do +ActiveRecord::Schema.define(version: 2020_12_05_203806) do + + create_table "matches", force: :cascade do |t| + t.datetime "date" + t.integer "home_team_id" + t.integer "away_team_id" + t.integer "home_team_goals" + t.integer "away_team_goals" + t.integer "stadium_id" + end + + create_table "stadiums", force: :cascade do |t| + t.string "name" + end create_table "teams", force: :cascade do |t| t.string "name" From a5aa6d0033c1441d650b9baab391339726a8feee Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sat, 5 Dec 2020 13:40:41 -0800 Subject: [PATCH 06/11] created seed file --- db/migrate/seeds.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 db/migrate/seeds.rb diff --git a/db/migrate/seeds.rb b/db/migrate/seeds.rb new file mode 100644 index 00000000..dd869d24 --- /dev/null +++ b/db/migrate/seeds.rb @@ -0,0 +1,4 @@ +Match.destroy_all +Stadium.destroy_all +Team.desteroy_all + From 1bab7e017130210a716c5d9b52ac0bea2d514f8c Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sun, 6 Dec 2020 13:41:40 -0800 Subject: [PATCH 07/11] updated classes --- app/models/{stadium.rb => arena.rb} | 2 +- app/models/match.rb | 2 +- app/models/team.rb | 2 +- config/environment.rb | 1 + db/migrate/20201205203750_create_stadiums.rb | 7 ------- ...reate_teams.rb => 20201206005755_create_teams.rb} | 0 ...e_matches.rb => 20201206010054_create_matches.rb} | 2 +- db/migrate/20201206213753_create_arenas.rb | 7 +++++++ db/migrate/seeds.rb | 2 +- db/schema.rb | 12 ++++++------ app/models/make_request.rb => make_request.rb | 0 11 files changed, 19 insertions(+), 18 deletions(-) rename app/models/{stadium.rb => arena.rb} (65%) delete mode 100644 db/migrate/20201205203750_create_stadiums.rb rename db/migrate/{20201205203527_create_teams.rb => 20201206005755_create_teams.rb} (100%) rename db/migrate/{20201205203806_create_matches.rb => 20201206010054_create_matches.rb} (90%) create mode 100644 db/migrate/20201206213753_create_arenas.rb rename app/models/make_request.rb => make_request.rb (100%) diff --git a/app/models/stadium.rb b/app/models/arena.rb similarity index 65% rename from app/models/stadium.rb rename to app/models/arena.rb index 36c97175..6b838ab9 100644 --- a/app/models/stadium.rb +++ b/app/models/arena.rb @@ -1,4 +1,4 @@ -class Stadium < ActiveRecord::Base +class Arena < ActiveRecord::Base has_many :matches has_many :teams, through: :matches end diff --git a/app/models/match.rb b/app/models/match.rb index 0ced91a9..ebb25926 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -1,4 +1,4 @@ class Match < ActiveRecord::Base - belongs_to :stadium + belongs_to :arena belongs_to :teams end diff --git a/app/models/team.rb b/app/models/team.rb index bfa72f5e..0850e0da 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -1,4 +1,4 @@ class Team < ActiveRecord::Base has_many :matches - has_many :stadiums, through: :matches + has_many :arenas, through: :matches end diff --git a/config/environment.rb b/config/environment.rb index 4dbe13e5..94acb644 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,6 @@ require 'bundler' Bundler.require +require_all 'app/models' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') require_all 'lib' diff --git a/db/migrate/20201205203750_create_stadiums.rb b/db/migrate/20201205203750_create_stadiums.rb deleted file mode 100644 index b9abebc6..00000000 --- a/db/migrate/20201205203750_create_stadiums.rb +++ /dev/null @@ -1,7 +0,0 @@ -class CreateStadiums < ActiveRecord::Migration[6.0] - def change - create_table :stadiums do |t| - t.string :name - end - end -end diff --git a/db/migrate/20201205203527_create_teams.rb b/db/migrate/20201206005755_create_teams.rb similarity index 100% rename from db/migrate/20201205203527_create_teams.rb rename to db/migrate/20201206005755_create_teams.rb diff --git a/db/migrate/20201205203806_create_matches.rb b/db/migrate/20201206010054_create_matches.rb similarity index 90% rename from db/migrate/20201205203806_create_matches.rb rename to db/migrate/20201206010054_create_matches.rb index 01d0f249..3cd6ed1b 100644 --- a/db/migrate/20201205203806_create_matches.rb +++ b/db/migrate/20201206010054_create_matches.rb @@ -6,7 +6,7 @@ def change t.integer :away_team_id t.integer :home_team_goals t.integer :away_team_goals - t.integer :stadium_id + t.integer :arena_id end end end diff --git a/db/migrate/20201206213753_create_arenas.rb b/db/migrate/20201206213753_create_arenas.rb new file mode 100644 index 00000000..6be62abc --- /dev/null +++ b/db/migrate/20201206213753_create_arenas.rb @@ -0,0 +1,7 @@ +class CreateArenas < ActiveRecord::Migration[6.0] + def change + create_table :arenas do |t| + t.string :name + end + end +end diff --git a/db/migrate/seeds.rb b/db/migrate/seeds.rb index dd869d24..c6b2ac30 100644 --- a/db/migrate/seeds.rb +++ b/db/migrate/seeds.rb @@ -1,4 +1,4 @@ Match.destroy_all -Stadium.destroy_all +Arena.destroy_all Team.desteroy_all diff --git a/db/schema.rb b/db/schema.rb index 0dfde4fc..9d018b08 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_12_05_203806) do +ActiveRecord::Schema.define(version: 2020_12_06_213753) do + + create_table "arenas", force: :cascade do |t| + t.string "name" + end create_table "matches", force: :cascade do |t| t.datetime "date" @@ -18,11 +22,7 @@ t.integer "away_team_id" t.integer "home_team_goals" t.integer "away_team_goals" - t.integer "stadium_id" - end - - create_table "stadiums", force: :cascade do |t| - t.string "name" + t.integer "arena_id" end create_table "teams", force: :cascade do |t| diff --git a/app/models/make_request.rb b/make_request.rb similarity index 100% rename from app/models/make_request.rb rename to make_request.rb From eb43ec7ebbeb93869c987feca0b977caf932bfaa Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sun, 6 Dec 2020 13:45:57 -0800 Subject: [PATCH 08/11] created seeds --- db/migrate/seeds.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/db/migrate/seeds.rb b/db/migrate/seeds.rb index c6b2ac30..4bcb29ce 100644 --- a/db/migrate/seeds.rb +++ b/db/migrate/seeds.rb @@ -2,3 +2,10 @@ Arena.destroy_all Team.desteroy_all +t1 = Team.new(name: "Liverpool") +t2 = Team.new(name: "Manchester City") + +a1 = Arena.new(name: "First Arena") +a2 = Arena.new(name: "Second Arena") + +m1 = Match.new (date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) \ No newline at end of file From 1b4b31777c55ff5adcefe863f2f35da193bb8ab2 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sun, 6 Dec 2020 13:58:27 -0800 Subject: [PATCH 09/11] added seed data --- db/{migrate => }/seeds.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) rename db/{migrate => }/seeds.rb (52%) diff --git a/db/migrate/seeds.rb b/db/seeds.rb similarity index 52% rename from db/migrate/seeds.rb rename to db/seeds.rb index 4bcb29ce..3cfd2f24 100644 --- a/db/migrate/seeds.rb +++ b/db/seeds.rb @@ -1,6 +1,6 @@ Match.destroy_all Arena.destroy_all -Team.desteroy_all +Team.destroy_all t1 = Team.new(name: "Liverpool") t2 = Team.new(name: "Manchester City") @@ -8,4 +8,7 @@ a1 = Arena.new(name: "First Arena") a2 = Arena.new(name: "Second Arena") -m1 = Match.new (date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) \ No newline at end of file +m1 = Match.new(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) + + +puts "Bingo!" \ No newline at end of file From fe6348ca5398536ef3d0a0f1a8f1a51c99841b05 Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sun, 6 Dec 2020 17:01:27 -0800 Subject: [PATCH 10/11] make_request class building --- Rakefile | 1 + app/models/make_request.rb | 112 ++++++++++++++++++++ app/models/match.rb | 11 ++ app/models/team.rb | 7 ++ config/environment.rb | 3 +- db/migrate/20201206010054_create_matches.rb | 2 +- db/seeds.rb | 10 +- make_request.rb | 57 ---------- 8 files changed, 139 insertions(+), 64 deletions(-) create mode 100644 app/models/make_request.rb delete mode 100644 make_request.rb diff --git a/Rakefile b/Rakefile index 508ef20e..f9aca5ad 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require_relative 'config/environment' require 'sinatra/activerecord/rake' +require_relative 'app/models/make_request' desc 'starts a console' task :console do diff --git a/app/models/make_request.rb b/app/models/make_request.rb new file mode 100644 index 00000000..6fd59abf --- /dev/null +++ b/app/models/make_request.rb @@ -0,0 +1,112 @@ +require 'uri' +require 'net/http' +require 'openssl' +require 'json' + +class MakeRequest < ActiveRecord::Base + + +@@urls = [ + # URI("https://api-football-v1.p.rapidapi.com/v2/topscorers/2"), #scorers in Premier League + URI("https://api-football-v1.p.rapidapi.com/v2/teams/league/2"), #teams in Premier League + # URI("https://api-football-v1.p.rapidapi.com/v2/leagueTable/2"), #team standings in Premier League + URI("https://api-football-v1.p.rapidapi.com/v2/fixtures/league/2?timezone=Europe%2FLondon") #fixtures in Premier League +] +@@http = [] +urls.each do |url| + h = Net::HTTP.new(url.host, url.port) + h.use_ssl = true + h.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.push(h) +end + +@@requests = [] +urls.each do |url| + r = Net::HTTP::Get.new(url) + r["x-rapidapi-key"] = '680adbf113mshff91628eec25474p194702jsnd1c76f97e364' + r["x-rapidapi-host"] = 'api-football-v1.p.rapidapi.com' + requests.push(r) +end + +@@responses = [] +count = 0 +http.each do |h| + responses.push(h.request(requests[count])) + count += 1 +end + + + +def self.populate_match + match = JSON.parse(responses[1].read_body) + match_filtered = match['api']['fixtures'].map{|m| [m['event_date'], m['homeTeam']['team_name'], m['awayTeam']['team_name'], m['goalsHomeTeam'], m['goalsAwayTeam'], m['venue']]} + match_filtered.each do |m| + Match.create(date: m[0], home_team_id: Team.find_by(name:"#{m[1]}").id, away_team_id: Team.find_by(name:"#{m[2]}").id, home_team_goals: m[3], away_team_goals: m[4], arena_id: Arena.find_by(name: "#{m[5]}").id) + end +end + +def self.populate_team + team = JSON.parse(responses[0].read_body) + team_filtered = team['api']['teams'].map{|m| m['name']} + team_filtered.each do |t| + Team.create(name: t[0]) + end +end + + + +def self.populate_arena + team = JSON.parse(responses[0].read_body) + arena = team['api']['teams'].map{|m| m['venue_name']} + arena.each do |a| + Arena.create(name: a[0]) + end +end + +end + + + + + +# def populateCharity(url) +# parsed = populateHelper(url) +# parsed.each do |charity| +# Charity.create(ein: charity["ein"], name: charity["charityName"], category: charity["category"], +# state: charity["state"], accepting_donations: charity["acceptingDonations"]) +# end +# end + +# end + + + + + + + + + + + + + + + + + + + +# repeats = standings['api']['leagues'].map {|league| league['name']} +# #pp repeats.uniq +# players = JSON.parse(responses[1].read_body) +# player_stats = players['api']['topscorers'].map {|player| [player['player_name'], player['goals']['total'], player['team_name'], player['nationality']]} +# #pp player_stats +# # pp players +# # teams = JSON.parse(responses[2].read_body) +# # pp teams +# champs = JSON.parse(responses[3].read_body) +# #pp champs['api']['standings'][0].map {|team| [team['teamName'], team['description'] ]} +# national_teams = JSON.parse(responses[4].read_body) + + diff --git a/app/models/match.rb b/app/models/match.rb index ebb25926..6648b8d5 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -1,4 +1,15 @@ +# require 'make_request.rb' + class Match < ActiveRecord::Base belongs_to :arena belongs_to :teams + + def home_team_goals + pp match['api']['fixtures'].map{|m|m['goalsHomeTeam']} + end + + def test + p "it works!" + end + end diff --git a/app/models/team.rb b/app/models/team.rb index 0850e0da..83975ffb 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -1,4 +1,11 @@ class Team < ActiveRecord::Base has_many :matches has_many :arenas, through: :matches + + + + end + + + diff --git a/config/environment.rb b/config/environment.rb index 94acb644..4e38a3b5 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,6 +1,7 @@ require 'bundler' Bundler.require -require_all 'app/models' + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') require_all 'lib' +require_all 'app/models' diff --git a/db/migrate/20201206010054_create_matches.rb b/db/migrate/20201206010054_create_matches.rb index 3cd6ed1b..70d94744 100644 --- a/db/migrate/20201206010054_create_matches.rb +++ b/db/migrate/20201206010054_create_matches.rb @@ -1,7 +1,7 @@ class CreateMatches < ActiveRecord::Migration[6.0] def change create_table :matches do |t| - t.datetime :date + t.string :date t.integer :home_team_id t.integer :away_team_id t.integer :home_team_goals diff --git a/db/seeds.rb b/db/seeds.rb index 3cfd2f24..e33dcc68 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,13 +2,13 @@ Arena.destroy_all Team.destroy_all -t1 = Team.new(name: "Liverpool") -t2 = Team.new(name: "Manchester City") +t1 = Team.create(name: "Liverpool") +t2 = Team.create(name: "Manchester City") -a1 = Arena.new(name: "First Arena") -a2 = Arena.new(name: "Second Arena") +a1 = Arena.create(name: "First Arena") +a2 = Arena.create(name: "Second Arena") -m1 = Match.new(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) +m1 = Match.create(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) puts "Bingo!" \ No newline at end of file diff --git a/make_request.rb b/make_request.rb deleted file mode 100644 index 1b66b6f0..00000000 --- a/make_request.rb +++ /dev/null @@ -1,57 +0,0 @@ -require ‘uri’ -require ‘net/http’ -require ‘openssl’ -require ‘json’ -urls = [ - # URI(“https://api-football-v1.p.rapidapi.com/v2/topscorers/2”), #scorers in Premier League - URI(“https://api-football-v1.p.rapidapi.com/v2/teams/league/2”), #teams in Premier League - # URI(“https://api-football-v1.p.rapidapi.com/v2/leagueTable/2”), #team standings in Premier League - URI(“https://api-football-v1.p.rapidapi.com/v2/fixtures/league/2?timezone=Europe%2FLondon”) #fixtures in Premier League -] -http = [] -urls.each do |url| - h = Net::HTTP.new(url.host, url.port) - h.use_ssl = true - h.verify_mode = OpenSSL::SSL::VERIFY_NONE - http.push(h) -end -# http = [Net::HTTP.new(url1.host, url1.port), , Net::HTTP.new(url2.host, url2.port), Net::HTTP.new(url3.host, url3.port)] -# http.each do |h| -# h.use_ssl = true -# h.verify_mode = OpenSSL::SSL::VERIFY_NONE -# end -# http.use_ssl = true -#http.each {|h| h.verify_mode = OpenSSL::SSL::VERIFY_NONE} -# http.verify_mode = OpenSSL::SSL::VERIFY_NONE -requests = [] -urls.each do |url| - r = Net::HTTP::Get.new(url) - r[“x-rapidapi-key”] = ‘680adbf113mshff91628eec25474p194702jsnd1c76f97e364’ - r[“x-rapidapi-host”] = ‘api-football-v1.p.rapidapi.com’ - requests.push(r) -end -# request = [Net::HTTP::Get.new(url1), Net::HTTP::Get.new(url2), Net::HTTP::Get.new(url3)] -# requests.each {|r| r[“x-rapidapi-key”] = ‘680adbf113mshff91628eec25474p194702jsnd1c76f97e364’} -# requests.each {|r| r[“x-rapidapi-host”] = ‘api-football-v1.p.rapidapi.com’} -responses = [] -count = 0 -http.each do |h| - responses.push(h.request(requests[count])) - count += 1 -end -# response1 = http[0].request(requests[0]) -# response2 = http[1].request(requests[1]) -# response3 = http[2].request(requests[2]) -standings = JSON.parse(responses[0].read_body) -pp standings -# repeats = standings[‘api’][‘leagues’].map {|league| league[‘name’]} -# #pp repeats.uniq -# players = JSON.parse(responses[1].read_body) -# player_stats = players[‘api’][‘topscorers’].map {|player| [player[‘player_name’], player[‘goals’][‘total’], player[‘team_name’], player[‘nationality’]]} -# #pp player_stats -# # pp players -# # teams = JSON.parse(responses[2].read_body) -# # pp teams -# champs = JSON.parse(responses[3].read_body) -# #pp champs[‘api’][‘standings’][0].map {|team| [team[‘teamName’], team[‘description’] ]} -# national_teams = JSON.parse(responses[4].read_body) \ No newline at end of file From d874b657e45736aded92a4135a571714059f31ec Mon Sep 17 00:00:00 2001 From: handsomemolly Date: Sun, 6 Dec 2020 18:44:25 -0800 Subject: [PATCH 11/11] populating matches without IDs --- app/models/make_request.rb | 32 ++++++++++++++++++++------------ db/seeds.rb | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/models/make_request.rb b/app/models/make_request.rb index 6fd59abf..c3688312 100644 --- a/app/models/make_request.rb +++ b/app/models/make_request.rb @@ -13,53 +13,61 @@ class MakeRequest < ActiveRecord::Base URI("https://api-football-v1.p.rapidapi.com/v2/fixtures/league/2?timezone=Europe%2FLondon") #fixtures in Premier League ] @@http = [] -urls.each do |url| +@@urls.each do |url| h = Net::HTTP.new(url.host, url.port) h.use_ssl = true h.verify_mode = OpenSSL::SSL::VERIFY_NONE - http.push(h) + @@http.push(h) end @@requests = [] -urls.each do |url| +@@urls.each do |url| r = Net::HTTP::Get.new(url) r["x-rapidapi-key"] = '680adbf113mshff91628eec25474p194702jsnd1c76f97e364' r["x-rapidapi-host"] = 'api-football-v1.p.rapidapi.com' - requests.push(r) + @@requests.push(r) end @@responses = [] count = 0 -http.each do |h| - responses.push(h.request(requests[count])) +@@http.each do |h| + @@responses.push(h.request(@@requests[count])) count += 1 end +def self.find_team_id_by_name(name) + Team.find_by(name: name) +end + +def self.find_arena_id_by_name(name) + Arena.find_by(name: name) +end def self.populate_match - match = JSON.parse(responses[1].read_body) + match = JSON.parse(@@responses[1].read_body) match_filtered = match['api']['fixtures'].map{|m| [m['event_date'], m['homeTeam']['team_name'], m['awayTeam']['team_name'], m['goalsHomeTeam'], m['goalsAwayTeam'], m['venue']]} + match_filtered.each do |m| - Match.create(date: m[0], home_team_id: Team.find_by(name:"#{m[1]}").id, away_team_id: Team.find_by(name:"#{m[2]}").id, home_team_goals: m[3], away_team_goals: m[4], arena_id: Arena.find_by(name: "#{m[5]}").id) + Match.create(date: m[0], home_team_id: self.find_team_id_by_name(m[1]), away_team_id: self.find_team_id_by_name(m[2]), home_team_goals: m[3], away_team_goals: m[4], arena_id: self.find_arena_id_by_name(m[5])) end end def self.populate_team - team = JSON.parse(responses[0].read_body) + team = JSON.parse(@@responses[0].read_body) team_filtered = team['api']['teams'].map{|m| m['name']} team_filtered.each do |t| - Team.create(name: t[0]) + Team.create(name: t) end end def self.populate_arena - team = JSON.parse(responses[0].read_body) + team = JSON.parse(@@responses[0].read_body) arena = team['api']['teams'].map{|m| m['venue_name']} arena.each do |a| - Arena.create(name: a[0]) + Arena.create(name: a) end end diff --git a/db/seeds.rb b/db/seeds.rb index e33dcc68..225133b9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -8,7 +8,7 @@ a1 = Arena.create(name: "First Arena") a2 = Arena.create(name: "Second Arena") -m1 = Match.create(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) +# m1 = Match.create(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) puts "Bingo!" \ No newline at end of file