[英]Configuring Ruby On Rails Application With Iex-Ruby-Client Gem
我是一個初學者程序員。 最近,我建立了一個使用iex-ruby-client gem的應用程序,該應用程序可以為我輸入網頁表格中的股票報價。 效果很好。
但是,在6月初,IEX更改了他們的API,因此您必須從IEX雲控制台獲得可發布的令牌。 我從IEX雲控制台獲得了可發布的令牌。
更新的gem docs( https://github.com/dblock/iex-ruby-client )表示我必須立即“配置”該應用程序。 我根本不知道如何或在何處實現配置代碼。 這是gem文檔中建議的代碼。 我只是不知道放在哪里。
Configure IEX::Api.configure do |config|
config.publishable_token = 'token' # defaults to
ENV['IEX_API_PUBLISHABLE_TOKEN']
config.endpoint = 'https://sandbox.iexapis.com/v1' # defaults to
'https://cloud.iexapis.com/v1'
end
這些文檔還指出:“您還可以直接配置客戶端實例。”
client = IEX::Api::Client.new(
publishable_token: 'token',
endpoint: 'https://sandbox.iexapis.com/v1'
)
我將添加額外的代碼,以根據此處的響應來澄清我所做的事情。 這是我新的config / initializers / iex-ruby-client.rb文件(令牌信息不是真正的文件)。
IEX::Api.configure do |config|
config.publishable_token = 'pk_3b38fsdadfsafjsdalfjdsakfjlda12f519'
config.endpoint = 'https://sandbox.iexapis.com/v1'
end
這是我需要庫的控制器中的相關方法:
def index
require 'iex-ruby-client'
if params[:id] == ""
@nothing = "You forgot to enter a symbol ;)."
elsif
if params[:id]
begin
@stock = IEX::Resources::Quote.get(params[:id])
@company = IEX::Resources::Company.get(params[:id])
rescue StandardError
@error = "That stock symbol doesn't seem to exist. Please enter
another symbol."
end
end
end
end
因此,我創建了配置文件,並在方法頂部需要使用gem,但仍然出現錯誤。 我確定在實施此令牌要求時存在一些缺陷。 如果您還有其他建議,歡迎您。 但是,如果我對Stack Overflow的要求不高,我理解。 謝謝。
好吧,您顯然有兩個選擇:
/config/initializers
下創建一個配置文件(即iex_client.rb
)來使用初始化 /config/initializers
並添加: Configure IEX::Api.configure do |config|
config.publishable_token = 'token' # defaults to
ENV['IEX_API_PUBLISHABLE_TOKEN']
config.endpoint = 'https://sandbox.iexapis.com/v1' # defaults to
'https://cloud.iexapis.com/v1'
end
client = IEX::Api::Client.new(
publishable_token: 'token',
endpoint: 'https://sandbox.iexapis.com/v1'
)
您可能需要用正確的token
替換token
。 您還需要確保無論您想在哪里使用它require
該庫。
在嘗試配置IEX-ruby-client gem失敗后(如我在堆棧溢出問題中所述),我切換到了stock_quote gem。 該gem是基於相同的IEX API構建的,並且使用在config / initializers中保存的stock_quote.rb文件配置應用程序沒有問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.