ruby中的API包装器没有参数?

时间:2014-06-26 05:38:54

标签: ruby-on-rails ruby rest httparty

我正在编写一个库以与Elophant API进行交互,而我当前的包装器current game infomation看起来像这样

require "httparty"

class Elophant
    include HTTParty
    base_uri 'api.elophant.com'

    def initialize(name, region)
        @options = {query: {
            summoner_name: name,
            region: region,
            key: ENV["ELOPHANT_API_KEY"]
        }}
    end

    def self.current_game
        HTTParty.get("/v2/#{region}/in_progress_game_info/#{summoner_name}?key=#{key}", @options)
    end

    def self.summoner_info
        HTTParty.get("/v2/#{region}/summoner/#{summoner_name}?key=#{key}", @options)
    end
end

所以我可以打电话

elophant = Elophant.new
elophant.current_game("summoner_name", "OCE")

将此信息发送到我的rails视图

  def index
    elophant = Elophant.new
    @CurrentGame = elophant.current_game("summoner_name", "OCE")
  end

除了rails抛出此错误

wrong number of arguments (0 for 2)

    base_uri 'api.elophant.com'

    def initialize(name, region)
        @options = {query: {
            summoner_name: name,
            region: region,

1 个答案:

答案 0 :(得分:0)

尝试使用Elpohant类,如下所示:

elophant = Elophant.new("summoner_name", "OCE")
elophant.current_game

您已将Elophant定义为在初始值设定项中包含两个参数,即summoner_nameregion。但是,您在调用current_game时传递了这些内容。

如果在您的情况下,这些参数是方法调用的本地参数,并且可以根据调用的方法而更改,您可能希望将这些参数带到方法中,在这种情况下为current_game