puree 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,26 +1,184 @@
1
1
  module Puree
2
2
 
3
- # Download resource
3
+ # Download
4
4
  #
5
- class Download < Resource
5
+ class Download
6
+ attr_reader :response
6
7
 
7
8
  # @param endpoint [String]
8
9
  # @param optional username [String]
9
10
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
11
- super(api: :download,
12
- endpoint: endpoint,
13
- username: username,
14
- password: password)
11
+ # @param optional basic_auth [Boolean]
12
+ def initialize(endpoint: nil,
13
+ username: nil,
14
+ password: nil,
15
+ basic_auth: nil)
16
+ @resource_type = :download
17
+ @api_map = Puree::Map.new.get
18
+ @endpoint = endpoint.nil? ? Puree.endpoint : endpoint
19
+ @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth
20
+ if @basic_auth === true
21
+ @username = username.nil? ? Puree.username : username
22
+ @password = password.nil? ? Puree.password : password
23
+ end
15
24
  end
16
25
 
26
+ # Get
27
+ #
28
+ # @param optional limit [Integer]
29
+ # @param optional offset [Integer]
30
+ # @param optional resource [Symbol]
31
+ # @return [Array<Hash>]
32
+ def get(limit: 20,
33
+ offset: 0,
34
+ resource: nil)
35
+ missing = missing_credentials
36
+ if !missing.empty?
37
+ missing.each do |m|
38
+ puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}"
39
+ end
40
+ exit
41
+ end
42
+
43
+ # strip any trailing slash
44
+ @endpoint = @endpoint.sub(/(\/)+$/, '')
45
+ @auth = Base64::strict_encode64(@username + ':' + @password)
46
+
47
+ @options = {
48
+ basic_auth: @basic_auth,
49
+ latest_api: true,
50
+ resource_type: @resource_type.to_sym,
51
+ rendering: :system,
52
+ limit: limit,
53
+ offset: offset,
54
+ resource: resource.to_sym
55
+ }
56
+ headers = {
57
+ 'Accept' => 'application/xml',
58
+ 'Authorization' => 'Basic ' + @auth
59
+ }
60
+ query = {}
61
+ query['rendering'] = @options[:rendering]
62
+
63
+ if @options[:limit]
64
+ query['window.size'] = @options[:limit]
65
+ end
66
+
67
+ if @options[:offset]
68
+ query['window.offset'] = @options[:offset]
69
+ end
70
+
71
+ if @options[:resource]
72
+ query['contentType'] = service_family
73
+ end
74
+
75
+ begin
76
+ url = build_url
77
+ req = HTTP.headers accept: headers['Accept']
78
+ if @options[:basic_auth]
79
+ req = req.auth headers['Authorization']
80
+ end
81
+ @response = req.get(url, params: query)
82
+ @doc = Nokogiri::XML @response.body
83
+ @doc.remove_namespaces!
84
+ rescue HTTP::Error => e
85
+ puts 'HTTP::Error '+ e.message
86
+ end
87
+
88
+ get_data? ? metadata : []
89
+ end
90
+
91
+
92
+
93
+
17
94
  # All metadata
18
95
  #
19
- # @return [Hash]
20
- # def metadata
21
- # super
22
- # end
96
+ # @return [Array<Hash>]
97
+ def metadata
98
+ statistic
99
+ end
23
100
 
24
- end
25
101
 
