Try to implement using Rakuten product search API (easy)

Introduction

Rails 6.0.3.4

This time, I would like to record the procedure for implementing a super-simple search application using the Rakuten product search API. Perhaps it's the simplest way to do it, aside from being easy to understand ...? I think it is selfish. This time, error handling is omitted.

https://webservice.rakuten.co.jp/ Let's register from the above and get the application ID.

items controller creation

rails g controller items search

If you enter this command in the terminal, you can enter the controller or view (search.html.erb), It also describes the routing (modify the routing to your liking).

Describe the view

<h2 class="product-subtitle-explain">Let's search for products!</h2>

<%= form_with url:product_search_path, method: :get, local: true do |f| %>
<%= f.text_field :keyword, placeholder: "Please enter a search keyword", autocomplete: 'off' %>
<%= f.submit "Search" , :name => nil%>
<% end %>

<h2>search results</h2>
<%= render partial: 'item_list' %>

Is it something like this? Since there is nothing to save in particular, write the url in form_with as to which action to skip.

:Search word as keyword:Store in keyword. ← Important!



#### **` "Please enter a search keyword"As an example, I put the characters on the form. Also,`**
```placeholder


#### **` 'off'So, I have disabled form auto-completion.`**
```autocomplete


 Last but not least, I would like you to pay attention to this.

#### **` 'product_list' %>`**
```<%= render partial

 As a result, the part where the search results are displayed is skipped to another view file.
 So here, in app / views / item,

#### **`_item.list.Create erb. The search I made earlier.html.It's the same place as erb.`**

Next is the render destination _item.list.erb, I will write a description to display the result.

<% @items.first(10).each do |item| %>

  <img src="<%= item['smallImageUrls'][0] %>">

  <a href="<%= item['itemUrl']%>" target="_blank"><%= item['itemName']%></a>

  <%= item['itemPrice'] %>Circle
 
<% end %>

Looking from above, first

<% @items.first(10).each do |item| %>★<%end%>


 As a result, I described what I wanted to copy in the product.

#### **`.first(10)By doing so, the first 10 items that were caught in the search are displayed.`**

@itemsWill be defined in the search controller after this! (Even if you look at the view now, an error will occur)

item['smallImageUrls'][0] I'm getting the first image of an item in a small size. item['itemurl'] I'm getting the URL to go to the item details. target="_blank" When I click an item, I open another tab and go to Rakuten's product details page. item['itemname'] I'm getting the name of the item. ['itemprice'] I am getting the price of the item. You can also get various information about the item. ↓ https://webservice.rakuten.co.jp/api/ichibaitemsearch/

Controller description

https://webservice.rakuten.co.jp/sdk/ruby.html We will implement it based on.

gem 'rakuten_web_service'

To the gem file and `bundle install` Please restart the terminal. From here, we will enter the description of the items controller.

class ProductController < ApplicationController
  require 'rakuten_web_service'

  def search
    if params[:keyword].present?
      @items = RakutenWebService::Ichiba::Item.search(keyword: params[:keyword])
    else
      render :search
    end
  end
end

params[:keyword]Is search.html.Keywords are stored in the input form of erb.


 Check [: keyword] in params and store it in @items.

#### **`RakutenWebService::Ichiba::Item.search is a cliché. For more information`**

https://webservice.rakuten.co.jp/sdk/ruby.html Please take a look.

Application ID setting

Create rukuten.rb under config / initialize. I will describe the application ID in it The description method differs depending on the timing, so be sure to check the formula as well.

RakutenWebService.configure do |c|
  c.application_id = '*******************'
  c.affiliate_id = '*******************'← This is optional
end

When using environment variables:

RakutenWebService.configure do |c|
  c.application_id = ENV['RWS_APPLICATION_ID']
  c.affiliate_id = ENV["RWS_AFFILIATION_ID"]
end

After it works, set the environment variables even in the production environment. After preparing with HTML / CSS, I think it will probably look like this. products.gif

Recommended Posts

Try to implement using Rakuten product search API (easy)
[Java] I tried to implement Yahoo API product search
[Java] Try to implement using generics
Implement search function with Rakuten Books (DVD) API
Rails learning How to implement search function using ActiveModel
Try to implement tagging function using rails and js
Try to implement Yubaba in Ruby
Easy way to create a mapping class when using the API
Implement search functionality using LIKE clauses
Try to implement Yubaba in Java
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
Try to issue or get a card from Jave to Trello using API
How to implement search functionality in Rails
Try to implement n-ary addition in Java
Try using the Stream API in Java
Try using JSON format API in Java
[Swift] Easy to implement modal with PanModal
Try to implement UIAlertController by separating files
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
Try using the Emotion API from Android
[Java] New Yahoo! Product Search API Implementation
Try to make a timeline report of method execution time using JFR API
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Java] Create something like a product search API
Try local file search using Fess on CentOS7
Implement partial match search function without using Ransuck
Try local file search using Fess on CentOS8
Implement the product category function using ancestry ① (Preparation)
Try using GCP's Cloud Vision API in Java
How to implement the breadcrumb function using gretel
Try to implement login function with Spring Boot
Flow to implement image posting function using ActiveStorage
Try similar search of Image Search using Java SDK [Search]
(Android) Try to display static text using DataBinding
[API] I tried using the zip code search API
Try to display prime numbers using boolean type
Try to make a music player using Basic Player
Try to get redmine API key with ruby
Try to implement TCP / IP + NIO with JAVA
How to implement optimistic locking in REST API
Try using the COTOHA API parsing in Java
I tried to implement a server using Netty
[Java] How to operate List using Stream API