sip2 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 9d87384752360718cc923357a88f890ab47ad5a3
4
- data.tar.gz: 8adea44884170a7c508ca20e9fa4e074f7ef0546
3
+ metadata.gz: f3b0916dbfd4d2c9b14716d3a245e249229a85a2
4
+ data.tar.gz: 4be89cf798d034635dbf3a330c54fb9d44640cd4
5
5
  SHA512:
6
- metadata.gz: 26c9cb0b476a7c35939b14975a241b3e1d73a0fecdc2bdaecb20f3cec1f570ce8aae649d7d810a2cc246a2a138ac1037a26a7dd4828f7fb7be02b787e11422f8
7
- data.tar.gz: f77ac5118fe0ab0f2ea2db4acf622424168f872f475ac7331e6e5e6ee63c618a6b014da3de12c4fc11b152cb82c73105fde8e9ecf63d38c3a1710b3d8d048c5e
6
+ metadata.gz: 4e6e118916e0f2ec98fd1f2d129b5b522201684b62f925908d3ecbcfc88bed2e688008171abd0a0c723c3d6f45e881496588dc3c8e2bb87a52956803680a2186
7
+ data.tar.gz: 9910a5f01b3c1696a292788ae01c68f83ffccb9f1acd4a2d8112973373f263660a646dc35f84b481f7f8094731472d3b09cba986de96ffc9bc29b499ea5aa1ef
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
3
  - 2.1.0
5
4
  - 2.2.5
6
5
  - 2.3.0
data/README.md CHANGED
@@ -26,7 +26,17 @@ $ bundle
26
26
 
27
27
  ## Usage
28
28
 
29
- TODO
29
+ ```ruby
30
+ client = Sip2::Client.new(host: 'my.sip2.host.net', port: 6001)
31
+ patron =
32
+ client.connect do |connection|
33
+ if connection.login 'sip_username', 'sip_password'
34
+ connection.patron_information 'patron_username', 'patron_password'
35
+ end
36
+ end
37
+
38
+ puts 'Valid patron' if patron && patron.authenticated?
39
+ ```
30
40
 
31
41
 
32
42
  ## Contributing
@@ -2,6 +2,9 @@ require 'sip2/version'
2
2
 
3
3
  require 'sip2/patron_information'
4
4
 
5
+ require 'sip2/messages/login'
6
+ require 'sip2/messages/patron_information'
7
+
5
8
  require 'sip2/non_blocking_socket'
6
9
  require 'sip2/connection'
7
10
  require 'sip2/client'
@@ -3,36 +3,51 @@ module Sip2
3
3
  # Sip2 Connection
4
4
  #
5
5
  class Connection
6
+ @connection_modules = []
7
+
8
+ class << self
9
+ attr_reader :connection_modules
10
+
11
+ def add_connection_module(module_name)
12
+ @connection_modules << module_name
13
+ end
14
+ end
15
+
16
+ include Messages::Login
17
+ include Messages::PatronInformation
18
+
6
19
  def initialize(socket, ignore_error_detection)
7
20
  @socket = socket
8
21
  @ignore_error_detection = ignore_error_detection
9
22
  @sequence = 1
10
23
  end
11
24
 
12
- def login(username, password, location_code = nil)
13
- login_message = build_login_message(username, password, location_code)
14
- response = send_message login_message
15
- login_successful? response
25
+ def method_missing(method_name, *args)
26
+ if Connection.connection_modules.include?(method_name.to_sym)
27
+ send_and_handle_message(method_name, *args)
28
+ else
29
+ super
30
+ end
16
31
  end
17
32
 
18
- def patron_information(patron_uid, password)
19
- patron_message = build_patron_message(patron_uid, password)
20
- response = send_message patron_message
21
- return unless sequence_and_checksum_valid?(response)
22
- PatronInformation.new response
33
+ def respond_to_missing?(method_name, _include_private = false)
34
+ Connection.connection_modules.include?(method_name.to_sym) || super
23
35
  end
24
36
 
25
37
  private
26
38
 
39
+ def send_and_handle_message(message_type, *args)
40
+ message = send "build_#{message_type}_message", *args
41
+ message = with_checksum with_error_detection message
42
+ response = send_message message
43
+ send "handle_#{message_type}_response", response
44
+ end
45
+
27
46
  def send_message(message)
