pure_iterator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40c5cd37848eac435da6f66979b843eeaba59551fe1a83a97431b6c28846770e
4
+ data.tar.gz: 947ba58ff5eeda3fd87ad0b33b08bded49bcaacb21ac1ad9c027bd76a48b5c1d
5
+ SHA512:
6
+ metadata.gz: 1d22bfb7e46602d1ed7de1d078476171eca7a3ac087048afb099c643eeb8cd8a05028d8440c479c2cb3ad6be7060c63b71c1455d16da735a82fdfd42d2b0013b
7
+ data.tar.gz: 8e4713b6e68e97ad343e5aed8027c60be383bea467ad752e64023458cb6714b21e2866ce1b7418a989933c6c4881fbc1272b052e6280965e858eedae1bd50854
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## 0.1.0 - 2019-10-25
6
+ ### Added
7
+ - Initial product.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pure_iterator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Adrian Albin-Clark
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Pure Iterator
2
+
3
+ A flexible way to process records in the Pure Research Information System.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pure_iterator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pure_iterator
20
+
21
+ ## Usage
22
+ ```ruby
23
+ class Foo < PureIterator::Base
24
+ def act(response)
25
+ # do something
26
+ end
27
+
28
+ def post_endpoint
29
+ 'persons'
30
+ end
31
+ end
32
+ ```
33
+
34
+ ```ruby
35
+ def config
36
+ {
37
+ host: 'pure.example.com',
38
+ username: 'YOUR_PURE_USERNAME',
39
+ password: 'YOUR_PURE_PASSWORD',
40
+ api_key: 'YOUR_PURE_API_KEY',
41
+ api_version: 514
42
+ }
43
+ end
44
+
45
+ iterator = Foo.new config
46
+ iterator.accept :json # default is :xml
47
+ params = {size: 20} # for pagination, use size and offset
48
+ iterator.iterate params
49
+ ```
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,86 @@
1
+ require 'http'
2
+ require 'nokogiri'
3
+
4
+ module PureIterator
5
+ class Base
6
+ # @param config [Hash]
7
+ # @option config [String] :host Pure host
8
+ # @option config [String] :username Username of the Pure host account
9
+ # @option config [String] :password Password of the Pure host account
10
+ # @option config [String] :api_key API key of the Pure host account
11
+ # @option config [Integer] :api_version Pure API version
12
+ def initialize(config)
13
+ http_client = HTTP::Client.new
14
+ http_client = http_client.headers({ 'api-key' => config[:api_key] })
15
+ http_client = http_client.basic_auth({user: config[:username], pass: config[:password]})
16
+ @http_client = http_client
17
+ @host = config[:host]
18
+ @api_version = config[:api_version]
19
+ accept :xml
20
+ end
21
+
22
+ # Set the response Accept type
23
+ # @param accept [Symbol]
24
+ def accept(accept)
25
+ supported_accept = [:xml, :json]
26
+ raise "Supported Accept values #{supported_accept}" unless supported_accept.include? accept
27
+ @accept = accept
28
+ end
29
+
30
+ # Traverse a collection, doing something with each response (which may contain multiple records if size parameter used)
31
+ # @param params [Hash] Pure POST parameters (except page and pageSize)
32
+ # @return [String] 'done' when collection has been traversed
33
+ def iterate(params = {})
34
+ query_record_count = count(params)
35
+ @http_client = @http_client.headers({ 'Accept' => "application/#{@accept}" })
36
+ default_query_params = {size: 1, offset: 0}
37
+ query_options = params.dup
38
+ query_options.delete :page
39
+ query_options.delete :pageSize
40
+ query_params = default_query_params.merge(query_options)
41
+ while query_params[:offset] < query_record_count
42
+ response = @http_client.post url, json: query_params
43
+ act response
44
+ query_params[:offset] += query_params[:size]
45
+ end
46
+ 'done'
47
+ end
48
+
49
+ def count(params = {})
50
+ @http_client = @http_client.headers({ 'Accept' => "application/xml" })
51
+ default_count_params = {size: 0}
52
+ count_options = params.dup
53
+ count_options.delete :size
54
+ count_options.delete :page
55
+ count_options.delete :pageSize
56
+ response = @http_client.post url, json: default_count_params.merge(count_options)
57
+ if response.code === 200
58
+ count_from_xml response
59
+ else
60
+ raise response
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def url
67
+ File.join 'https://', @host, 'ws', 'api', @api_version.to_s, post_endpoint
68
+ end
69
+
70
+ def count_from_xml(xml)
71
+ doc = Nokogiri::XML xml
72
+ doc.remove_namespaces!
73
+ doc.xpath('/result/count').text.to_i
74
+ end
75
+
76
+ # @return [String] Pure POST endpoint
77
+ def post_endpoint
78
+ raise "#{self.class.name}##{__method__} not implemented"
79
+ end
80
+
81
+ # @param response [HTTP::Response]
82
+ def act(response)
83
+ raise "#{self.class.name}##{__method__} not implemented"
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module PureIterator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "pure_iterator/version"
2
+ require_relative 'pure_iterator/base'
3
+
4
+ module PureIterator
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pure_iterator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pure_iterator"
8
+ spec.version = PureIterator::VERSION
9
+ spec.authors = ["Adrian Albin-Clark"]
10
+ spec.email = ["a.albin-clark@lancaster.ac.uk"]
11
+ spec.summary = %q{A flexible way to process records in the Pure Research Information System.}
12
+ spec.metadata = {
13
+ 'source_code_uri' => "https://github.com/lulibrary/#{spec.name}",
14
+ "documentation_uri" => "https://www.rubydoc.info/gems/#{spec.name}/#{spec.version}",
15
+ }
16
+ spec.license = "MIT"
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'minitest-reporters', '~> 1.1'
23
+
24
+ spec.add_dependency 'http', '~> 3.0'
25
+ spec.add_dependency 'nokogiri', '~> 1.6'
26
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pure_iterator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Albin-Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest-reporters
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ description:
56
+ email:
57
+ - a.albin-clark@lancaster.ac.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - lib/pure_iterator.rb
68
+ - lib/pure_iterator/base.rb
69
+ - lib/pure_iterator/version.rb
70
+ - pure_iterator.gemspec
71
+ homepage:
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ source_code_uri: https://github.com/lulibrary/pure_iterator
76
+ documentation_uri: https://www.rubydoc.info/gems/pure_iterator/0.1.0
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A flexible way to process records in the Pure Research Information System.
96
+ test_files: []