puree 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,147 @@
1
+ module Puree
2
+
3
+ # Server
4
+ #
5
+ class Server
6
+
7
+ attr_reader :response
8
+
9
+ # @param endpoint [String]
10
+ # @param optional username [String]
11
+ # @param optional password [String]
12
+ # @param optional basic_auth [Boolean]
13
+ def initialize(endpoint: nil,
14
+ username: nil,
15
+ password: nil,
16
+ basic_auth: nil)
17
+ @resource_type = :server
18
+ @api_map = Puree::Map.new.get
19
+ @endpoint = endpoint.nil? ? Puree.endpoint : endpoint
20
+ @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth
21
+ if @basic_auth === true
22
+ @username = username.nil? ? Puree.username : username
23
+ @password = password.nil? ? Puree.password : password
24
+ end
25
+ end
26
+
27
+ # Get
28
+ #
29
+ # @return [Hash]
30
+ def get
31
+ missing = missing_credentials
32
+ if !missing.empty?
33
+ missing.each do |m|
34
+ puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}"
35
+ end
36
+ exit
37
+ end
38
+
39
+ # strip any trailing slash
40
+ @endpoint = @endpoint.sub(/(\/)+$/, '')
41
+ @auth = Base64::strict_encode64(@username + ':' + @password)
42
+
43
+ @options = {
44
+ basic_auth: @basic_auth,
45
+ latest_api: true,
46
+ resource_type: @resource_type.to_sym,
47
+ rendering: :system,
48
+ }
49
+ headers = {
50
+ 'Accept' => 'application/xml',
51
+ 'Authorization' => 'Basic ' + @auth
52
+ }
53
+ query = {}
54
+ query['rendering'] = @options[:rendering]
55
+
56
+ begin
57
+ url = build_url
58
+ req = HTTP.headers accept: headers['Accept']
59
+ if @options[:basic_auth]
60
+ req = req.auth headers['Authorization']
61
+ end
62
+ @response = req.get(url, params: query)
63
+ @doc = Nokogiri::XML @response.body
64
+ @doc.remove_namespaces!
65
+ rescue HTTP::Error => e
66
+ puts 'HTTP::Error '+ e.message
67
+ end
68
+
69
+ get_data? ? metadata : {}
70
+ end
71
+
72
+ # All metadata
73
+ #
74
+ # @return [Hash]
75
+ def metadata
76
+ o = {}
77
+ o['version'] = version
78
+ o
79
+ end
80
+
81
+ # Version
82
+ #
83
+ # @return [String]
84
+ def version
85
+ path = service_response_name + '/baseVersion'
86
+ xpath_query(path).text.strip
87
+ end
88
+
89
+ private
90
+
91
+
92
+ # Is there any data after get?
93
+ #
94
+ # @return [Boolean]
95
+ def get_data?
96
+ # n.b. arbitrary element existence check
97
+ path = service_response_name + '/baseVersion'
98
+ xpath_result = @doc.xpath path
99
+ xpath_result.size ? true : false
100
+ end
101
+
102
+ def service_name
103
+ resource_type = @options[:resource_type]
104
+ @api_map[:resource_type][resource_type][:service]
105
+ end
106
+
107
+ def service_response_name
108
+ resource_type = @options[:resource_type]
109
+ @api_map[:resource_type][resource_type][:response]
110
+ end
111
+
112
+ def build_url
113
+ service = service_name
114
+ if @options[:latest_api] === false
115
+ service_api_mode = service
116
+ else
117
+ service_api_mode = service + '.current'
118
+ end
119
+ @endpoint + '/' + service_api_mode
120
+ end
121
+
122
+ def xpath_query(path)
123
+ xml = @response.body
124
+ doc = Nokogiri::XML xml
125
+ doc.remove_namespaces!
126
+ doc.xpath path
127
+ end
128
+
129
+ def missing_credentials
130
+ missing = []
131
+ if @endpoint.nil?
132
+ missing << 'endpoint'
133
+ end
134
+ if @username.nil?
135
+ missing << 'username'
136
+ end
137
+ if @password.nil?
138
+ missing << 'password'
139
+ end
140
+ missing
141
+ end
142
+
143
+ alias :find :get
144
+
145
+ end
146
+ end
147
+
@@ -1,3 +1,3 @@
1
1
  module Puree
