我从mashape获得了翻译yoda API。 https://github.com/sfinley89/BEWD_midcourse 现在我试图让随机的yoda giphy图像显示在欢迎/ yoda'页面,当他们的文字被翻译成尤达说话。
以下是我现在正在欢迎/ yoda'
Yoda Says, "Lets gets some gifs alrady. "
#<Giphy::RandomGif:0x007ffae5467e08>
#<unirest::httpresponse:0x007ffae57a23e8>
{"data"=>{"type"=>"gif", "id"=>"PciMitIKp0UjS", "url"=>"http://giphy.com/gifs/yoda-PciMitIKp0UjS", "image_original_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/giphy.gif", "image_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/giphy.gif", "image_mp4_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/giphy.mp4", "image_frames"=>"21", "image_width"=>"150", "image_height"=>"190", "fixed_height_downsampled_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/200_d.gif", "fixed_height_downsampled_width"=>"158", "fixed_height_downsampled_height"=>"200", "fixed_width_downsampled_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/200w_d.gif", "fixed_width_downsampled_width"=>"200", "fixed_width_downsampled_height"=>"253", "fixed_height_small_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/100.gif", "fixed_height_small_still_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/100_s.gif", "fixed_height_small_width"=>"79", "fixed_height_small_height"=>"100", "fixed_width_small_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/100w.gif", "fixed_width_small_still_url"=>"http://media4.giphy.com/media/PciMitIKp0UjS/100w_s.gif", "fixed_width_small_width"=>"100", "fixed_width_small_height"=>"127", "username"=>"", "caption"=>""}, "meta"=>{"status"=>200, "msg"=>"OK"}}
Back
如何让它显示随机yoda gif?
这是我的控制器welcome_controller.rb
class WelcomeController < ApplicationController
def index
@ask
end
def ask
end
def yoda
#@voice = HTTParty.get('https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.++Oh+wait.').parsed_response
#sentence = "You+will+learn+how+to+speak+like+me+someday.++Oh+wait."
sentence = params[:sentence]
#render plain: ENV["YODA_KEY"]
@response = Unirest.get "https://yoda.p.mashape.com/yoda?sentence="+sentence,
headers:{
"X-Mashape-Key" => ENV["YODA_KEY"],
"Accept" => "text/plain",
}
@gifpic = Unirest.get "http://api.giphy.com/v1/gifs/search?q=Yoda&api_key=dc6zaTOxFJmzC"
@gif = Giphy.random('Yoda')
url = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=yoda"
resp = Net::HTTP.get_response(URI.parse(url))
buffer = resp.body
@result = JSON.parse(buffer)
end
end
这是我的welcome / index.html.erb
<center>
<h2>Midcourse Project</h2><br>
<br>
<%= link_to "Ask Yoda How He Would Say It!", 'welcome/ask' %>
<br><br><br><br><br><br>
<%= image_tag "http://www.extension.zone/wp-content/uploads/2015/05/Giphy-logo.jpg", size: "200x100", :alt => 'logo' %>
</center>
欢迎/ ask.html.erb
<center>
<h1> I am yoda! What do you want me to say? </h1><br>
<%= form_tag "/welcome/yoda" %>
<%= text_field_tag :sentence %>
<%= submit_tag "Submit" %>
</center>
欢迎来到/ yoda.html.erb
<p> Yoda Says, "<%= p @response.raw_body %>"</p> <br>
<%= @gif %> <br>
<%= image_tag @gifpic %> <br>
<%= p @result %>
<br>
<%= link_to "Back", '/welcome/ask' %>
这是我的宝石文件
source 'https://rubygems.org'
# unirest http://unirest.io/ruby
gem 'unirest', '~> 1.1', '>= 1.1.2'
# HTTParty
#gem 'httparty', '~> 0.13.7'
# Gem from https://rubygems.org/gems/giphy/versions/3.0.0
# gifs https://github.com/sebasoga/giphy
gem 'giphy', '~> 3.0'
# Geocoding https://github.com/alexreisner/geocoder
gem 'geocoder'
# Yelp https://github.com/Yelp/yelp-ruby
gem 'yelp', require: 'yelp'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
这是routes.rb
Rails.application.routes.draw do
# get the yoda page
get 'welcome/yoda'
# post to the yoda page
post 'welcome/yoda'
# get the ask page
get 'welcome/ask'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
end
文档: 在github checkout / Giphy / GiphyAPI 或/ sebasoga / giphy
请帮我解决这个问题。
答案 0 :(得分:0)
答案:<%= image_tag @result["data"]["image_url"] %>
将此信息放在您希望显示的视图中。
将控制器welcome_controller.rb更改为
class WelcomeController < ApplicationController
def index
@ask
end
def ask
end
def yoda
#@voice = HTTParty.get('https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.++Oh+wait.').parsed_response
#sentence = "You+will+learn+how+to+speak+like+me+someday.++Oh+wait."
sentence = params[:sentence]
#render plain: ENV["YODA_KEY"]
@response = Unirest.get "https://yoda.p.mashape.com/yoda?sentence="+sentence,
headers:{
"X-Mashape-Key" => ENV["YODA_KEY"],
"Accept" => "text/plain",
}
url = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=yoda"
resp = Net::HTTP.get_response(URI.parse(url))
buffer = resp.body
@result = JSON.parse(buffer)
end
end
将view welcome / yoda.html.erb更改为
<p> Yoda Says, "<%= p @response.raw_body %>"</p> <br>
<%= image_tag @result["data"]["image_url"] %>
<br>
<%= link_to "Back", '/welcome/ask' %>