figshare_api_v2 0.9.5 → 0.9.6

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: 36c39b92b903060de17387c48901a4fe606938a50361549c3981013c962ebae6
4
- data.tar.gz: d67537244545182b7d7075b8dbe1ce19480aad7ad13af441289f5a7b7959ffd1
3
+ metadata.gz: 1a7584ac8d8eb3bd0c1750eccfd3568eef74781b1bb3c5d3c3cce12fe17affab
4
+ data.tar.gz: 3d10aed08ec5bbb2b4f9534510e478d70c4ec3f20b2674e073c33249d9e6b56e
5
5
  SHA512:
6
- metadata.gz: ca3d9efee24f0e8a4d07573e46d35c1518b4461506093a75d73f0179fd90185e1a311704420ee74dcea5f70b4a900bbc434eac187978a323c310799683f13b6b
7
- data.tar.gz: 20e43ce7f22338c3dcf96c8fe371f4c5c66a020b21bd0bd5ac23e04718039c619aeceb81ecfb73ff02261cae5220ef21a94871a1f174d329ef3cd29b1d66279c
6
+ metadata.gz: 141ffdc6d2487e5febd08fd547f2836efadd0b3d55907ba9e025f61846efed48c6e8382c52c0729b85093c5916a5c9569f5fa1adcc08d8650ef132f450ddc46f
7
+ data.tar.gz: cf73c9713e16b66e782ef79246a8f7422166d8ae8f06318d34f9fff2bd8bd517af9b1116050604c4ce4e58841b62590f87f4706384578f0f0b808726bc7d3373
data/History.txt CHANGED
@@ -1,3 +1,39 @@
1
+ robertburrowes Mon Sep 20 12:31:26 2021 +1200
2
+ Test for listing all institutional accounts, and getting individual account quota.
3
+ robertburrowes Mon Sep 20 12:30:34 2021 +1200
4
+ Ran through Rubocop to clean up formatting, picking up a few error in quote usage; extra args; and other misc errors.
5
+ robertburrowes Thu Sep 2 16:26:24 2021 +1200
6
+ Reserve DOIs
7
+ robertburrowes Thu Sep 2 16:22:36 2021 +1200
8
+ Reworked, to check if articles already exist and to reserve DOIs.
9
+ robertburrowes Thu Sep 2 16:20:53 2021 +1200
10
+ Look for duplicate files in the source directories.
11
+ robertburrowes Thu Sep 2 16:17:02 2021 +1200
12
+ Fetch article IDs, if the conf file has them set to 0
13
+ robertburrowes Sat Apr 24 09:17:14 2021 +1200
14
+ check local dir against figshare
15
+ robertburrowes Thu Apr 22 12:31:17 2021 +1200
16
+ exclude sub test dir
17
+ robertburrowes Thu Apr 22 12:31:06 2021 +1200
18
+ missing ,
19
+ robertburrowes Thu Apr 22 12:30:49 2021 +1200
20
+ delete tmp test scripts
21
+ robertburrowes Fri Mar 19 13:54:58 2021 +1300
22
+ tests of creation of articles, and upload, using config file
23
+ robertburrowes Fri Mar 19 13:54:18 2021 +1300
24
+ Iterate across just last upload
25
+ robertburrowes Fri Mar 19 13:51:08 2021 +1300
26
+ Look for duplicate files within upload set, as defined by upload config file
27
+ robertburrowes Fri Mar 19 13:26:46 2021 +1300
28
+ hash to json naming changes
29
+ robertburrowes Fri Mar 19 13:24:00 2021 +1300
30
+ changes from hash to json naming
31
+ robertburrowes Fri Mar 19 13:17:26 2021 +1300
32
+ run from json config file, to test this works
33
+ robertburrowes Fri Mar 19 13:15:52 2021 +1300
34
+ Creating generic upload, that uses json config
35
+ robertburrowes Thu Mar 18 11:59:15 2021 +1300
36
+ Testing with Peter and Danes articles
1
37
  robertburrowes Tue Mar 16 14:08:00 2021 +1300
2
38
  private articles create was broken: Added private article body method
3
39
  robertburrowes Tue Dec 1 14:43:50 2020 +1300
data/lib/authors.rb CHANGED
@@ -12,23 +12,27 @@ module Figshare
12
12
  # @param order [String] "published_date" Default, "modified_date", "views", "cites", "shares"
13
13
  # @param order_direction [String] "desc" Default, "asc"