102
+ private
103
+
104
+ # Is there any data after get?
105
+ #
106
+ # @return [Boolean]
107
+ def get_data?
108
+ # n.b. arbitrary element existence check
109
+ path = service_response_name + '/downloadCount'
110
+ xpath_result = @doc.xpath path
111
+ xpath_result.size ? true : false
112
+ end
113
+
114
+ # Statistic
115
+ #
116
+ # @return [Array<Hash>]
117
+ def statistic
118
+ path = service_response_name + '/downloadCount'
119
+ xpath_result = xpath_query path
120
+ data_arr = []
121
+ xpath_result.each { |i|
122
+ data = {}
123
+ data['uuid'] = i.attr('uuid').strip
124
+ data['download'] = i.attr('downloads').strip
125
+ data_arr << data
126
+ }
127
+ data_arr.uniq
128
+ end
129
+
130
+ def service_family
131
+ resource_type = @options[:resource]
132
+ if @api_map[:resource_type].has_key? resource_type
133
+ @api_map[:resource_type][resource_type][:family]
134
+ else
135
+ puts "#{resource_type} is an unrecognised resource type"
136
+ exit
137
+ end
138
+ end
139
+
140
+ def service_name
141
+ resource_type = @options[:resource_type]
142
+ @api_map[:resource_type][resource_type][:service]
143
+ end
144
+
145
+ def service_response_name
146
+ resource_type = @options[:resource_type]
147
+ @api_map[:resource_type][resource_type][:response]
148
+ end
149
+
150
+ def build_url
151
+ service = service_name
152
+ if @options[:latest_api] === false
153
+ service_api_mode = service
154
+ else
155
+ service_api_mode = service + '.current'
156
+ end
157
+ @endpoint + '/' + service_api_mode
158
+ end
159
+
160
+ def xpath_query(path)
161
+ xml = @response.body
162
+ doc = Nokogiri::XML xml
163
+ doc.remove_namespaces!
164
+ doc.xpath path
165
+ end
166
+
167
+ def missing_credentials
168
+ missing = []
169
+ if @endpoint.nil?
170
+ missing << 'endpoint'
171
+ end
172
+ if @username.nil?
173
+ missing << 'username'
174
+ end
175
+ if @password.nil?
176
+ missing << 'password'
177
+ end
178
+ missing
179
+ end
180
+
181
+ alias :find :get
182
+
183
+ end
26
184
  end
@@ -7,18 +7,23 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :event,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
15
17
  end
16
18
 
19
+
20
+
21
+
17
22
  # City
18
23
  #
19
24
  # @return [String]
20
25
  def city
21
- path = '//city'
26
+ path = '/city'
22
27
  xpath_query(path).text.strip
23
28
  end
24
29
 
@@ -26,7 +31,7 @@ module Puree
26
31
  #
27
32
  # @return [String]
28
33
  def country
29
- path = '//country/term/localizedString'
34
+ path = '/country/term/localizedString'
30
35
  xpath_query(path).text.strip
31
36
  end
32
37
 
@@ -35,7 +40,7 @@ module Puree
35
40
  # @return [Hash]
36
41
  def date
37
42
  data = {}
38
- path = '//dateRange'
43
+ path = '/dateRange'
39
44
  range = xpath_query path
40
45
  data['start'] = range.xpath('startDate').text.strip
41
46
  data['end'] = range.xpath('startDate').text.strip
@@ -46,7 +51,7 @@ module Puree
46
51
  #
47
52
  # @return [String]
48
53
  def description
49
- path = '//content/description'
54
+ path = '/description'
50
55
  xpath_query(path).text.strip
51
56
  end
52
57
 
@@ -54,7 +59,7 @@ module Puree
54
59
  #
55
60
  # @return [String]
56
61
  def location
57
- path = '//location'
62
+ path = '/location'
58
63
  xpath_query(path).text.strip
59
64
  end
60
65
 
@@ -62,16 +67,16 @@ module Puree
62
67
  #
63
68
  # @return [String]
64
69
  def title
65
- path = '//title/localizedString'
66
- xpath_query(path).text.strip
70
+ path = '/title/localizedString'
71
+ xpath_query_for_single_value path
67
72
  end
68
73
 
69
74
  # Type
70
75
  #
71
76
  # @return [String]
72
77
  def type
