jalc 1.2.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96ec63df2a4d939e8eb433fff06fe70d84536174ab759755314dbf67fd21c900
4
- data.tar.gz: 25288558d0cfb086f73d72634051892d48bf9a21e64de7aa9bb06a72b44dc153
3
+ metadata.gz: a5925914098939b17ec3a6743ea89d5c4ffd2d0eee02a6953f82154b2c16f5fe
4
+ data.tar.gz: 4cd2066ea1a7c87959bb69ac6daa04dee66eaec8510a496630b06f4b33f4f55b
5
5
  SHA512:
6
- metadata.gz: 286ab06114bd1b84518239c7b4e801f67a1a275d9e8b9f11e379df59dd66aaa319743849735495c5420f2811f8d5dba879ed162a00a15f805403ab26fd71ca05
7
- data.tar.gz: b1fbb0ebed28715ea7dfaee774652eef36ccef399c2bc89c2fbe1d260e9a08d84937720fc144665afca3d8799ce325491ede9ce835701b27241e24623fdeec71
6
+ metadata.gz: ef5428a39e9a9164678f47f53ec0db79c15523f8f09d6f0e066f24d0071655834adb68476c120a262f2a9f24424235b03bce0462ab28193f7484f04aac91c77a
7
+ data.tar.gz: 907e630e36856c8624da546b67971c9e37937b68b560415dfb2309efb448de5ab7376b41d61ed82f97b25bdce4bf152a1cb9175dd3bd5526573409b038f3a81f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.1.0] - 2022-04-07
4
+
5
+ - Update the constructor arguments of REST::Client and Registration::Client
6
+
7
+ ## [2.0.1] - 2022-03-06
8
+
9
+ - Make the registration error messages Japanese
10
+
11
+ ## [2.0.0] - 2022-01-10
12
+
13
+ - Change the return values from `Faraday::Response` to its `#body`
14
+
3
15
  ## [1.2.0] - 2022-01-10
4
16
 
5
17
  - Filter password on logs
data/README.md CHANGED
@@ -31,15 +31,15 @@ JaLC::REST.config.logger = MyLogger.new
31
31
 
32
32
  # GET /prefixes
33
33
  prefixes_res = JaLC::REST.prefixes(ra: 'JaLC', sort: 'siteId', order: 'desc')
34
- prefix = prefixes_res.body['data']['items'].first['prefix'] #=> "10.123"
34
+ prefix = prefixes_res['data']['items'].first['prefix'] #=> "10.123"
35
35
 
36
36
  # GET /doilist/:prefix
37
37
  doilist_res = JaLC::REST.doilist(prefix, rows: 100)
38
- doi = doilist_res.body['data']['items'].last['dois']['doi'] #=> "10.123/abc"
38
+ doi = doilist_res['data']['items'].last['dois']['doi'] #=> "10.123/abc"
39
39
 
40
40
  # GET /dois/:doi
41
41
  doi_res = JaLC::REST.doi(doi)
42
- doi_res.body['data']
42
+ doi_res['data']
43
43
  ```
44
44
 
45
45
  ### JaLC Registration API
@@ -50,15 +50,14 @@ See https://japanlinkcenter.org/top/doc/JaLC_tech_interface_doc.pdf for API deta
50
50
  require 'jalc'
51
51
 
52
52
  JaLC::Registration.configure do |config|
53
+ config.base_url = 'https://japanlinkcenter.org' # default
53
54
  config.id = 'sankichi92'
54
55
  config.password = ENV['JALC_PASSWORD']
55
56
  config.logger = nil
56
57
  end
57
58
 
58
- res = JaLC::Registration.post(File.open('/path/to/xml'))
59
-
60
- # body is an REXML::Document
61
- res.body.root.elements['head/okcnt'].text #=> "1"
59
+ res = JaLC::Registration.post(File.open('/path/to/xml')) # returns a REXML::Document
60
+ res.root.elements['head/okcnt'].text #=> "1"
62
61
 
63
62
  # async registration (result_method=2)
64
63
  async_res = JaLC::Registration.post(StringIO.new(<<~XML))
@@ -73,10 +72,10 @@ async_res = JaLC::Registration.post(StringIO.new(<<~XML))
73
72
  </body
74
73
  </root>
75
74
  XML
76
- exec_id = async_res.body.root.elements['head/exec_id'].text #=> "12345"
75
+ exec_id = async_res.root.elements['head/exec_id'].text #=> "12345"
77
76
 
78
77
  result_res = JaLC::Registration.get_result(exec_id)
79
- result_res.body.root.elements['head/status'].text #=> "2"
78
+ result_res.root.elements['head/status'].text #=> "2"
80
79
  ```
