试图从带有Ruby的API中提取 - 如何将变量传递给字符串?

时间:2015-05-26 14:25:00

标签: ruby

我试图用Ruby练习拉API。我试图从Steam中提取视频游戏新闻。

以下是我的代码。

这个想法是,当程序运行时,提示用户输入200到440之间的游戏ID。

任何不存在的东西都不存在或数字不连续。

无论如何,我试图将gameID变量传递给字符串:

http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=# {游戏ID}&安培;计数= 5&安培;最大长度= 300安培;格式= JSON”

字符串包含在函数中。当我尝试运行程序时,错误表示参数数量错误(0表示1)。

我做错了什么,我错过了什么?非常感谢,像往常一样:)

*到目前为止,除了提问之外什么都没做,希望有一天我会做出更好的贡献:)

require 'json'
require 'HTTParty'

puts "----------------------------------------------------------" 
puts "Welcome to my practice"
puts "The purpose of this exercise is to use the SteamAPI"
puts "to pull videogame news from Steam"
puts "----------------------------------------------------------"

reset = true
while reset
    puts "Please enter a game ID between 200 - 440"
    gameID = gets.to_i
    if gameID < 200
        puts "--Invalid input--"
        reset = true
    elsif gameID > 400
        puts "--Invalid input--"
        reset= true
    else
        reset = false
    end
end

puts "--------------------Loading API----------------------------"

def get_news( gameID )
    string = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=#{gameID}&count=5&maxlength=300&format=json"
    page = HTTParty.get( string )
    browse = page["appnews"]["newsitems"]
    browse.map do |content|
        {title: content["title"], contents: content["contents"]}
    end
end

def display_story( content )
    puts "Title: #{content[:title]}"
    puts "--------------------" 
    puts " #{content[:contents]}"
    puts "--------------------"
end

get_news.each do |content|
    display_story( content )
end

1 个答案:

答案 0 :(得分:0)

您已定义get_news以获取gameID参数,但您尚未传递任何内容。在文件的末尾,您需要get_news(gameID).each