jalc 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fdce5e853ede1ac804e51da6a9bf0b7532837ff1498aa6add35eceaee2d0687
4
- data.tar.gz: ad553fdc076b4ae3ad9b17b22f2b0286f676a342bae260c890e9a9c4c304a773
3
+ metadata.gz: 0dc45fa2ad1bb26e588c4b99dcd4b79ce9ef6eb1b1ce758fc241b1aefcf553a1
4
+ data.tar.gz: 4ab4bd89829b7ad3af731359e9bf0424585e2991255e4a6e29564991937d94d5
5
5
  SHA512:
6
- metadata.gz: 40bda7b62fb1957ef8642c84df04d9181445f98762c130637e7f23140b7021efc8688159774456ea3a5d4318654988cfb68360d54b74ec81be20d8d3ad774fcb
7
- data.tar.gz: e372dc2f3c0ac7982b72f551a5f9901289a3c56229c6b4c1b892b3f5abcbf88553557aca262c15f81d62a7540a769432083086bf05ddbe0e0352e2885d070cf7
6
+ metadata.gz: b105bfbf4b983932942144f965d45016ed7d64df42448d425ac94d53479f030f9ddd9bbbc3bdfcb5951fc13365233cdb888fe04478a377caba897d9f5f5e6feb
7
+ data.tar.gz: 3465ba5121e720647d40f0dca8b8060990736ea0d712a06b0d6f926a5700966a731f28a1382c84562246498c188b0c22b9ba5d0c01384d27bb2a225e94735bfd
data/.rubocop.yml CHANGED
@@ -22,6 +22,9 @@ Naming/MethodParameterName:
22
22
  Style/Documentation:
23
23
  Enabled: false
24
24
 
25
+ Style/NumericLiterals:
26
+ Enabled: false
27
+
25
28
  Style/TrailingCommaInArguments:
26
29
  EnforcedStyleForMultiline: consistent_comma
27
30
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.1.0] - 2022-01-10
4
+
5
+ - Add JaLC::Registration for the registration API
6
+
3
7
  ## [1.0.0] - 2022-01-09
4
8
 
5
9
  - Initial release
data/README.md CHANGED
@@ -42,6 +42,40 @@ doi_res = JaLC::REST.doi(doi)
42
42
  doi_res.body['data']