2
- VERSION = "0.14.0"
2
+ VERSION = "0.15.0"
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Adrian Albin-Clark"]
10
10
  spec.email = ["a.albin-clark@lancaster.ac.uk"]
11
11
  spec.summary = %q{A client for the Pure Research Information System API.}
12
- spec.description = %q{Consumes the Pure Research Information System API and facilitates post-processing of metadata into simple data structures.}
12
+ spec.description = %q{Pur&#233;e consumes the Pure Research Information System API and puts the metadata into simple data structures.}
13
13
  spec.homepage = "https://github.com/lulibrary/puree.git"
14
14
  spec.license = "MIT"
15
15
 
@@ -20,6 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '~> 2.1'
22
22
 
23
- spec.add_runtime_dependency 'httparty', '~> 0.13'
23
+ spec.add_runtime_dependency 'http', '~> 2.0'
24
24
  spec.add_runtime_dependency 'nokogiri', '~> 1.6'
25
25
  end
@@ -3,7 +3,15 @@ require 'spec_helper'
3
3
  describe 'Collection' do
4
4
 
5
5
  it '#new' do
6
- p = Puree::Collection.new(api: :dataset)
6
+ endpoint = ENV['PURE_ENDPOINT']
7
+ username = ENV['PURE_USERNAME']
8
+ password = ENV['PURE_PASSWORD']
9
+ p = Puree::Collection.new(resource: :dataset,
10
+ endpoint: endpoint,
11
+ username: username,
12
+ password: password,
13
+ basic_auth: true
14
+ )
7
15
  expect(p).to be_an_instance_of Puree::Collection
8
16
  end
9
17
 
@@ -12,15 +20,16 @@ describe 'Collection' do
12
20
  endpoint = ENV['PURE_ENDPOINT']
13
21
  username = ENV['PURE_USERNAME']
14
22
  password = ENV['PURE_PASSWORD']
15
- @p = Puree::Collection.new(api: :dataset,
23
+ @p = Puree::Collection.new(resource: :dataset,
16
24
  endpoint: endpoint,
17
25
  username: username,
18
- password: password)
19
- @p.find limit: 5
26
+ password: password,
27
+ basic_auth: true)
28
+ @metadata = @p.find limit: 5
20
29
  end
21
30
 
22
- it '#UUID' do
23
- expect(@p.uuid).to be_an_instance_of(Array)
31
+ it '@metadata' do
32
+ expect(@metadata).to be_an_instance_of(Array)
24
33
  end
25
34
 
26
35
  end
@@ -15,84 +15,85 @@ describe 'Dataset' do
15
15
  uuid = ENV['PURE_DATASET_UUID']
16
16
  @p = Puree::Dataset.new(endpoint: endpoint,
17
17
  username: username,
18
- password: password)
19
- @p.find uuid: uuid
18
+ password: password,
19
+ basic_auth: true)
20
+ @metadata = @p.find uuid: uuid
21
+ end
22
+
23
+ it '@metadata' do
24
+ expect(@metadata).to be_an_instance_of(Hash)
20
25
  end
21
26
 
22
27
  it '#access' do
23
- expect(@p.access).to be_an_instance_of(String)
28
+ expect(@metadata['access']).to be_an_instance_of(String)
24
29
  end
25
30
 
26
31
  it '#associated' do
27
- expect(@p.associated).to be_an_instance_of(Array)
32
+ expect(@metadata['associated']).to be_an_instance_of(Array)
28
33
  end
29
34
 
30
35
  it '#available' do
31
- expect(@p.available).to be_an_instance_of(Hash)
36
+ expect(@metadata['available']).to be_an_instance_of(Hash)
32
37
  end
33
38
 
34
39
  it '#created' do
35
- expect(@p.created).to be_an_instance_of(String)
40
+ expect(@metadata['created']).to be_an_instance_of(String)
36
41
  end
37
42
 
38
43
  it '#description' do
39
- expect(@p.description).to be_an_instance_of(String)
44
+ expect(@metadata['description']).to be_an_instance_of(String)
40
45
  end
41
46
 
42
47
  it '#doi' do