14
14
  # @yield [Hash] {id, first_name, last_name, full_name, url_name, is_active, is_public, orcid_id, institution_id, group_id, job_title}
15
- def search(institute: false, group_id: nil, orcid: nil,
16
- is_active: true, is_public: true,
17
- order: 'published_date', order_direction: 'desc',
18
- search_for:,
15
+ def search( search_for:,
16
+ institute: false,
17
+ group_id: nil,
18
+ orcid: nil,
19
+ is_active: true,
20
+ is_public: true,
21
+ order: 'published_date',
22
+ order_direction: 'desc',
19
23
  &block
20
24
  )
21
25
  args = { 'search_for' => search_for }
22
- args['institution'] = @institute_id if ! institute.nil?
23
- args['group_id'] = group_id if ! group_id.nil?
24
- args['is_active'] = is_active if ! is_active.nil?
25
- args['is_public'] = is_public if ! is_public.nil?
26
- args['orcid'] = orcid if ! orcid.nil?
27
- args['order'] = order if ! order.nil?
28
- args['order_direction'] = order_direction if ! order_direction.nil?
26
+ args['institution'] = @institute_id unless institute.nil?
27
+ args['group_id'] = group_id unless group_id.nil?
28
+ args['is_active'] = is_active unless is_active.nil?
29
+ args['is_public'] = is_public unless is_public.nil?
30
+ args['orcid'] = orcid unless orcid.nil?
31
+ args['order'] = order unless order.nil?
32
+ args['order_direction'] = order_direction unless order_direction.nil?
29
33
  post(api_query: 'account/authors/search', args: args, &block)
30
34
  end
31
-
35
+
32
36
  # Get an authors details
33
37
  #
34
38
  # @param author_id [Integer] Figshare Author ID
@@ -37,5 +41,4 @@ module Figshare
37
41
  get(api_query: "account/authors/#{author_id}", &block)
38
42
  end
39
43
  end
40
-
41
44
  end
data/lib/base.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Figshare
1
+ module Figshare
2
2
  # Supporting web calls to the API
3
3
  #
4
4
  class Base
@@ -8,7 +8,7 @@ module Figshare
8
8
  attr_accessor :hostname
9
9
  attr_accessor :api_url
10
10
  attr_accessor :institute_id
11
-
11
+
12
12
  # Init reads the Json configuration files, setting @course_codes_to_faculty and @academic_department_code_to_faculty
13
13
  # Opens a connection to the LDAP server, setting @ldap for other methods to use.
14
14
  #
@@ -17,14 +17,14 @@ module Figshare
17
17
  def initialize(figshare_user:, conf_dir:)
18
18
  figshare_token = load_json_file("#{conf_dir}/figshare_keys.json")
19
19
  @auth_token = figshare_token[figshare_user]
20
-
20
+
21
21
  figshare_site_params = load_json_file("#{conf_dir}/figshare_site_params.json")
22
-
22
+
23
23
  @hostname = figshare_site_params['host']
24
24
  @api_url = figshare_site_params['api_url']
25
25
  @institute_id = figshare_site_params['institute_id']
26
26
  end
27
-
27
+
28
28
  # Parse the config file
29
29
  #
30
30
  # @param filename [String] config file name to parse
@@ -32,7 +32,7 @@ module Figshare
32
32
  private def load_json_file(filename)
33
33
  JSON.parse(File.read(filename))
34
34
  end
35
-
35
+
36
36
  # get iterates through the API response, yielding each value to the passed block
37
37
  # When Figshare API usually has no paging option.
38
38
  # If there is no block, then the results are printed (useful for debugging)
@@ -67,16 +67,16 @@ module Figshare
67
67
  private def get_paginate(api_query:, args: {}, debug: false, by_offset: false, &block)
68
68
  args = {} if args.nil?
69
69
  if ! args.is_a?(Hash)
70
- raise "get_paginate(): Expecting args to be a Hash"
70
+ raise 'get_paginate(): Expecting args to be a Hash'
71
71
  end
72
-
72
+
73
73
  offset = 0
74
74
  page = 1
75
75
  limit = page_size = 100
76
76
  result_count = 0
77
77
  loop do
78
78
  content_type = response = nil
79
- form_args = by_offset ? {"limit"=>limit, "offset"=>offset} : {"page_size"=>page_size, "page"=>page}
79
+ form_args = by_offset ? { 'limit' => limit, 'offset' => offset } : { 'page_size' => page_size, 'page' => page }
80
80
  WIKK::WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
81
81
  response = ws.get_page( query: "#{@api_url}#{api_query}",
82
82
  authorization: "token #{@auth_token}",
@@ -88,10 +88,10 @@ module Figshare
88
88
  result_count += page_count
89
89
  break if page_count < page_size # Got less results than we asked for, so it was the last page
90
90
 
91
- page += 1 #Ready to fetch next page
92
- offset += limit #if we use offset, then mor
91
+ page += 1 # Ready to fetch next page
92
+ offset += limit # if we use offset, then mor
93
93
  end
94
-
94
+
95
95
  return result_count
96
96
  end
97
97
 
@@ -105,26 +105,26 @@ module Figshare
105
105
  # @param content_type [String] Assuming Json, but might need binary ('application/octet-stream')
106
106
  # @yield [String] if given a block, iterates through the result from figshare
107
107
  # @return [Integer] number of results.
108
- private def post(api_query:, args: {}, data: nil, debug: false, content_type: 'application/json; charset=UTF-8', &block)
108
+ private def post(api_query:, args: {}, data: nil, debug: false, content_type: 'application/json; charset=UTF-8', &block)
109
109
  body = nil
110
- if data.is_a?(Hash)
111
- # Convert hash to json, and merge in additional args
112
- body = data.merge(args).to_j
113
- elsif data.nil? && ! args.empty?
114
- # No data, but args, so just use the args
115
- body = args.to_j
116
- else
117
- # Data isn't a Hash, so just pass it through (might be nil)
118
- body = data
119
- end
120
-
121
- content_type = response = nil
110
+ body = if data.is_a?(Hash)
111
+ # Convert hash to json, and merge in additional args
112
+ data.merge(args).to_j
113
+ elsif data.nil? && ! args.empty?
114
+ # No data, but args, so just use the args
115
+ args.to_j
116
+ else
117
+ # Data isn't a Hash, so just pass it through (might be nil)
118
+ data
119
+ end
120
+
121
+ response = nil
122
122
  WIKK::WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
123
123
  response = ws.post_page( query: "#{@api_url}#{api_query}",
124
124
  content_type: content_type,
125
125
  authorization: "token #{@auth_token}",
126
126
  data: body
127
- )
127
+ )
128
128
  content_type = ws.header_value(key: 'Content-Type')
129
129
  end
130
130
  return iterate_json_response(response: response, content_type: content_type, debug: debug, &block)
@@ -140,34 +140,34 @@ module Figshare
140
140
  # @param by_offset [Boolean] use offset/limit rather than page/page_size in API calls
141
141
  # @yield [String] if given a block, iterates through the result from figshare
142
142
  # @return [Integer] number of results.
143
- private def post_paginate(api_query:, args: {}, debug: false, by_offset: false, &block)
143
+ private def post_paginate(api_query:, args: {}, debug: false, by_offset: false, &block)
144
144
  page = 1
145
145
  offset = 0
146
146
  limit = page_size = 100
147
147
  result_count = 0
148
-
148
+
149
149
  args = {} if args.nil?
150
150
  if ! args.is_a?(Hash)
151
- raise "post_paginate(): Expecting args to be a Hash"
151
+ raise 'post_paginate(): Expecting args to be a Hash'
152
152
  end
153
-
153
+
154
154
  loop do
155
155
  content_type = response = nil
156
- form_args = by_offset ? {"limit"=>limit, "offset"=>offset} : {"page_size"=>page_size, "page"=>page}
156
+ form_args = by_offset ? { 'limit' => limit, 'offset' => offset } : { 'page_size' => page_size, 'page' => page }
157
157
  WIKK::WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
158
158
  response = ws.post_page( query: "#{@api_url}#{api_query}",
159
159
  content_type: 'application/json; charset=UTF-8',
160
160
  authorization: "token #{@auth_token}",
161
161
  data: args.merge(form_args).to_j
162
- )
162
+ )
163
163
  content_type = ws.header_value(key: 'Content-Type')