43
43
  ```
44
44
 
45
+ ### JaLC Registration API
46
+
47
+ See https://japanlinkcenter.org/top/doc/JaLC_tech_interface_doc.pdf for API detail
48
+
49
+ ```ruby
50
+ require 'jalc'
51
+
52
+ JaLC::Registration.configure do |config|
53
+ config.id = 'sankichi92'
54
+ config.password = ENV['JALC_PASSWORD']
55
+ end
56
+
57
+ res = JaLC::Registration.post(File.open('/path/to/xml'))
58
+ # response.body is an instance of REXML::Document
59
+ res.body.root.elements['head/okcnt'].text #=> 1
60
+
61
+ # With XML head/result_method=2 (Async)
62
+ async_res = JaLC::Registration.post(StringIO.new(<<~XML))
63
+ <?xml version="1.0" encoding="UTF-8"?>
64
+ <root>
65
+ <head>
66
+ <result_method>2</result_method>
67
+ </head>
68
+ <body>
69
+ ...
70
+ </body
71
+ </root>
72
+ XML
73
+ exec_id = async_res.body.root.elements['head/exec_id'].text #=> 12345
74
+
75
+ result_res = JaLC::Registration.get_result(exec_id)
76
+ res.body.root.elements['head/status'].text
77
+ ```
78
+
45
79
  ## Development
46
80
 
47
81
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/jalc.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.metadata['homepage_uri'] = spec.homepage
17
17
  spec.metadata['source_code_uri'] = 'https://github.com/sankichi92/jalc-ruby'
18
18
  spec.metadata['changelog_uri'] = 'https://github.com/sankichi92/jalc-ruby/blob/main/CHANGELOG.md'
19
+ spec.metadata['github_repo'] = 'https://github.com/sankichi92/jalc-ruby.git'
19
20
 
20
21
  # Specify which files should be added to the gem when it is released.
21
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -30,6 +31,7 @@ Gem::Specification.new do |spec|
30
31
 
31
32
  # Uncomment to register a new dependency of your gem
32
33
  spec.add_dependency 'faraday', '>= 1.0', '< 3.0'
34
+ spec.add_dependency 'faraday-multipart', '~> 1.0'
33
35
 
34
36
  # For more information and examples about making a new gem, check out our
35
37
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday/multipart'
5
+
6
+ require_relative '../version'
7
+ require_relative 'middleware/parse_xml'
8
+ require_relative 'middleware/raise_error'
9
+
10
+ module JaLC
11
+ module Registration
12
+ BASE_URL = 'https://japanlinkcenter.org'
13
+
14
+ 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
20
+ end
21
+
22
+ def post(xml_file)
23
+ conn.post(
24
+ '/jalc/infoRegistry/registDataReceive/index',
25
+ {
26
+ login_id: Faraday::Multipart::ParamPart.new(@id, 'text/plain'),
27
+ login_password: Faraday::Multipart::ParamPart.new(@password, 'text/plain'),
28
+ fname: Faraday::Multipart::FilePart.new(xml_file, 'text/xml'),
29
+ },
30
+ )
31
+ end
32
+
33
+ def get_result(exec_id)
34
+ conn.get(
35
+ '/jalc/infoRegistry/registDataResult/index',
36
+ {
37
+ login_id: @id,
38
+ login_password: @password,
39
+ exec_id: exec_id,
40
+ },
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def conn
47
+ @conn ||= Faraday.new(
48
+ url: @base_url,
49
+ headers: { 'User-Agent' => "jalc-ruby v#{VERSION}" },
50
+ ) do |f|
51
+ f.request :multipart
52
+ f.use Middleware::RaiseError
53
+ f.use Middleware::ParseXML
54
+ f.response :raise_error
55
+ f.response :logger, @logger, { headers: false } if @logger
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module JaLC
6
+ module Registration
7
+ class Config
8
+ attr_accessor :id, :password, :logger
9
+
10
+ def initialize
11
+ @id = nil
12
+ @password = nil
13
+ @logger = ::Logger.new($stdout)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JaLC
4
+ module Registration
5
+ class Error < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rexml'
4
+
5
+ module JaLC
6
+ module Registration
7
+ module Middleware
8
+ class ParseXML < Faraday::Middleware
9
+ def on_complete(env)
10
+ env[:body] = REXML::Document.new(env[:body].to_s)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ require_relative '../error'
6
+
7
+ module JaLC
8
+ module Registration
9
+ module Middleware
10
+ class RaiseError < Faraday::Middleware
11
+ def on_complete(env)
12
+ case env.body.root.elements['head/errcd']&.text
13
+ when '*'
14
+ raise AuthenticationError,
15
+ 'Wrong login_id/login_password or non-registered IP address request (errcd=*)'
16
+ when '#'
17
+ raise InvalidXMLError, 'XML format is invalid or any required params are blank (errcd=#)'
18
+ when '+'
19
+ raise RegistrationError, 'Non-XML file, non UTF-8 encoding or other error (errcd=+)'
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ class RegistrationError < Error; end
26
+ class AuthenticationError < RegistrationError; end
27
+ class InvalidXMLError < RegistrationError; end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ require_relative 'registration/client'
6
+ require_relative 'registration/config'
7
+
8
+ module JaLC
9
+ module Registration
10
+ class << self
11
+ extend Forwardable
12
+
13
+ def_delegators :client, :post, :get_result
14
+
15
+ def configure
16
+ yield config
17
+ end
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+
23
+ private
24
+
25
+ def client
26
+ @client ||= Client.new(
27
+ id: config.id,
28
+ password: config.password,
29
+ logger: config.logger,
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,7 +4,7 @@ require 'uri'
4
4
 
5
5
  require 'faraday'
6
6
 
7
- require_relative 'response/raise_error'
7
+ require_relative 'middleware/raise_error'
8
8
  require_relative '../version'
9
9
 
10
10
  module JaLC
@@ -54,7 +54,7 @@ module JaLC
54
54
  url: @base_url,
55
55
  headers: { 'User-Agent' => "jalc-ruby v#{VERSION}" },
56
56
  ) do |f|
57
- f.use Response::RaiseError
57
+ f.use Middleware::RaiseError
58
58
  f.response :json
59
59
  f.response :logger, @logger, { headers: false } if @logger
60
60
  end
@@ -1,35 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'faraday'
4
-
5
3
  module JaLC
6
4
  module REST
7
- class Error < StandardError; end
8
-
9
- class HTTPError < Error
10
- attr_reader :response
11
-
12
- def initialize(msg = nil, response: nil)
13
- @response = response
14
- msg ||= response[:body].dig('message', 'errors', 'message') if response && response[:body].respond_to?(:dig)
15
- msg ||= "the server responded with status #{response[:status]}" if response
16
-
17
- super(msg)
18
- end
19
-
20
- def inspect
21
- if response
22
- %(#<#{self.class} response=#{response.inspect}>)
23
- else
24
- super
25
- end
26
- end
5
+ class Error < StandardError
27
6
  end
28
-
29
- class ClientError < HTTPError; end
30
- class BadRequestError < ClientError; end
31
- class ResourceNotFound < ClientError; end
32
- class ServerError < HTTPError; end
33
- class NilStatusError < ServerError; end
34
7
  end
35
8
  end
@@ -6,7 +6,7 @@ require_relative '../error'
6
6
 
7
7
  module JaLC
8
8
  module REST
9
- class Response
9
+ class Middleware
10
10
  class RaiseError < Faraday::Middleware
11
11
  def on_complete(env)
12
12
  case env[:status]
@@ -26,6 +26,8 @@ module JaLC
26
26
  end
27
27
  end
28
28
 
29
+ private
30
+
29
31
  def response_values(env)
30
32
  {
31
33
  status: env.status,
@@ -41,5 +43,31 @@ module JaLC
41
43
  end
42
44
  end
43
45
  end
46
+
47
+ class HTTPError < Error
48
+ attr_reader :response
49
+
50
+ def initialize(msg = nil, response: nil)
51
+ @response = response
52
+ msg ||= response[:body].dig('message', 'errors', 'message') if response && response[:body].respond_to?(:dig)
53
+ msg ||= "the server responded with status #{response[:status]}" if response
54
+
55
+ super(msg)
56
+ end
57
+
58
+ def inspect
59
+ if response
60
+ %(#<#{self.class} response=#{response.inspect}>)
61
+ else
62
+ super
63
+ end
64
+ end
65
+ end
66
+
67
+ class ClientError < HTTPError; end
68
+ class BadRequestError < ClientError; end
69
+ class ResourceNotFound < ClientError; end
70
+ class ServerError < HTTPError; end
71
+ class NilStatusError < ServerError; end
44
72
  end
45
73
  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.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/jalc.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'jalc/registration'
3
4
  require_relative 'jalc/rest'
4
5
  require_relative 'jalc/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jalc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Miyoshi
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday-multipart
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
33
47
  description:
34
48
  email:
35
49
  - takahiro-miyoshi@sankichi.net
@@ -49,11 +63,17 @@ files:
49
63
  - bin/setup
50
64
  - jalc.gemspec
51
65
  - lib/jalc.rb
66
+ - lib/jalc/registration.rb
67
+ - lib/jalc/registration/client.rb
68
+ - lib/jalc/registration/config.rb
69
+ - lib/jalc/registration/error.rb
70
+ - lib/jalc/registration/middleware/parse_xml.rb
71
+ - lib/jalc/registration/middleware/raise_error.rb
52
72
  - lib/jalc/rest.rb
53
73
  - lib/jalc/rest/client.rb
54
74
  - lib/jalc/rest/config.rb
55
75
  - lib/jalc/rest/error.rb
56
- - lib/jalc/rest/response/raise_error.rb
76
+ - lib/jalc/rest/middleware/raise_error.rb
57
77
  - lib/jalc/version.rb
58
78
  - sig/jalc/rest.rbs
59
79
  homepage: https://github.com/sankichi92/jalc-ruby
@@ -63,6 +83,7 @@ metadata:
63
83
  homepage_uri: https://github.com/sankichi92/jalc-ruby
64
84
  source_code_uri: https://github.com/sankichi92/jalc-ruby
65
85
  changelog_uri: https://github.com/sankichi92/jalc-ruby/blob/main/CHANGELOG.md
86
+ github_repo: https://github.com/sankichi92/jalc-ruby.git
66
87
  rubygems_mfa_required: 'true'
67
88
  post_install_message:
68
89
  rdoc_options: []