Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/arena.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Arena < ActiveRecord::Base
has_many :matches
has_many :teams, through: :matches
end
120 changes: 120 additions & 0 deletions app/models/make_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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.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_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: 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_filtered = team['api']['teams'].map{|m| m['name']}
team_filtered.each do |t|
Team.create(name: t)
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)
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)


15 changes: 15 additions & 0 deletions app/models/match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +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
11 changes: 11 additions & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Team < ActiveRecord::Base
has_many :matches
has_many :arenas, through: :matches




end



2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'bundler'
Bundler.require


ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db')
require_all 'lib'
require_all 'app/models'
7 changes: 7 additions & 0 deletions db/migrate/20201206005755_create_teams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateTeams < ActiveRecord::Migration[6.0]
def change
create_table :teams do |t|
t.string :name
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20201206010054_create_matches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateMatches < ActiveRecord::Migration[6.0]
def change
create_table :matches do |t|
t.string :date
t.integer :home_team_id
t.integer :away_team_id
t.integer :home_team_goals
t.integer :away_team_goals
t.integer :arena_id
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20201206213753_create_arenas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateArenas < ActiveRecord::Migration[6.0]
def change
create_table :arenas do |t|
t.string :name
end
end
end
32 changes: 32 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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: 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"
t.integer "home_team_id"
t.integer "away_team_id"
t.integer "home_team_goals"
t.integer "away_team_goals"
t.integer "arena_id"
end

create_table "teams", force: :cascade do |t|
t.string "name"
end

end
14 changes: 14 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Match.destroy_all
Arena.destroy_all
Team.destroy_all

t1 = Team.create(name: "Liverpool")
t2 = Team.create(name: "Manchester City")

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)


puts "Bingo!"