Nice to meet you, I'm a new graduate first year programming beginner Ota! !! I mainly use ruby on rails in the company, and I still have a sweet knowledge of ruby, so I created a gem as a study! I thought it could be used for something in the future, so this is a memorandum of it.
The gem created this time used the GoogleBooks API to search for books and output it as json. (I hope it can be used for something in the future ... w) Also, please note that the created gem has not been released until it is released.
For Google Books API, I referred to this article. The official reference is here
ruby 2.6.2
Bundler 2.2.4
irb 1.0.0
Since these are used, it is necessary to install gem etc.
--New repository on github
It seems that gem works depending on git, so I will create it. (It was written in some article)
Create the foundation using the bundler command.
You can create it with bundle gem gem_name
.
This time it is a book search, so I created it with a simple name of book_search. (I regret that book_search_json was okay because it is json output)
$ bundle gem book_search
This will automatically generate the file.
|--bin
| |--console
| |--setup
|--lib
| |--books_search.rb
| |--books_search
| | |--version.rb
|--.gitignore
|--.rubocop.yml
|--books_search.gemspec
|--CODE_OF_CONDUCT.md
|--Gemfile
|--Gemfile.lock
|--LICENSE.txt
|--README.md
|--Rakefile
(Don't worry because rubocop is originally included in the gem ;;)
※ Notes
Note that if you add -
in between when naming the gem, the directory structure in lib will change. (I was addicted to it once here.)
--Example
$ bundle gem books-search
|--lib
| |--books
| | |--search
| | |--search.rb
| | | |--version.rb
Please note that it will look like this.
Write here with TODO on the automatically generated gemspec! Since there is a part that is said to be, I will add it there.
If you just bundle install
, you will get an error.
Click here for the part you changed
books_search.gemspec
#Overview
spec.summary = "GoogleBooksAPI use."
#Explanation
spec.description = "It uses google to search for books."
#Appropriately my github account
spec.homepage = "https://github.com/yuki-ohta0086"
#here"https://rubygems.org"OK
spec.metadata["allowed_push_host"] = "https://rubygems.org"
# "source_code_uri"When"source_code_uri"Enter the URL of the created github repository
spec.metadata["source_code_uri"] = "https://github.com/yuki-ohta0086/books_search"
spec.metadata["source_code_uri"] = "https://github.com/yuki-ohta0086/books_search"
This is OK for the TODO part. After that, I will write the gems necessary for the environment.
This time we need bundler
and rake
, so add them. (If you want to write a test, add rspec or minitest.)
books_search.gemspec
spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "rake", "~> 13.1"
spec.add_development_dependency "rspec", "~> 3.0"
When you finish writing everything, do bundle install
.
$ bundle install
If it succeeds, I will push it for the time being. (Just in case)
Finally the main subject. I will write the processing contents in books_search.rb in lib! !! This time, I will write the process because it only outputs json using Google Books API based on the keyword.
books_search.rb
require 'net/http'
require 'uri'
require 'json'
require_relative 'books_search/version'
module BooksSearch
class Error < StandardError; end
#volume search API URL
GOOGLEAPI_URL = 'https://www.googleapis.com/books/v1/volumes?q='
#Since it is a module, it cannot be called unless a class method is created, so create it.
class << self
def get_book_json(params)
uri = URI.parse(GOOGLEAPI_URL + params)
resources = Net::HTTP.get(uri)
puts resources
end
end
end
I wanted a get request, so I looked it up and found a library called net/http
, so I used that.
As the processing content
--Parse the character string created by GOOGLEAPI_URL + params
and convert it to uri.
--Get request it and output the returned json
Only this.
If you want to use it properly, you can convert this json to a ruby object and use it in various ways. However, this time I just want to see the json output, so I leave it as it is. If you do, I think it will look like this.
books_search.rb
def get_book_json(params)
uri = URI.parse(GOOGLEAPI_URL + params)
resources = Net::HTTP.get(uri)
json = JSON.parse(resources)
end
You can finally install your own gem! !!
Try installing and checking locally with bundle exec rake install
.
$ bundle exec rake install
books_search 0.1.0 built to pkg/books_search-0.1.0.gem.
books_search (0.1.0) installed.
I build it with rake and just gem install
.
Check with irb when you're done.
$ bundle exec irb
irb(main):001:0> require 'books_search'
=> true
irb(main):002:0> BooksSearch.get_book_json('ruby')
{
"kind": "books#volumes",
"totalItems": 731,
"items": [
{
"kind": "books#volume",
"id": "Dif2bl2KRUYC",
"etag": "pmkr/T2kh1g",
"selfLink": "https://www.googleapis.com/books/v1/volumes/Dif2bl2KRUYC",
"volumeInfo": {
"title": "Programming Ruby 1.9 languages",
"authors": [
"Chad Fowler",
"Andy Hunt"
],
~The following is omitted~
I was able to do it like this!
Completed
If you do the actual upload
$ rake release
It seems that you can do it with. For details, see Rubygems. (Honestly, this was the easiest to understand)
I think that it is very convenient because the output output by the Google Books API can output thumbnails of images. However, as a drawback, it seems that you can only issue up to 1000 cases a day, so be careful.
I made the gem for the first time this time, but it was surprisingly easy to make! Also, I am very glad that the mechanism of ruby and new knowledge have increased.
I wish I could try various things for myself and gain strength! !!
-[How to use Google Books API ~ Part 4 ~] (https://qiita.com/stella0270/items/8fdd2efcdd9db96a5c4d) -[Ruby] From how to create gem to release
Recommended Posts