164
164
  end
165
165
  page_count = iterate_json_response(response: response, content_type: content_type, debug: debug, &block)
166
166
  result_count += page_count
167
167
  break if page_count < page_size # Got less results than we asked for, so it was the last page
168
168
 
169
- page += 1 #Ready to fetch next page
170
- offset += limit #if we use offset
169
+ page += 1 # Ready to fetch next page
170
+ offset += limit # if we use offset
171
171
  end
172
172
  return result_count
173
173
  end
@@ -181,27 +181,27 @@ module Figshare
181
181
  # @param debug [Boolean] print result to stdout
182
182
  # @param content_type [String] Assuming Json, but might need binary ('application/octet-stream')
183
183
  # @yield [String] if given a block, iterates through the result from figshare
184
- # @return [Integer] number of results
184
+ # @return [Integer] number of results
185
185
  private def put(api_query:, args: {}, data: nil, debug: false, content_type: 'application/json; charset=UTF-8', &block)
186
186
  body = nil
187
- if data.is_a?(Hash)
188
- # Convert hash to json, and merge in additional args
189
- body = data.merge(args).to_j
190
- elsif data.nil? && ! args.empty?
191
- # No data, but args, so just use the args
192
- body = args.to_j
193
- else
194
- # Data isn't a Hash, so just pass it through (might be nil)
195
- body = data
196
- end
187
+ body = if data.is_a?(Hash)
188
+ # Convert hash to json, and merge in additional args
189
+ data.merge(args).to_j
190
+ elsif data.nil? && ! args.empty?
191
+ # No data, but args, so just use the args
192
+ args.to_j
193
+ else
194
+ # Data isn't a Hash, so just pass it through (might be nil)
195
+ data
196
+ end
197
197
 