73
- path = '//content/typeClassification/term/localizedString'
74
- xpath_query(path).text.strip
78
+ path = '//typeClassification/term/localizedString'
79
+ xpath_query_for_single_value path
75
80
  end
76
81
 
77
82
  # All metadata
@@ -7,13 +7,16 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :journal,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
15
17
  end
16
18
 
19
+
17
20
  # All metadata
18
21
  #
19
22
  # @return [Hash]
@@ -21,13 +21,19 @@ module Puree
21
21
  family: 'dk.atira.pure.modules.datasets.external.model.dataset.DataSet'
22
22
  },
23
23
  download: {
24
- service: 'downloadcount',
25
- response: 'GetDownloadCountResponse'
24
+ service: 'downloadcount',
25
+ response: 'GetDownloadCountResponse'
26
26
  },
27
+ server: {
28
+ service: 'servermeta',
29
+ response: 'GetServerMetaResponse'
30
+ },
31
+
27
32
  }
28
33
  }
29
34
 
30
35
  add_convention
36
+ # add_family
31
37
  end
32
38
 
33
39
  # Pure API map
@@ -38,6 +44,7 @@ module Puree
38
44
  end
39
45
 
40
46
 
47
+
41
48
  private
42
49
 
43
50
  def add_convention
@@ -49,6 +56,10 @@ module Puree
49
56
  end
50
57
  end
51
58
 
59
+ # def add_family
60
+ # @api_map[:resource_type][:event][:family] = 'dk.atira.pure.api.shared.model.event.Event'
61
+ # end
62
+
52
63
 
53
64
  end
54
65
 
@@ -7,19 +7,23 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :organisation,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
15
17
  end
16
18
 
19
+
20
+
17
21
  # Address
18
22
  #
19
23
  # @return [Array<Hash>]
20
24
  def address
21
- path = '//addresses/classifiedAddress'
22
- xpath_result = xpath_query path
25
+ path = '/addresses/classifiedAddress'
26
+ xpath_result = xpath_query path
23
27
 
24
28
  data = []
25
29
 
@@ -39,7 +43,7 @@ module Puree
39
43
  #
40
44
  # @return [Array<String>]
41
45
  def email
42
- path = '//emails/classificationDefinedStringFieldExtension/value'
46
+ path = '/emails/classificationDefinedStringFieldExtension/value'
43
47
  xpath_result = xpath_query path
44
48
  arr = []
45
49
  xpath_result.each { |i| arr << i.text.strip }
@@ -50,8 +54,8 @@ module Puree
50
54
  #
51
55
  # @return [String]
52
56
  def name
53
- data = node 'name'
54
- !data.nil? && !data.empty? ? data['localizedString']['__content__'].strip : ''
57
+ path = '/name/localizedString'
58
+ xpath_query_for_single_value path
55
59
  end
56
60
 
57
61
  # Parent
@@ -70,7 +74,7 @@ module Puree
70
74
  #
71
75
  # @return [Array<String>]
72
76
  def phone
73
- path = '//phoneNumbers/classificationDefinedStringFieldExtension/value'
77
+ path = '/phoneNumbers/classificationDefinedStringFieldExtension/value'
74
78
  xpath_result = xpath_query path
75
79
  arr = []
76
80
  xpath_result.each { |i| arr << i.text.strip }
@@ -81,16 +85,15 @@ module Puree
81
85
  #
82
86
  # @return [String]
83
87
  def type
84
- path = '//content/typeClassification/term/localizedString'
85
- xpath_result = xpath_query path
86
- xpath_result ? xpath_result.text.strip : ''
88
+ path = '/typeClassification/term/localizedString'
89
+ xpath_query_for_single_value path
87
90
  end
88
91
 
89
92
  # URL
90
93
  #
91
94
  # @return [Array<String>]
92
95
  def url
93
- path = '//content/webAddresses/classificationDefinedFieldExtension/value/localizedString'
96
+ path = '/webAddresses/classificationDefinedFieldExtension/value/localizedString'
94
97
  xpath_result = xpath_query path
