jisho_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +2 -0
- data/TODO +0 -0
- data/bin/console +9 -0
- data/bin/setup +8 -0
- data/data/sample.json +2250 -0
- data/jisho_api.gemspec +28 -0
- data/lib/jisho_api/client.rb +53 -0
- data/lib/jisho_api/version.rb +3 -0
- data/lib/jisho_api.rb +46 -0
- metadata +89 -0
data/jisho_api.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "jisho_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "jisho_api"
|
7
|
+
spec.version = JishoAPI::VERSION
|
8
|
+
spec.authors = ["tomholford"]
|
9
|
+
spec.email = ["tomholford@users.noreply.github.com"]
|
10
|
+
|
11
|
+
spec.summary = 'Ruby client gem for the unofficial Jisho API.'
|
12
|
+
spec.homepage = "https://github.com/tomholford/jisho-api"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = 'https://github.com/tomholford/twitter-labs-api/blob/master/CHANGELOG.md'
|
18
|
+
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module JishoAPI
|
4
|
+
class APIError < StandardError; end
|
5
|
+
|
6
|
+
class Client
|
7
|
+
attr_accessor :response
|
8
|
+
|
9
|
+
URI = 'https://jisho.org/api/v1/search/words'.freeze
|
10
|
+
|
11
|
+
def initialize; end
|
12
|
+
|
13
|
+
def make_request(params: {})
|
14
|
+
params.merge!(keyword: query) unless params.key?(:keyword)
|
15
|
+
params.merge!(page: page) unless params.key?(:page)
|
16
|
+
|
17
|
+
self.response = connection.get do |req|
|
18
|
+
req.params = params
|
19
|
+
end
|
20
|
+
|
21
|
+
handle_response
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def connection
|
27
|
+
@connection ||= Faraday.new(
|
28
|
+
url: URI,
|
29
|
+
headers: { 'Content-Type' => 'application/json' }
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def handle_response
|
34
|
+
if response.status == 200 && api_status == 200
|
35
|
+
api_data
|
36
|
+
else
|
37
|
+
raise APIError, "#{response.status} response from API"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def api_status
|
42
|
+
parsed_response[:meta][:status]
|
43
|
+
end
|
44
|
+
|
45
|
+
def api_data
|
46
|
+
parsed_response[:data]
|
47
|
+
end
|
48
|
+
|
49
|
+
def parsed_response
|
50
|
+
@parsed_response ||= JSON.parse(response.body).with_indifferent_access
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/jisho_api.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
|
4
|
+
require 'jisho_api/version'
|
5
|
+
require 'jisho_api/client'
|
6
|
+
|
7
|
+
module JishoAPI
|
8
|
+
# Ruby gem for the unofficial Jisho API.
|
9
|
+
class JishoAPI
|
10
|
+
attr_accessor :page, :query
|
11
|
+
|
12
|
+
def initialize(query: nil, page: 1, debug: false)
|
13
|
+
@query = query
|
14
|
+
@page = page
|
15
|
+
require 'httplog' if debug
|
16
|
+
end
|
17
|
+
|
18
|
+
# Query using the query and page set initially, but can be overridden by passing in args.
|
19
|
+
# @param query (string) Search the API for a given query.
|
20
|
+
# @param page (integer) Which page to fetch; defaults to 1
|
21
|
+
def search(query: nil, page: nil)
|
22
|
+
query ||= self.query
|
23
|
+
page ||= self.page
|
24
|
+
|
25
|
+
client.make_request(params: { keyword: query, page: page })
|
26
|
+
end
|
27
|
+
|
28
|
+
# Fetches the next page of results, reusing the same query. Increments the internal page counter as a side effect.
|
29
|
+
def next_page!
|
30
|
+
self.page += 1
|
31
|
+
|
32
|
+
client.make_request(params: { keyword: query, page: page })
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param query (string) Search the API for a given query and fetches the first page of results;
|
36
|
+
def self.search(query)
|
37
|
+
new.search(query: query)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def client
|
43
|
+
@client ||= Client.new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jisho_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tomholford
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- tomholford@users.noreply.github.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CHANGELOG.md
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- TODO
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- data/sample.json
|
59
|
+
- jisho_api.gemspec
|
60
|
+
- lib/jisho_api.rb
|
61
|
+
- lib/jisho_api/client.rb
|
62
|
+
- lib/jisho_api/version.rb
|
63
|
+
homepage: https://github.com/tomholford/jisho-api
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
homepage_uri: https://github.com/tomholford/jisho-api
|
68
|
+
source_code_uri: https://github.com/tomholford/jisho-api
|
69
|
+
changelog_uri: https://github.com/tomholford/twitter-labs-api/blob/master/CHANGELOG.md
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.0.3
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Ruby client gem for the unofficial Jisho API.
|
89
|
+
test_files: []
|