81
80
 
82
81
  ## Development
@@ -9,51 +9,50 @@ require_relative 'middleware/raise_error'
9
9
 
10
10
  module JaLC
11
11
  module Registration
12
- BASE_URL = 'https://japanlinkcenter.org'
13
-
14
12
  class Client
15
- def initialize(id:, password:, logger: nil, base_url: BASE_URL)
16
- @id = id
17
- @password = password
18
- @logger = logger
19
- @base_url = base_url
13
+ attr_reader :config
14
+
15
+ def initialize(config)
16
+ @config = config
20
17
  end
21
18
 
22
19
  def post(xml_file)
23
- conn.post(
20
+ response = conn.post(
24
21
  '/jalc/infoRegistry/registDataReceive/index',
25
22
  {
26
- login_id: Faraday::Multipart::ParamPart.new(@id, 'text/plain'),
27
- login_password: Faraday::Multipart::ParamPart.new(@password, 'text/plain'),
23
+ login_id: Faraday::Multipart::ParamPart.new(config.id, 'text/plain'),
24
+ login_password: Faraday::Multipart::ParamPart.new(config.password, 'text/plain'),
28
25
  fname: Faraday::Multipart::FilePart.new(xml_file, 'text/xml'),
29
26
  },
30
27
  )
28
+ response.body
31
29
  end
32
30
 
33
31
  def get_result(exec_id)
34
- conn.get(
32
+ response = conn.get(
35
33
  '/jalc/infoRegistry/registDataResult/index',
36
34
  {
37
- login_id: @id,
38
- login_password: @password,
35
+ login_id: config.id,
36
+ login_password: config.password,
39
37
  exec_id: exec_id,
40
38
  },
41
39
  )
40
+ response.body
42
41
  end
43
42
 
44
43
  private
45
44
 
46
45
  def conn
47
46
  @conn ||= Faraday.new(
48
- url: @base_url,
47
+ url: config.base_url,
49
48
  headers: { 'User-Agent' => "jalc-ruby v#{VERSION}" },
50
49
  ) do |f|
51
50
  f.request :multipart
52
51
  f.use Middleware::RaiseError
53
52
  f.use Middleware::ParseXML
54
53
  f.response :raise_error
55
- if @logger
56
- f.response :logger, @logger, { headers: false } do |logger|
54
+ if config.logger
55
+ f.response :logger, config.logger, { headers: false } do |logger|
57
56
  logger.filter(/(password=)\w+/, '\1[FILTERED]')
58
57
  end
59
58
  end
@@ -5,9 +5,12 @@ require 'logger'
5
5
  module JaLC
6
6
  module Registration
7
7
  class Config
8
- attr_accessor :id, :password, :logger
8
+ DEFAULT_BASE_URL = 'https://japanlinkcenter.org'
9
+
10
+ attr_accessor :base_url, :id, :password, :logger
9
11
 
10
12
  def initialize
13
+ @base_url = DEFAULT_BASE_URL
11
14
  @id = nil
12
15
  @password = nil
13
16
  @logger = ::Logger.new($stdout)
@@ -12,11 +12,11 @@ module JaLC
12
12
  case env.body.root.elements['head/errcd']&.text
13
13
  when '*'
14
14
  raise AuthenticationError,
15
- 'Wrong login_id/login_password or non-registered IP address request (errcd=*)'
15
+ 'IDとパスワードの組合せが間違っているか、アクセス元のIPアドレスがJaLCに登録されているIPアドレスと異なっている可能性があります (errcd=*)'
16
16
  when '#'
17
- raise InvalidXMLError, 'XML format is invalid or any required params are blank (errcd=#)'
17
+ raise InvalidXMLError, 'XMLファイルの構造に誤りがあるか、login_id、login_passwd、fnameのパラメータに未設定のものがある可能性があります (errcd=#)'
18
18
  when '+'
19
- raise RegistrationError, 'Non-XML file, non UTF-8 encoding or other error (errcd=+)'
19
+ raise RegistrationError, 'fnameで指定したファイルがXMLファイルでないか、XMLファイルの文字コードがUTF-8でない可能性があります (errcd=+)'
20
20
  end
21
21
  end
22
22
  end
@@ -23,11 +23,7 @@ module JaLC
23
23
  private
24
24
 
25
25
  def client
26
- @client ||= Client.new(
27
- id: config.id,
28
- password: config.password,
29
- logger: config.logger,
30
- )
26
+ @client ||= Client.new(config)
31
27
  end
32
28
  end
33
29
  end
@@ -9,16 +9,15 @@ require_relative '../version'
9
9
 
10
10
  module JaLC
11
11
  module REST
12
- BASE_URL = 'https://api.japanlinkcenter.org'
13
-
14
12
  class Client
15
- def initialize(logger: nil, base_url: BASE_URL)
16
- @logger = logger
17
- @base_url = base_url
13
+ attr_reader :config
14
+
15
+ def initialize(config)
16
+ @config = config
18
17
  end
19
18
 
20
19
  def prefixes(ra: nil, sort: nil, order: nil)
21
- conn.get(
20
+ response = conn.get(
22
21
  '/prefixes',
23
22
  {
24
23
  ra: ra,
@@ -26,10 +25,11 @@ module JaLC
26
25
  order: order,
27
26
  }.compact,
28
27
  )
28
+ response.body
29
29
  end
30
30
 
31
31
  def doilist(prefix, from: nil, to: nil, rows: nil, page: nil, sort: nil, order: nil)
32
- conn.get(
32
+ response = conn.get(
33
33
  "/doilist/#{prefix}",
34
34
  {
35
35
  from: from,
@@ -40,23 +40,25 @@ module JaLC
40
40
  order: order,
41
41
  }.compact,
42
42
  )
43
+ response.body
43
44
  end
44
45
 
45
46
  def doi(doi)
46
47
  encoded_doi = URI.encode_www_form_component(URI.encode_www_form_component(doi))
47
- conn.get("/dois/#{encoded_doi}")
48
+ response = conn.get("/dois/#{encoded_doi}")
49
+ response.body
48
50
  end
49
51
 
50
52
  private
51
53
 
52
54
  def conn
53
55
  @conn ||= Faraday.new(
54
- url: @base_url,
56
+ url: config.base_url,
55
57
  headers: { 'User-Agent' => "jalc-ruby v#{VERSION}" },
56
58
  ) do |f|
57
59
  f.use Middleware::RaiseError
58
60
  f.response :json
59
- f.response :logger, @logger, { headers: false } if @logger
61
+ f.response :logger, config.logger, { headers: false } if config.logger
60
62
  end
61
63
  rescue Faraday::Error => e
62
64
  raise e unless e.message.match?(/is not registered/)
@@ -5,9 +5,12 @@ require 'logger'
5
5
  module JaLC
6
6
  module REST
7
7
  class Config
8
- attr_accessor :logger
8
+ DEFAULT_BASE_URL = 'https://api.japanlinkcenter.org'
9
+
10
+ attr_accessor :base_url, :logger
9
11
 
10
12
  def initialize
13
+ @base_url = DEFAULT_BASE_URL
11
14
  @logger = ::Logger.new($stdout)
12
15
  end
13
16
  end
data/lib/jalc/rest.rb CHANGED
@@ -23,7 +23,7 @@ module JaLC
23
23
  private
24
24
 
25
25
  def client
26
- @client ||= Client.new(logger: config.logger)
26
+ @client ||= Client.new(config)
27
27
  end
28
28
  end
29
29
  end
data/lib/jalc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JaLC
4
- VERSION = '1.2.0'
4
+ VERSION = '2.1.0'
5
5
  end
data/sig/jalc.rbs CHANGED
@@ -1,6 +1,7 @@
1
- type faraday_response = untyped
2
-
3
1
  module JaLC
2
+ interface _Logger
3
+ end
4
+
4
5
  VERSION: String
5
6
 
6
7
  module Registration
@@ -24,22 +25,25 @@ module JaLC
24
25
  class Client
25
26
  interface _IO
26
27
  end
28
+ type rexml_document = untyped
27
29
 
28
- def initialize: (id: String, password: String, ?logger: Logger?, ?base_url: String) -> void
29
- def post: (_IO xml_file) -> faraday_response
30
- def get_result: (Integer | String exec_id) -> faraday_response
30
+ def initialize: (Config config) -> void
31
+ def post: (_IO xml_file) -> rexml_document
32
+ def get_result: (Integer | String exec_id) -> rexml_document
31
33
  end
32
34
 
33
35
  class Config
34
36
  attr_accessor id: String?
35
37
  attr_accessor password: String?
36
- attr_accessor logger: Logger?
38
+ attr_accessor logger: _Logger?
37
39
 
38
40
  def initialize: -> void
39
41
  end
40
42
  end
41
43
 
42
44
  module REST
45
+ type response_body = Hash[String, untyped] | String | nil
46
+
43
47
  BASE_URL: String
44
48
 
45
49
  def self.configure: { (Config) -> void } -> void
@@ -49,8 +53,7 @@ module JaLC
49
53
  end
50
54
 
51
55
  class HTTPError < Error
52
- type faraday_header = untyped
53
- type response_hash = {status: Integer, headers: faraday_header, body: untyped, request: {method: Symbol, url: URI, headers: faraday_header, body: String?}}
56
+ type response_hash = {status: Integer, headers: Hash[String, String], body: response_body, request: {method: Symbol, url: URI, headers: Hash[String, String], body: String?}}
54
57
 
55
58
  attr_reader response: response_hash?
56
59
 
@@ -71,14 +74,14 @@ module JaLC
71
74
  end
72
75
 
73
76
  class Client
74
- def initialize: (?logger: Logger?, ?base_url: String) -> void
75
- def prefixes: (?ra: String?, ?sort: String?, ?order: String?) -> faraday_response
76
- def doilist: (String, ?from: String?, ?to: String?, ?rows: Integer? | String?, ?page: Integer? | String?, ?sort: String?, ?order: String?) -> faraday_response
77
- def doi: (String) -> faraday_response
77
+ def initialize: (Config config) -> void
78
+ def prefixes: (?ra: String?, ?sort: String?, ?order: String?) -> response_body
79
+ def doilist: (String prefix, ?from: String?, ?to: String?, ?rows: Integer? | String?, ?page: Integer? | String?, ?sort: String?, ?order: String?) -> response_body
80
+ def doi: (String doi) -> response_body
78
81
  end
79
82
 
80
83
  class Config
81
- attr_accessor logger: Logger?
84
+ attr_accessor logger: _Logger?
82
85
 
83
86
  def initialize: -> void
84
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jalc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Miyoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-10 00:00:00.000000000 Z
11
+ date: 2022-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
116
  requirements: []
117
- rubygems_version: 3.3.3
117
+ rubygems_version: 3.3.7
118
118
  signing_key:
119
119
  specification_version: 4
120
120
  summary: JaLC (Japan Link Center) API Client