What I want to do
I want to get the response of book information by hitting google books api from Book model of Rails
The error that occurred
web_1 | Errno::EADDRNOTAVAIL (Failed to open TCP connection to :80 (Address not available-connect(2) for nil port 80)):
web_1 |
web_1 | app/models/book.rb:24:in `search'
web_1 | app/controllers/books_controller.rb:3:in `search'
code
Hit the API using the keywords (book name, author, etc.) sent from the form.
class BooksController <ApplicationController
def search
@results = Book.search(params[:book_search][:search_key_word])
end
end
Since it contains non-ASCII characters, it is encoded using the url_encode
method.
class Book <ApplicationRecord
class << self
def search(key_word)
api_endpoint = ERB::Util.url_encode("https://www.googleapis.com/books/v1/volumes?q=#{key_word}&maxResults=20")
response = Net::HTTP.get(URI.parse(api_endpoint)) # I'm getting the above error.
JSON.parse(response)
end
end
end
Executing the curl command for the same endpoint will of course give the proper response.
Is there something to do with using docker? It is based on alpine linux. https://hub.docker.com/layers/ruby/library/ruby/2.6.6-alpine/images/sha256-95d5b6bf7084a6a0f04b48a7fc1c533ccb15e1550c670ff589ef56e620f40286?context=explore
Environment
- docker 2.3.0.4
- docker-compose 3.8
- rails container base image ruby:2.6.6-alpine
- ruby 2.6.6
- rails 6.0.3.2
I would be grateful if you could teach me if you have any idea.
Thank you for your cooperation.