198
- content_type = response = nil
198
+ response = nil
199
199
  WIKK::WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
200
- response = ws.put_page( query: "#{@api_url}#{api_query}",
201
- content_type: content_type,
202
- authorization: "token #{@auth_token}",
203
- data: body
204
- )
200
+ response = ws.put_req( query: "#{@api_url}#{api_query}",
201
+ content_type: content_type,
202
+ authorization: "token #{@auth_token}",
203
+ data: body
204
+ )
205
205
  content_type = ws.header_value(key: 'Content-Type')
206
206
  end
207
207
  return iterate_json_response(response: response, content_type: content_type, debug: debug, &block)
@@ -214,18 +214,18 @@ module Figshare
214
214
  # @param debug [Boolean] print result to stdout
215
215
  # @yield [Hash] Unlikely to have a result from delete calls, but if we do, we see it here
216
216
  # @return [Integer] number of results (usually 0)
217
- private def delete(api_query:, args: {}, debug: false, &block)
218
- #ignoring args for the moment. Not sure what to do with them, if we do get them.
219
- content_type = response = nil
217
+ private def delete(api_query:, args: {}, debug: false, content_type: 'application/json; charset=UTF-8', &block)
218
+ response = nil
220
219
  WIKK::WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
221
220
  response = ws.delete_req( query: "#{@api_url}#{api_query}",
222
- authorization: "token #{@auth_token}"
223
- )
221
+ authorization: "token #{@auth_token}",
222
+ form_values: args
223
+ )
224
224
  content_type = ws.header_value(key: 'Content-Type')
225
225
  end
226
226
  return iterate_json_response(response: response, content_type: content_type, debug: debug, &block)
227
227
  end
228
-
228
+
229
229
  # For iterate through the api response
230
230
  #
231
231
  # @param response [String] response from the API call
@@ -234,25 +234,25 @@ module Figshare
234
234
  # @yield [Hash] each array member in the response (or the entire response, if not iteratable)
235
235
  private def iterate_json_response(response:, content_type:, debug: false )
236
236
  return 0 if response.nil? # got no responses
237
-
237
+
238
238
  if content_type =~ /application\/json/
239
239
  response_array = JSON.parse(response)
240
- #If we don't have an Array, turn the response into an Array so we can iterate over this one response.
241
- response_array = [ response_array ] if ! (response_array.class == Array)
240
+ # If we don't have an Array, turn the response into an Array so we can iterate over this one response.
241
+ response_array = [ response_array ] unless response_array.instance_of?(Array)
242
242
  return 0 if response_array.empty? # got empty array of responses
243
-
243
+
244
244
  count = 0
245
245
  response_array.each do |r|
246
246
  yield r if block_given?
247
247
  p r if debug
248
248
  count += 1
249
249
  end
250
-
250
+
251
251
  return count
252
- else #just dump the entire response on the caller :)
253
- yield response if block_given?
252
+ else # just dump the entire response on the caller :)
253
+ yield response if block_given?
254
254
  return response.length
255
255
  end
256
256
  end
257
257
  end
