简体   繁体   English

发出API请求,将响应放入RAILS 4.0.0

[英]Make API Request, puts response in RAILS 4.0.0

I have been using ruby to make API calls and operating strictly in the terminal for some time. 一段时间以来,我一直在使用ruby进行API调用并严格在终端中运行。 I am now in the process of learning more about rails and trying to get out of my terminal. 我现在正在学习有关Rails的更多信息,并尝试退出终端。 How can I, using rails 4.0, put a variable to the screen from an already existing .rb file? 如何使用Rails 4.0将变量从一个已经存在的.rb文件放到屏幕上? I am confused as to where I should write the API request to get the variable- Is it a controller, can I write it directly in a view, etc. 我对应该在哪里编写API请求以获取变量感到困惑-它是一个控制器吗,我可以直接在视图中编写它,等等。

Sample idea: 示例示例:

#test.rb

call= "/api/v2/surveys/"
auth = {:username => "test", :password => "password"}
url = HTTParty.get("https://surveys.com#{call}",
:basic_auth => auth,
:headers => { 'ContentType' => 'application/json' } )
response = JSON.parse(url.body)
survey_ids = response["surveys"].map { |s| s["id"] }
survey_ids.each do |i|
  puts i
end

That is a sample .rb script I already have. 那是我已经拥有的示例.rb脚本。 The difference is I would like for puts i to happen on a web app when a page is loaded instead of me running the script in my terminal. 所不同的是我想为puts i要当一个页面加载,而不是我跑在我的终端脚本在一个Web应用程序发生。 What would I use in rails to make that happen? 我会在rails中使用什么来实现这一目标?

It depends entirely on how your application is going to be set up but here's a basic example: 这完全取决于您的应用程序的设置方式,但这是一个基本示例:

Say you have a Survey model: 假设您有一个调查模型:

class Survey < ActiveRecord::Base
  attr_accessible :survey_id
end

You can place your call for a list of surveys (I'm assuming that's what your code does) in the SurveysController: 您可以在SurveysController中调用调查列表(我假设这就是您的代码):

class SurveysController < ApplicationController

def index
  @surveys = Survey.all
end

def show
  @survey = Survey.find(params[:id])
 end

def pull_surveys
  call= "/api/v2/surveys/"
  auth = {:username => "test", :password => "password"}
  url = HTTParty.get("https://surveys.com#{call}",
        :basic_auth => auth,
        :headers => { 'ContentType' => 'application/json' } )
  response = JSON.parse(url.body)
  survey_ids = response["surveys"].map { |s| s["id"] }
  survey_ids.each do |i|
    Survey.create(survey_id: i)
  end
end

After calling the pull_surveys method, you'll actually have surveys your view can load so in your views for the Survey Model you can use @surveys or @survey (depending on which view you're in) and serve up whatever you want (eg @survey.survey_id in show to show that specific survey's ID). 调用pull_surveys方法后,实际上将有可以加载视图的调查,因此在调查模型的视图中可以使用@surveys或@survey(取决于您所在的视图)并提供所需的任何内容(例如, @ survey.survey_id,以显示特定调查的ID)。

Note that you'll want to be careful about where you place your API call methods - I placed it in the controller for simplicity's sake but you may not want to do this. 请注意,您将需要注意放置API调用方法的位置-为简单起见,我将其放置在控制器中,但您可能不想这样做。

There's lots of useful info in the rails guides to get you started: http://guides.rubyonrails.org/index.html Rails指南中有很多有用的信息可帮助您入门: http : //guides.rubyonrails.org/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM