adding the isbn_search client

trunk
Thomas Riboulet 3 years ago
parent f8be3938f6
commit 7fac0781f3

@ -29,6 +29,7 @@ gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.4', require: false
gem 'sidekiq'
gem 'excon'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

@ -70,6 +70,7 @@ GEM
crass (1.0.6)
diff-lcs (1.4.4)
erubi (1.10.0)
excon (0.85.0)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
@ -203,6 +204,7 @@ PLATFORMS
DEPENDENCIES
bootsnap (>= 1.4.4)
byebug
excon
jbuilder (~> 2.7)
listen (~> 3.3)
pg (~> 1.1)

@ -21,11 +21,11 @@ class BooksController < ApplicationController
# POST /books or /books.json
def create
@book = Book.new(book_params)
@book = Book.new(book_info)
respond_to do |format|
if @book.save
NotifyWorker.perform_async(book_params[:title], book_params[:author])
NotifyWorker.perform_async(book_info[:title], book_info[:author])
format.html { redirect_to @book, notice: "Book was successfully created." }
format.json { render :show, status: :created, location: @book }
else
@ -38,7 +38,7 @@ class BooksController < ApplicationController
# PATCH/PUT /books/1 or /books/1.json
def update
respond_to do |format|
if @book.update(book_params)
if @book.update(book_info)
format.html { redirect_to @book, notice: "Book was successfully updated." }
format.json { render :show, status: :ok, location: @book }
else
@ -58,13 +58,16 @@ class BooksController < ApplicationController
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Only allow a list of trusted parameters through.
def book_params
params.require(:book).permit(:title, :author, :description)
end
def isbn_params
params.require(:book).permit(:isbn)
end
def book_info
Internal::IsbnSearch::Client.new.get(isbn_params[:isbn])
end
end

@ -12,18 +12,8 @@
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :author %>
<%= form.text_field :author %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description %>
<%= form.label :isbn %>
<%= form.text_field :isbn %>
</div>
<div class="actions">

@ -24,6 +24,8 @@ module Backend
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
config.autoload_paths += %W(#{config.root}/lib)
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files

@ -13,6 +13,12 @@ services:
image: redis:6.2.5
ports:
- "6379:6379"
isbn_search:
build: ../isbn-search/
environment:
- PORT=3001
ports:
- "3001:3001"
volumes:
data-postgres:
driver: local

@ -0,0 +1,26 @@
module Internal
module IsbnSearch
class Client
def initialize
@port = ENV.fetch('ISBN_SEARCH_PORT', 3001)
@host = ENV.fetch('ISBN_SEARCH_HOST', '0.0.0.0')
@path = 'isbn/'
end
def get(isbn)
response = Excon.get(url_for(isbn))
if response.status == 200
JSON.parse(response.body)
else
nil
end
end
private
def url_for(isbn)
"http://#{@host}:#{@port}/#{@path}/#{isbn}"
end
end
end
end
Loading…
Cancel
Save