43
- expect(@p.doi).to be_an_instance_of(String)
48
+ expect(@metadata['doi']).to be_an_instance_of(String)
44
49
  end
45
50
 
46
51
  it '#file' do
47
- expect(@p.file).to be_an_instance_of(Array)
52
+ expect(@metadata['file']).to be_an_instance_of(Array)
48
53
  end
49
54
 
50
55
  it '#geographical' do
51
- expect(@p.geographical).to be_an_instance_of(Array)
56
+ expect(@metadata['geographical']).to be_an_instance_of(Array)
52
57
  end
53
58
 
54
59
  it '#keyword' do
55
- expect(@p.keyword).to be_an_instance_of(Array)
60
+ expect(@metadata['keyword']).to be_an_instance_of(Array)
56
61
  end
57
62
 
58
63
  it '#link' do
59
- expect(@p.link).to be_an_instance_of(Array)
60
- end
61
-
62
- it '#metadata' do
63
- expect(@p.metadata).to be_an_instance_of(Hash)
64
+ expect(@metadata['link']).to be_an_instance_of(Array)
64
65
  end
65
66
 
66
67
  it '#modified' do
67
- expect(@p.modified).to be_an_instance_of(String)
68
+ expect(@metadata['modified']).to be_an_instance_of(String)
68
69
  end
69
70
 
70
71
  it '#person' do
71
- expect(@p.person).to be_an_instance_of(Hash)
72
+ expect(@metadata['person']).to be_an_instance_of(Hash)
72
73
  end
73
74
 
74
75
  it '#production' do
75
- expect(@p.production).to be_an_instance_of(Hash)
76
+ expect(@metadata['production']).to be_an_instance_of(Hash)
76
77
  end
77
78
 
78
79
  it '#project' do
79
- expect(@p.project).to be_an_instance_of(Array)
80
+ expect(@metadata['project']).to be_an_instance_of(Array)
80
81
  end
81
82
 
82
83
  it '#publication' do
83
- expect(@p.publication).to be_an_instance_of(Array)
84
+ expect(@metadata['publication']).to be_an_instance_of(Array)
84
85
  end
85
86
 
86
87
  it '#publisher' do
87
- expect(@p.publisher).to be_an_instance_of(String)
88
+ expect(@metadata['publisher']).to be_an_instance_of(String)
88
89
  end
89
90
 
90
91
  it '#temporal' do
91
- expect(@p.temporal).to be_an_instance_of(Hash)
92
+ expect(@metadata['temporal']).to be_an_instance_of(Hash)
92
93
  end
93
94
 
94
95
  it '#title' do
95
- expect(@p.title).to be_an_instance_of(String)
96
+ expect(@metadata['title']).to be_an_instance_of(String)
96
97
  end
97
98
 
98
99
 
@@ -1,4 +1,4 @@
1
- require 'httparty'
1
+ require 'http'
2
2
  require 'nokogiri'
3
3
  require 'puree/configuration'
4
4
  require 'puree/date'
@@ -12,4 +12,16 @@ require 'puree/person'
12
12
  require 'puree/project'
13
13
  require 'puree/publication'
14
14
  require 'puree/publisher'
15
- require 'puree/collection'
15
+ require 'puree/collection'
16
+ require 'puree/download'
17
+ require 'puree/server'
18
+
19
+ module Puree
20
+
21
+ class << self
22
+
23
+ include Puree::Configuration
24
+
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Albin-Clark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-10 00:00:00.000000000 Z
11
+ date: 2016-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: httparty
14
+ name: http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.13'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.13'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: nokogiri
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,8 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
- description: Consumes the Pure Research Information System API and facilitates post-processing
42
- of metadata into simple data structures.
41
+ description: Pur&#233;e consumes the Pure Research Information System API and puts
42
+ the metadata into simple data structures.
43
43
  email:
44
44
  - a.albin-clark@lancaster.ac.uk
45
45
  executables: []
@@ -67,6 +67,7 @@ files:
67
67
  - lib/puree/publication.rb
68
68
  - lib/puree/publisher.rb
69
69
  - lib/puree/resource.rb
70
+ - lib/puree/server.rb
70
71
  - lib/puree/version.rb
71
72
  - puree.gemspec
72
73
  - spec/collection.rb