使用しているOSはOS Xで、以下のコードはRailsのRakefile上に書いております。
本題ですが以下の様なハッシュオブジェクト(多次元ハッシュ?)があり、この各々の値(要素)をDBへ入れたいです。
APIの呼び出し元は以下のサイトです
APIドキュメント、EVENTS項目
#rakefile def latest_game(url) away = nil home = nil ex_ah = nil json_data = Net::HTTP.get(URI.parse(url)) data = JSON.parse(json_data) #入れ子になっているlineup以下のハッシュの値を変数に入れる data.each do |detail2| b = detail['lineup'] home = b.assoc('home').pop away = b.assoc('away').pop end #lineup以外をデータに入れる team_data.each do |detail3| ex_ah = detail2.except('lineup') end #上のハッシュをまとめる summary = ex_ah.zip(home,away) #3つの多次元ハッシュがまとめられた変数summaryをレシーバにeachメソッドでtablesテーブルに値を入れたい summary.each do |detail1| Table.create(match_id:detail1['match_id'], country_id:detail1['country_id'], country_name:detail1['country_name'], league_id:detail1['league_id'], league_name:detail1['league_name'], match_date:detail1['match_date'], match_status:detail1['match_status'], match_time:detail1['match_time'], match_hometeam_name:detail1['match_hometeam_name'], match_hometeam_score:detail1['match_hometeam_score'], match_awayteam_name:detail1['match_awayteam_score'], match_awayteam_score:detail1['match_awayteam_score'],match_hometeam_halftime_score:detail1['match_hometeam_halftime_score'], match_awayteam_halftime_score:detail1['match_awayteam_halftime_score'], match_hometeam_extra_score:detail1['match_hometeam_extra_score'], match_awayteam_extra_score:detail1['match_awayteam_extra_score'], match_hometeam_penalty_score:detail1['match_hometeam_penalty_score'], match_awayteam_penalty_score:detail1['match_awayteam_penalty_score'], match_hometeam_system:detail1['match_hometeam_system'], match_awayteam_system:detail1['match_awayteam_system'], match_live:detail1['match_live'], goalscorer:detail1['goalscorer'], cards:detail1['cards'], sub_h:detail1['substitutes'], sub_a:detail1['substitutes'], coach_h:detail1['coach'], coach_a:detail1['coach'], substitute_h:detail1['substitutions'], substitute_a:detail1['substitutions'], lineup_h:detail1['starting_lineups'], lineup_a:detail1['starting_lineups'], shotson_h:detail1['statistics']) end namespace :latest_game_l do desc '最新試合日程取得' task :get_latest_game => :environment do day = Date.today days = day.to_s tommorow = day + 6 tommorows = tommorow.to_s latest_game('https://apifootball.com/api/?action=get_events&from=' + days + '&to=' + tommorows + '&league_id=62&APIkey=xxxxxxxxxxxx') end end
#1.変数dataの中身 data= {"match_id"=>"306341", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"14:30", "match_hometeam_name"=>"Cardiff City", "match_hometeam_score"=>"", "match_awayteam_name"=>"Arsenal", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]} {"match_id"=>"306342", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Watford", "match_hometeam_score"=>"", "match_awayteam_name"=>"Tottenham Hotspur", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]} {"match_id"=>"310833", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Burnley", "match_hometeam_score"=>"", "match_awayteam_name"=>"Manchester United", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]} {"match_id"=>"317207", "country_id"=>"169", "country_name"=>"England", "league_id"=>"62", "league_name"=>"Premier League", "match_date"=>"2018-09-02", "match_status"=>"", "match_time"=>"17:00", "match_hometeam_name"=>"Watford FC", "match_hometeam_score"=>"", "match_awayteam_name"=>"Tottenham Hotspur", "match_awayteam_score"=>"", "match_hometeam_halftime_score"=>"", "match_awayteam_halftime_score"=>"", "match_hometeam_extra_score"=>"", "match_awayteam_extra_score"=>"", "match_hometeam_penalty_score"=>"", "match_awayteam_penalty_score"=>"", "match_hometeam_system"=>"", "match_awayteam_system"=>"", "match_live"=>"0", "goalscorer"=>[], "cards"=>[], "lineup"=>{"home"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}, "away"=>{"starting_lineups"=>[], "substitutes"=>[], "coach"=>[], "substitutions"=>[]}}, "statistics"=>[]}
しかし今、個人では以上の様に実装しているのですがうまくいきません。
lineupのkeyであるhomeとawayが入れ子になっている為、まずhomeとawayの値を変数として持っています。
また、homeとawayの重複を防ぐ為、homeとaway(lineup=>)以外のハッシュも変数として持っています
dataの中身をみるとわかる様に、多次元のハッシュになっている為、このex_ah、home、awayを一つにまとめ、テーブルに入れる為zipメソッドを使おうと使いました。
そもそもaway,home,ex_ahの様なブロック内で再代入した変数をブロック外で呼び出すと繰り返し処理を行う訳ではないので一つの要素しか呼ばれないとは思いますが。。。
以上の様なコードで、プロジェクトがあるディレクトリでrakeコマンドを実行してみるとno implicit conversion of String into Integer等と怒られます。
本当は上の様に既に作っているモデル、テーブルに値を入れていきたいのですが、、、、
長くなりましたが、端的な質問をまとめると、タイトルにも書きましたがネストしたハッシュの各々の値をどの様にすれば、DBのテーブルの同じ行に入れていけるのかということです。同じテーブルの同じ行に要素を入れていきたいです。
何日も格闘しているのですが全く解決できなかった為、質問いたしました。
長くなりましたが宜しくお願い致します。
回答2件
あなたの回答
tips
プレビュー