95
98
  arr = []
96
99
  xpath_result.each { |i| arr << i.text.strip }
@@ -113,14 +116,12 @@ module Puree
113
116
  end
114
117
 
115
118
 
116
- private
117
-
118
119
 
119
120
  # Organisation
120
121
  #
121
122
  # @return [Array<Hash>]
122
123
  def organisation
123
- path = '//organisations/organisation'
124
+ path = '/organisations/organisation'
124
125
  xpath_result = xpath_query path
125
126
 
126
127
  data = []
@@ -7,29 +7,49 @@ module Puree
7
7
  # @param endpoint [String]
8
8
  # @param optional username [String]
9
9
  # @param optional password [String]
10
- def initialize(endpoint: nil, username: nil, password: nil)
10
+ # @param optional basic_auth [Boolean]
11
+ def initialize(endpoint: nil, username: nil, password: nil, basic_auth: nil)
11
12
  super(api: :person,
12
13
  endpoint: endpoint,
13
14
  username: username,
14
- password: password)
15
+ password: password,
16
+ basic_auth: basic_auth)
15
17
  end
16
18
 
19
+
20
+
17
21
  # Affiliation
18
22
  #
19
- # @return [Array<String>]
23
+ # @return [Array<Hash>]
20
24
  def affiliation
21
- path = '//organisation/name/localizedString'
25
+ path = '//organisation'
22
26
  xpath_result = xpath_query path
23
- affiliations = []
24
- xpath_result.each { |i| affiliations << i.text }
25
- affiliations.uniq
27
+ data_arr = []
28
+ xpath_result.each { |i|
29
+ data = {}
30
+ data['uuid'] = i.attr('uuid').strip
31
+ data['name'] = i.xpath('name/localizedString').text.strip
32
+ data_arr << data
33
+ }
34
+ data_arr.uniq
26
35
  end
27
36
 
28
- # Image
37
+ # Email
38
+ #
39
+ # @return [Array]
40
+ def email
41
+ path = '//emails/classificationDefinedStringFieldExtension/value'
42
+ xpath_result = xpath_query path
43
+ data = []
44
+ xpath_result.each { |i| data << i.text }
45
+ data.uniq
46
+ end
47
+
48
+ # Image URL
29
49
  #
30
50
  # @return [Array<String>]
31
51
  def image
32
- path = '//photos/file/url'
52
+ path = '/photos/file/url'
33
53
  xpath_result = xpath_query path
34
54
  data = []
35
55
  xpath_result.each { |i| data << i.text }
@@ -51,12 +71,13 @@ module Puree
51
71
  #
52
72
  # @return [Hash]
53
73
  def name
54
- data = node 'name'
74
+ path = '/name'
75
+ xpath_result = xpath_query path
76
+ first = xpath_result.xpath('firstName').text.strip
77
+ last = xpath_result.xpath('lastName').text.strip
55
78
  o = {}
56
- if !data.nil? && !data.empty?
57
- o['first'] = data['firstName'].strip
58
- o['last'] = data['lastName'].strip
59
- end
79
+ o['first'] = first
80
+ o['last'] = last
60
81
  o
61
82
  end
62
83
 
@@ -64,8 +85,8 @@ module Puree
64
85
  #
65
86
  # @return [String]
66
87
  def orcid
67
- data = node 'orcid'
68
- !data.nil? && !data.empty? ? data.strip : ''
88
+ path = '/orcid'
89
+ xpath_query_for_single_value path
69
90
  end
70
91
 
71
92
  # All metadata
@@ -74,6 +95,7 @@ module Puree
74
95
  def metadata
75
96
  o = super
76
97
  o['affiliation'] = affiliation
98
+ o['email'] = email
77
99
  o['image'] = image
78
100
  o['keyword'] = keyword
79
101
  o['name'] = name