Amazon APIの検索バリエーションを増やす – ca008

キーワード以外にも、「本」「DVD」といった項目、および「売れている順」「発売日順」などのソート順を指定してAmazon検索ができるように拡張する。

■検索用フォームの追加

cpro01\app\views\top\index.html.erbを編集し、検索入力項目を追加する。

Ruby

<% # ---- cpro01\app\views\top\index.html.erb ---- %>

<h1>Top#index</h1>
<p>Find me in app/views/top/index.html.erb</p>
<%= @gmap.div(:width => 550, :height => 400) %>

<p>Amazonから登録図書を検索</p>
<% form_tag :controller => 'books', :action => 'search' do %>
  タイトル:<%= text_field_tag "keyword", @keyword %><br />
    分類:<%= radio_button_tag "search_index", "Books", checked=true %>本
  <%= radio_button_tag "search_index", "DVD" %>DVD
  <%= radio_button_tag "search_index", "Music" %>音楽
  <%= radio_button_tag "search_index", "Software" %>ソフトウェア
  <%= radio_button_tag "search_index", "VideoGames" %>ゲーム<br />

  検索順:<%= radio_button_tag "sort", "salesrank", checked=true %>売れている順
  <%= radio_button_tag "sort", "releasedate" %>発売日順
  <%= radio_button_tag "sort", "titlerank" %>アルファベット順(AからZへ)
  <%= radio_button_tag "sort", "-titlerank" %>アルファベット順(ZからAへ)<br />
  <%= submit_tag "アマゾンで検索" %><br />
<% end %>

■BooksControllerを編集

入力項目処理を追加

Ruby

# ---- cpro01\app\controllers\books_controller.rb ----

class BooksController < ApplicationController
  # GET /books
  # GET /books.xml
  def index
    @books = Book.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @books }
    end
  end

  # GET /books/1
  # GET /books/1.xml
  def show
    @book = Book.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/new
  # GET /books/new.xml
  def new
    @book = Book.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/1/edit
  def edit
    @book = Book.find(params[:id])
  end

  # POST /books
  # POST /books.xml
  def create
    @book = Book.new(params[:book])

    respond_to do |format|
      if @book.save
        flash[:notice] = 'Book was successfully created.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created, :location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /books/1
  # PUT /books/1.xml
  def update
    @book = Book.find(params[:id])

    respond_to do |format|
      if @book.update_attributes(params[:book])
        flash[:notice] = 'Book was successfully updated.'
        format.html { redirect_to(@book) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.xml
  def destroy
    @book = Book.find(params[:id])
    @book.destroy

    respond_to do |format|
      format.html { redirect_to(books_url) }
      format.xml  { head :ok }
    end
  end

  def search
    @searchck = {"Books" => false, "DVD" => false, "Music" => false, "Software" => false, "VideoGames" => false}
    @sortck = {"salesrank" => false, "releasedate" => false, "titlerank" => false, "-titlerank" => false}

    @keyword = params[:keyword]
    @search_index = params[:search_index]
    @sort = params[:sort]

    #デフォルトチェック用
    @searchck["#{@search_index}"] = true
    @sortck["#{@sort}"] = true

    #本だけはソート用のキーが他の物と違うため変更
    @sort = "daterank" if @search_index == "Books" && @sort == "releasedate"

    #検索
    res = Amazon::Ecs.item_search(@keyword, {
      :search_index => @search_index,
      :response_group => 'Medium',
      :sort => @sort
    })
    @items = []
    res.items.each do |item|
      @items << Item.new(
        item.get('asin'),
        item.get('itemattributes/title'),
        item.get('detailpageurl'),
        item.get_hash('smallimage'),
        item.get('author')
      )
    end
  end
end

■検索結果を変更

次の地図登録ページへの遷移用のボタンを追加

Ruby

<% # ---- cpro01\app\views\books\search.html.erb ---- %>

<p>検索結果</p>
<% if @items && @items.size > 0 %>
<table>
  <% @items.each do |item| %>

  <tr>
    <td><%= item.title %><br />
        <% if item.image != nil %>
          <%= link_to(image_tag(item.image[:url], :size => "#{item.image[:width]}x#{item.image[:height]}", :alt => item.title), item.url, :target => '_blank') %>
        <% end %><br />
        <%= item.asin %><br />
        <%= item.author %>
      <% form_tag :controller => "maps", :action => "show" do %>
          <%= hidden_field_tag("asin", item.asin) %>
          <%= hidden_field_tag("address", "新宿") %>
          <%= submit_tag("この本の地図データを登録") %>
      <% end %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

Post a Comment