28
47
  @socket.send(message + "\r", 0)
29
48
  @socket.gets "\r"
30
49
  end
31
50
 
32
- def with_error_detection_and_checksum(message)
33
- with_checksum with_error_detection message
34
- end
35
-
36
51
  def with_error_detection(message)
37
52
  message + '|AY' + @sequence.to_s
38
53
  end
@@ -60,33 +75,5 @@ module Sip2
60
75
  ensure
61
76
  @sequence += 1
62
77
  end
63
-
64
- def build_login_message(username, password, location_code)
65
- code = '93' # Login
66
- uid_algorithm = pw_algorithm = '0' # Plain text
67
- username_field = 'CN' + username
68
- password_field = 'CO' + password
69
- location_code = location_code.strip if location_code.is_a? String
70
- location_field = location_code ? "|CP#{location_code}" : ''
71
-
72
- message = [
73
- code, uid_algorithm, pw_algorithm, username_field, '|', password_field, location_field
74
- ].join
75
-
76
- with_error_detection_and_checksum message
77
- end
78
-
79
- def login_successful?(response)
80
- sequence_and_checksum_valid?(response) && response[/^94([01])AY/, 1] == '1'
81
- end
82
-
83
- def build_patron_message(uid, password)
84
- code = '63' # Patron information
85
- language = '000' # Unknown
86
- timestamp = Time.now.strftime('%Y%m%d %H%M%S')
87
- summary = ' ' * 10
88
- message = "#{code}#{language}#{timestamp}#{summary}AO|AA#{uid}|AC|AD#{password}"
89
- with_error_detection_and_checksum message
90
- end
91
78
  end
92
79
  end
@@ -0,0 +1,31 @@
1
+ module Sip2
2
+ module Messages
3
+ #
4
+ # Sip2 Login message module
5
+ #
6
+ module Login
7
+ def self.included(klass)
8
+ klass.add_connection_module :login
9
+ end
10
+
11
+ private
12
+
13
+ def build_login_message(username, password, location_code = nil)
14
+ code = '93' # Login
15
+ uid_algorithm = pw_algorithm = '0' # Plain text
16
+ username_field = 'CN' + username
17
+ password_field = 'CO' + password
18
+ location_code = location_code.strip if location_code.is_a? String
19
+ location_field = location_code ? "|CP#{location_code}" : ''
20
+
21
+ [
22
+ code, uid_algorithm, pw_algorithm, username_field, '|', password_field, location_field
23
+ ].join
24
+ end
25
+
26
+ def handle_login_response(response)
27
+ sequence_and_checksum_valid?(response) && response[/^94([01])AY/, 1] == '1'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Sip2
2
+ module Messages
3
+ #
4
+ # Sip2 Patron information message module
5
+ #
6
+ module PatronInformation
7
+ def self.included(klass)
8
+ klass.add_connection_module :patron_information
9
+ end
10
+
11
+ private
12
+
13
+ def build_patron_information_message(uid, password)
14
+ code = '63' # Patron information
15
+ language = '000' # Unknown
16
+ timestamp = Time.now.strftime('%Y%m%d %H%M%S')
17
+ summary = ' ' * 10
18
+ [code, language, timestamp, summary, 'AO|AA', uid, '|AC|AD', password].join
19
+ end
20
+
21
+ def handle_patron_information_response(response)
22
+ return unless sequence_and_checksum_valid?(response)
23
+ Sip2::PatronInformation.new response
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Sip2
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.required_ruby_version = '>= 2.0.0'
22
+ spec.required_ruby_version = '>= 2.1.0'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.11'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sip2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - abrom
@@ -97,6 +97,8 @@ files:
97
97
  - lib/sip2.rb
98
98
  - lib/sip2/client.rb
99
99
  - lib/sip2/connection.rb
100
+ - lib/sip2/messages/login.rb
101
+ - lib/sip2/messages/patron_information.rb
100
102
  - lib/sip2/non_blocking_socket.rb
101
103
  - lib/sip2/patron_information.rb
102
104
  - lib/sip2/version.rb
@@ -113,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
115
  requirements:
114
116
  - - ">="
115
117
  - !ruby/object:Gem::Version
116
- version: 2.0.0
118
+ version: 2.1.0
117
119
  required_rubygems_version: !ruby/object:Gem::Requirement
118
120
  requirements:
119
121
  - - ">="