1

我让翻译 yoda API 可以从 mashape 工作。https://github.com/sfinley89/BEWD_midcourse 现在我试图让随机的 yoda giphy 图像在他们的文本被翻译成 yoda speak 时显示在“欢迎/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

这是我的欢迎/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 上结帐 /Giphy/GiphyAPI 或 /sebasoga/giphy

这个你能帮我吗。

4

1 回答 1

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

更改视图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' %>
于 2015-11-07T20:40:42.340 回答