我正在尝试从电影数据库中获得多种语言的电视连续剧。
我的目标是更新API返回的对象,使其具有以下键:
name_en, name_fr, overview_en, overview_fr
我有这段代码:
require 'httparty'
class GetSeriesJob < ApplicationJob
queue_as :default
API_URL = 'https://api.themoviedb.org/3/'
LANGS = {'fr' => '&language=fr-FR', 'en' => '&language=en-EN'}
def perform
get_series
end
private
def get_series
seriesArray = []
LANGS.each do |lang|
series = HTTParty.get(API_URL + 'tv/popular?api_key=' + ENV['API_KEY'] + lang[1])
tmp = JSON.parse(series.body)['results']
tmp.each do |t|
if seriesArray.detect{ |s| s['id'] == t['id'] }
t['name_' + lang[0]] = t['name']
t['overview_' + lang[0]] = t['overview']
else
t['name_' + lang[0]] = t.delete t['name']
t['overview_' + lang[0]] = t.delete t['overview']
end
end
seriesArray = tmp
end
puts seriesArray
return seriesArray
end
end
返回哪一个(对于一个系列):
[["original_name", "The Expanse"], ["name_en", "The Expanse"], ["popularity", "66.679301"], ["origin_country", "[\"US\"]"], ["vote_count", 564], ["first_air_date", "2015-12-14"], ["backdrop_path", "/beIjmWr3OBOtcWK4tKMObOIDJ1C.jpg"], ["original_language", "en"], ["vote_average", 7.5], ["overview_en", "A thriller set two hundred years in the future following the case of a missing young woman who brings a hardened detective and a rogue ship's captain together in a race across the solar system to expose the greatest conspiracy in human history."], ["poster_path", "/prJFWxJ0x8tBPTliMjj51wgYnSK.jpg"], ["episode_run_time", "[43]"], ["number_of_seasons", 3], ["external_id", 63639], ["created_at", "2018-06-24 13:40:16.143952"], ["updated_at", "2018-06-24 13:40:16.143952"]]
我希望得到以下结果:
[["original_name", "The Expanse"], ["name_en", "The Expanse"], ["name_fr", "The Expanse"]["popularity", "66.679301"], ["origin_country", "[\"US\"]"], ["vote_count", 564], ["first_air_date", "2015-12-14"], ["backdrop_path", "/beIjmWr3OBOtcWK4tKMObOIDJ1C.jpg"], ["original_language", "en"], ["vote_average", 7.5], ["overview_en", "A thriller set two hundred years in the future following the case of a missing young woman who brings a hardened detective and a rogue ship's captain together in a race across the solar system to expose the greatest conspiracy in human history."], ["overview_fr", "overview in French."] ["poster_path", "/prJFWxJ0x8tBPTliMjj51wgYnSK.jpg"], ["episode_run_time", "[43]"], ["number_of_seasons", 3], ["external_id", 63639], ["created_at", "2018-06-24 13:40:16.143952"], ["updated_at", "2018-06-24 13:40:16.143952"]]
我在5.2导轨上使用红宝石
答案 0 :(得分:0)
所以,我找到了解决问题的方法,这是相关的修改
public virtual Domains.Vegetable.Result Get<T>(T entity) where T: Domains.Vegetable.Entity
{
var type = typeof(T);
var info = type.GetProperty("Segment");
var value = info.GetValue(entity).ToString(); // throws exception
// other stuff
}