258
- end
258
+ end
@@ -1,8 +1,10 @@
1
- module Figshare
2
- VERSION = '0.9.5'
1
+ module Figshare # :nodoc:
2
+ # Figshare module initialization
3
+ #
4
+ VERSION = '0.9.6'
3
5
 
4
- require "wikk_webbrowser"
5
- require "wikk_json"
6
+ require 'wikk_webbrowser'
7
+ require 'wikk_json'
6
8
  require 'time'
7
9
 
8
10
  require_relative 'base.rb'
@@ -18,8 +20,8 @@ module Figshare
18
20
  require_relative 'upload.rb'
19
21
  require_relative 'stats.rb'
20
22
  require_relative 'oai_pmh.rb'
21
-
22
- # Hack :) to do a lazy initialization of the Figshare subclasses. i.e. only if they get called.
23
+
24
+ # HACK: to do a lazy initialization of the Figshare subclasses. i.e. only if they get called.
23
25
  # Usage:
24
26
  # @figshare = Figshare::Init.new(figshare_user: 'figshare_admin', conf_dir: "#{__dir__}/conf")
25
27
  #
@@ -36,7 +38,7 @@ module Figshare
36
38
  @figshare_user = figshare_user
37
39
  @conf_dir = conf_dir
38
40
  end
39
-
41
+
40
42
  # Create Figshare::Authors, if it doesn't exist. Initialized with @figshare_user and @conf_dir
41
43
  #
42
44
  # @return [Figshare::Authors]
@@ -50,49 +52,49 @@ module Figshare
50
52
  def institutions
51
53
  @institutions ||= Institutions.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
52
54
  end
53
-
55
+
54
56
  # Create Figshare::Other, if it doesn't exist. Initialized with @figshare_user and @conf_dir
55
57
  #
56
58
  # @return [Figshare::Other]
57
59
  def other
58
60
  @other ||= Other.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
59
61
  end
60
-
62
+
61
63
  # Create Figshare::PrivateArticles, if it doesn't exist. Initialized with @figshare_user and @conf_dir
62
64
  #
63
65
  # @return [Figshare::PrivateArticles]
64
66
  def private_articles
65
67
  @private_articles ||= PrivateArticles.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
66
68
  end
67
-
69
+
68
70
  # Create Figshare::PrivateCollections, if it doesn't exist. Initialized with @figshare_user and @conf_dir
69
71
  #
70
72
  # @return [Figshare::PrivateCollections]
71
73
  def private_collections
72
74
  @private_collections ||= PrivateCollections.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
73
75
  end
74
-
76
+
75
77
  # Create Figshare::PrivateProjects, if it doesn't exist. Initialized with @figshare_user and @conf_dir
76
78
  #
77
79
  # @return [Figshare::PrivateProjects]
78
80
  def private_projects
79
81
  @private_projects ||= PrivateProjects.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
80
82
  end
81
-
83
+
82
84
  # Create Figshare::PublicArticles, if it doesn't exist. Initialized with @figshare_user and @conf_dir
83
85
  #
84
86
  # @return [Figshare::PublicArticles]
85
87
  def public_articles
86
88
  @public_articles ||= PublicArticles.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
87
89
  end
88
-
90
+
89
91
  # Create Figshare::PublicCollections, if it doesn't exist. Initialized with @figshare_user and @conf_dir
90
92
  #
91
93
  # @return [Figshare::PublicCollections]
92
94
  def public_collections
93
95
  @public_collections ||= PublicCollections.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
94
96
  end
95
-
97
+
96
98
  # Create Figshare::PublicProjects, if it doesn't exist. Initialized with @figshare_user and @conf_dir
97
99
  #
98
100
  # @return [Figshare::PublicProjects]
@@ -106,20 +108,19 @@ module Figshare
106
108
  def upload
107
109
  @upload ||= Upload.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
108
110
  end
109
-
111
+
110
112
  # Create Figshare::Stats, if it doesn't exist. Initialized with @figshare_user and @conf_dir
111
113
  #
112
114
  # @return [Figshare::Stats]
113
115
  def stats
114
116
  @stats ||= Stats.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
115
117
  end
116
-
118
+
117
119
  # Create Figshare::OAI_PMH, if it doesn't exist. Initialized with @figshare_user and @conf_dir
118
120
  #
119
121
  # @return [Figshare::OAI_PMH]
120
122
  def oai_pmh
121
123
  @oai_pmh ||= OAI_PMH.new(figshare_user: @figshare_user, conf_dir: @conf_dir)
122
124
  end
123
-
124
125
  end
125
- end # module
126
+ end