使用HTTParty使用RESTFul Web服务Ruby on Rails 4

时间:2014-06-06 20:48:18

标签: ruby-on-rails json rest httparty

我目前正在开发Ruby on Rails应用程序。 主要功能是具有一个输入文本字段的搜索表单。 然后输入应该用作RESTful Web服务的请求参数。

网络服务&#39; base uri是https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/。 请求获取两个参数,一个String作为搜索输入和一个api_key。 示例请求如下所示:curl --request GET 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/<name>?api_key=<api_key>' --include

Web服务以JSON格式提供响应。

我的初始实现如下:

我创建了一个包装webservice调用的类:

/app/models/summoner.rb
# This class encapsulates the riot summoner webservice api    
class Summoner 
    include HTTParty 

    # The webservice' base_uri
    base_uri "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name"     

    # The webservice' response format
    format :json     

    # The api_key used for the webservice call
    @api_key = "<my_api_key>"    

    # Create attributes and accessors for each of the response attributes
    attr_accessor :id, :name, :profile_icon_id, :revision_date, :summoner_level    

    # Create a new Summor with Summor.new
    def initialize(id, name, profile_icon_id, revision_date, summoner_level)
        self.id = id
        self.name = name
        self.profile_icon_id = profile_icon_id
        self.revision_date = revision_date
        self. summoner_level = summoner_level
    end 

    # The find method called with the input of the controller
    def self.find(summoner_name)
        # Create the response, therefore build the parameters
        response = get("/#{summoner_name}?api_key=" + @api_key)
        if response.success?
              # Yay, we got a result! Lets create a new Summoner then!
              self.new(response["id"], response["name"], response["profileIconId"], response["revisionDate"], response["summonerLevel"])
        else
             # Something went wrong, raise it to show!
             raise response.response
    end
end

结束


相应的控制器在这里:

/app/controllers/pages_controller.rb
# This class defines the controller for pages and their corresponding actions
class PagesController < ApplicationController
    before_action :set_summoner, only: [:home]

# The home action, if search params have been set, execute Summoner.find
def home
    if search_params
        logger.debug "Got params: #{search_params}"
        @summoner = Summoner.find(search_params)
    else
        logger.debug "Got no params!"
    end
end

private
    # Use callbacks to share common setup or constraints between actions.
    def set_summoner
        @summoner = Summoner.find(params[:summoner_name])
    end

    # Never trust parameters from the scary internet, only allow the white list   through.
    def search_params
        params.permit(:summoner_name)
    end
end

最后是用于呈现搜索表单和搜索结果的视图:

/app/view/pages/home.html.erb
<!-- Create a form to wrap around the search field -->
<%= form_tag(root_path, :method => "get", id: "search-form") do %>
    <%= text_field_tag :summoner_name, params[:summoner_name], placeholder: "Summoner Name" %>
<%= submit_tag "Search", :summoner_name => nil %>
<% end %>
<!-- If the controller has a summoner, render the summonerss id -->
<% if @summoner %>
    <%= form_for @summoner do |f| %>
    <%= f.text_field :id %>
<% end %>
<% end %>

我最初的实施是否朝着正确的方向前进?

如果我使用curl命令进行webservice调用,它可以正常工作,并将URL粘贴到浏览器中。 但是,如果我在rails应用程序中使用它,我会收到一个错误: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

我感谢任何评论/批评!

问候

0 个答案:

没有答案