serrano 0.5.0 → 0.5.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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Serrano
2
4
  # Custom error class for rescuing from all Serrano errors
3
5
  class Error < StandardError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'multi_json'
3
5
 
@@ -13,13 +15,13 @@ module FaradayMiddleware
13
15
  when 404
14
16
  raise Serrano::NotFound, error_message_400(response)
15
17
  when 500
16
- raise Serrano::InternalServerError, error_message_500(response, "Something is technically wrong.")
18
+ raise Serrano::InternalServerError, error_message_500(response, 'Something is technically wrong.')
17
19
  when 502
18
- raise Serrano::BadGateway, error_message_500(response, "The server returned an invalid or incomplete response.")
20
+ raise Serrano::BadGateway, error_message_500(response, 'The server returned an invalid or incomplete response.')
19
21
  when 503
20
- raise Serrano::ServiceUnavailable, error_message_500(response, "Crossref is rate limiting your requests.")
22
+ raise Serrano::ServiceUnavailable, error_message_500(response, 'Crossref is rate limiting your requests.')
21
23
  when 504
22
- raise Serrano::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
24
+ raise Serrano::GatewayTimeout, error_message_500(response, '504 Gateway Time-out')
23
25
  end
24
26
  end
25
27
  end
@@ -32,16 +34,16 @@ module FaradayMiddleware
32
34
  private
33
35
 
34
36
  def error_message_400(response)
35
- "\n #{response[:method].to_s.upcase} #{response[:url].to_s}\n Status #{response[:status]}#{error_body(response[:body])}"
37
+ "\n #{response[:method].to_s.upcase} #{response[:url]}\n Status #{response[:status]}#{error_body(response[:body])}"
36
38
  end
37
39
 
38
40
  def error_body(body)
39
- if not body.nil? and not body.empty? and body.kind_of?(String)
40
- if is_json?(body)
41
+ if !body.nil? && !body.empty? && body.is_a?(String)
42
+ if json?(body)
41
43
  body = ::MultiJson.load(body)
42
44
  if body['message'].nil?
43
45
  body = nil
44
- elseif body['message'].length == 1
46
+ elseif body['message'].length == 1
45
47
  body = body['message']
46
48
  else
47
49
  body = body['message'].collect { |x| x['message'] }.join('; ')
@@ -56,16 +58,15 @@ module FaradayMiddleware
56
58
  end
57
59
  end
58
60
 
59
- def error_message_500(response, body=nil)
60
- "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
61
+ def error_message_500(response, body = nil)
62
+ "#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
61
63
  end
62
64
 
63
- def is_json?(string)
65
+ def json?(string)
64
66
  MultiJson.load(string)
65
- return true
67
+ true
66
68
  rescue MultiJson::ParseError
67
- return false
69
+ false
68
70
  end
69
-
70
71
  end
71
72
  end
@@ -1,18 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # helper functions
2
4
  module Helpers
3
-
4
- $others = ['license_url','license_version','license_delay','full_text_version','full_text_type',
5
- 'award_number','award_funder']
6
-
7
5
  def filter_handler(x = nil)
6
+ others = %w[license_url license_version license_delay full_text_version full_text_type
7
+ award_number award_funder]
8
8
  if x.nil?
9
9
  nil
10
10
  else
11
11
  x = stringify(x)
12
- nn = x.keys.collect{ |z| z.to_s }
13
- if nn.collect{ |w| $others.include? w }.any?
14
- nn = nn.collect{ |b|
15
- if $others.include? b
12
+ nn = x.keys.collect(&:to_s)
13
+ if nn.collect { |w| others.include? w }.any?
14
+ nn = nn.collect do |b|
15
+ if others.include? b
16
16
  case b
17
17
  when 'license_url'
18
18
  'license.url'
@@ -32,24 +32,23 @@ module Helpers
32
32
  else
33
33
  b
34
34
  end
35
- }
35
+ end
36
36
  end
37
37
 
38
- newnn = nn.collect{ |m| m.gsub("_", "-") }
38
+ newnn = nn.collect { |m| m.tr('_', '-') }
39
39
  x = rename_keys(x, newnn)
40
- x = x.collect{ |k,v| [k, v].join(":") }.join(',')
41
- return x
40
+ x = x.collect { |k, v| [k, v].join(':') }.join(',')
41
+ x
42
42
  end
43
43
  end
44
44
 
45
45
  def stringify(x)
46
- (x.keys.map{ |k,v| k.to_s }.zip x.values).to_h
46
+ (x.keys.map { |k, _v| k.to_s }.zip x.values).to_h
47
47
  end
48
48
 
49
49
  def rename_keys(x, y)
50
50
  (y.zip x.values).to_h
51
51
  end
52
-
53
52
  end
54
53
 
55
54
  module Serrano
@@ -61,4 +60,3 @@ module Serrano
61
60
  include Helpers
62
61
  end
63
62
  end
64
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # Serrano::Filters
3
5
  #
@@ -14,81 +16,81 @@
14
16
  module Serrano
15
17
  module Filters
16
18
  def self.names
17
- $filter_details.keys
19
+ filter_details.keys
18
20
  end
19
21
 
20
22
  def self.filters
21
- $filter_details
23
+ filter_details
22
24
  end
23
25
  end
24
26
  end
25
27
 
26
- $filter_details = {
27
- "has_funder" => { "possible_values" => nil, "description" => "metadata which includes one or more funder entry" },
28
- "funder" => { "possible_values" => "{funder_id}", "description" => "metadata which include the {funder_id} in FundRef data" },
29
- "location" => { "possible_values" => "{country_name}", "description" => "funder records where location = {country name}. Only works on /funders route" },
30
- "prefix" => { "possible_values" => "{owner_prefix}", "description" => "metadata belonging to a DOI owner prefix {owner_prefix} (e.g. '10.1016' )" },
31
- "member" => { "possible_values" => "{member_id}", "description" => "metadata belonging to a CrossRef member" },
32
- "from_index_date" => { "possible_values" => '{date}', "description" => "metadata indexed since (inclusive) {date}" },
33
- "until_index_date" => { "possible_values" => '{date}', "description" => "metadata indexed before (inclusive) {date}" },
34
- "from_deposit_date" => { "possible_values" => '{date}', "description" => "metadata last (re)deposited since (inclusive) {date}" },
35
- "until_deposit_date" => { "possible_values" => '{date}', "description" => "metadata last (re)deposited before (inclusive) {date}" },
36
- "from_update_date" => { "possible_values" => '{date}', "description" => "Metadata updated since (inclusive) {date} Currently the same as 'from_deposit_date'" },
37
- "until_update_date" => { "possible_values" => '{date}', "description" => "Metadata updated before (inclusive) {date} Currently the same as 'until_deposit_date'" },
38
- "from_created_date" => { "possible_values" => '{date}', "description" => "metadata first deposited since (inclusive) {date}" },
39
- "until_created_date" => { "possible_values" => '{date}', "description" => "metadata first deposited before (inclusive) {date}" },
40
- "from_pub_date" => { "possible_values" => '{date}', "description" => "metadata where published date is since (inclusive) {date}" },
41
- "until_pub_date" => { "possible_values" => '{date}', "description" => "metadata where published date is before (inclusive) {date}" },
42
- "from_online_pub_date" => { "possible_values" => '{date}', "description" => "metadata where online published date is since (inclusive) {date}" },
43
- "until_online_pub_date" => { "possible_values" => '{date}', "description" => "metadata where online published date is before (inclusive) {date}" },
44
- "from_print_pub_date" => { "possible_values" => '{date}', "description" => "metadata where print published date is since (inclusive) {date}" },
45
- "until_print_pub_date" => { "possible_values" => '{date}', "description" => "metadata where print published date is before (inclusive) {date}" },
46
- "from_posted_date" => { "possible_values" => '{date}', "description" => "metadata where posted date is since (inclusive) {date}" },
47
- "until_posted_date" => { "possible_values" => '{date}', "description" => "metadata where posted date is before (inclusive) {date}" },
48
- "from_accepted_date" => { "possible_values" => '{date}', "description" => "metadata where accepted date is since (inclusive) {date}" },
49
- "until_accepted_date" => { "possible_values" => '{date}', "description" => "metadata where accepted date is before (inclusive) {date}" },
50
- "has_license" => { "possible_values" => nil, "description" => "metadata that includes any '<license_ref>' elements" },
51
- "license_url" => { "possible_values" => '{url}', "description" => "metadata where '<license_ref>' value equals {url}" },
52
- "license_version" => { "possible_values" => '{string}', "description" => "metadata where the '<license_ref>''s 'applies_to' attribute is '{string}'"},
53
- "license_delay" => { "possible_values" => "{integer}", "description" => "metadata where difference between publication date and the '<license_ref>''s 'start_date' attribute is <= '{integer}' (in days"},
54
- "has_full_text" => { "possible_values" => nil, "description" => "metadata that includes any full text '<resource>' elements_" },
55
- "full_text_version" => { "possible_values" => '{string}' , "description" => "metadata where '<resource>' element's 'content_version' attribute is '{string}'" },
56
- "full_text_type" => { "possible_values" => '{mime_type}' , "description" => "metadata where '<resource>' element's 'content_type' attribute is '{mime_type}' (e.g. 'application/pdf')" },
57
- "full_text_application" => { "possible_values" => '{string}' , "description" => "metadata where <resource> link has one of the following intended applications: text-mining, similarity-checking or unspecified" },
58
- "has_references" => { "possible_values" => nil , "description" => "metadata for works that have a list of references" },
59
- "has_archive" => { "possible_values" => nil , "description" => "metadata which include name of archive partner" },
60
- "archive" => { "possible_values" => '{string}', "description" => "metadata which where value of archive partner is '{string}'" },
61
- "has_orcid" => { "possible_values" => nil, "description" => "metadata which includes one or more ORCIDs" },
62
- "has_authenticated_orcid" => { "possible_values" => nil , "description" => "metadata which includes one or more ORCIDs where the depositing publisher claims to have witness the ORCID owner authenticate with ORCID"},
63
- "orcid" => { "possible_values" => '{orcid}', "description" => "metadata where '<orcid>' element's value = '{orcid}'" },
64
- "issn" => { "possible_values" => '{issn}', "description" => "metadata where record has an ISSN = '{issn}' Format is 'xxxx_xxxx'." },
65
- "directory" => { "possible_values" => "{directory}", "description" => "metadata records whose article or serial are mentioned in the given '{directory}'. Currently the only supported value is 'doaj'" },
66
- "doi" => { "possible_values" => '{doi}', "description" => "metadata describing the DOI '{doi}'" },
67
- "updates" => { "possible_values" => '{doi}', "description" => "metadata for records that represent editorial updates to the DOI '{doi}'" },
68
- "is_update" => { "possible_values" => nil, "description" => "metadata for records that represent editorial updates" },
69
- "has_update_policy" => { "possible_values" => nil, "description" => "metadata for records that include a link to an editorial update policy" },
70
- "container_title" => { "possible_values" => nil, "description" => "metadata for records with a publication title exactly with an exact match" },
71
- "category_name" => { "possible_values" => nil, "description" => "metadata for records with an exact matching category label" },
72
- "type" => { "possible_values" => '{type}', "description" => "metadata records whose type = '{type}' Type must be an ID value from the list of types returned by the '/types' resource" },
73
- "type_name" => { "possible_values" => nil, "description" => "metadata for records with an exacty matching type label" },
74
- "award_number" => { "possible_values" => "{award_number}", "description" => "metadata for records with a matching award nunber_ Optionally combine with 'award_funder'" },
75
- "award_funder" => { "possible_values" => '{funder doi or id}', "description" => "metadata for records with an award with matching funder. Optionally combine with 'award_number'" },
76
- "has_assertion" => { "possible_values" => nil, "description" => "metadata for records with any assertions" },
77
- "assertion_group" => { "possible_values" => nil, "description" => "metadata for records with an assertion in a particular group" },
78
- "assertion" => { "possible_values" => nil, "description" => "metadata for records with a particular named assertion" },
79
- "has_affiliation" => { "possible_values" => nil, "description" => "metadata for records that have any affiliation information" },
80
- "alternative_id" => { "possible_values" => nil, "description" => "metadata for records with the given alternative ID, which may be a publisher_specific ID, or any other identifier a publisher may have provided" },
81
- "article_number" => { "possible_values" => nil, "description" => "metadata for records with a given article number" },
82
- "has_abstract" => { "possible_values" => nil, "description" => "metadata for records which include an abstract" },
83
- "has_clinical_trial_number" => { "possible_values" => nil, "description" => "metadata for records which include a clinical trial number" },
84
- "content_domain" => { "possible_values" => nil, "description" => "metadata where the publisher records a particular domain name as the location Crossmark content will appear" },
85
- "has_content_domain" => { "possible_values" => nil, "description" => "metadata where the publisher records a domain name location for Crossmark content" },
86
- "has_crossmark_restriction" => { "possible_values" => nil, "description" => "metadata where the publisher restricts Crossmark usage to content domains" },
87
- "has_relation" => { "possible_values" => nil, "description" => "metadata for records that either assert or are the object of a relation" },
88
- "relation_type" => { "possible_values" => nil, "description" => "One of the relation types from the Crossref relations schema (e.g. is-referenced-by, is-parent-of, is-preprint-of)" },
89
- "relation_object" => { "possible_values" => nil, "description" => "Relations where the object identifier matches the identifier provided" },
90
- "relation_object_type" => { "possible_values" => nil, "description" => "One of the identifier types from the Crossref relations schema (e.g. doi, issn)" },
91
- "public_references" => { "possible_values" => nil, "description" => "metadata where publishers allow references to be distributed publically" },
92
- "publisher_name" => { "possible_values" => nil, "description" => "metadata for records with an exact matching publisher name" },
93
- "affiliation" => { "possible_values" => nil, "description" => "metadata for records with at least one contributor with the given affiliation" }
28
+ filter_details = {
29
+ 'has_funder' => { 'possible_values' => nil, 'description' => 'metadata which includes one or more funder entry' },
30
+ 'funder' => { 'possible_values' => '{funder_id}', 'description' => 'metadata which include the {funder_id} in FundRef data' },
31
+ 'location' => { 'possible_values' => '{country_name}', 'description' => 'funder records where location = {country name}. Only works on /funders route' },
32
+ 'prefix' => { 'possible_values' => '{owner_prefix}', 'description' => "metadata belonging to a DOI owner prefix {owner_prefix} (e.g. '10.1016' )" },
33
+ 'member' => { 'possible_values' => '{member_id}', 'description' => 'metadata belonging to a CrossRef member' },
34
+ 'from_index_date' => { 'possible_values' => '{date}', 'description' => 'metadata indexed since (inclusive) {date}' },
35
+ 'until_index_date' => { 'possible_values' => '{date}', 'description' => 'metadata indexed before (inclusive) {date}' },
36
+ 'from_deposit_date' => { 'possible_values' => '{date}', 'description' => 'metadata last (re)deposited since (inclusive) {date}' },
37
+ 'until_deposit_date' => { 'possible_values' => '{date}', 'description' => 'metadata last (re)deposited before (inclusive) {date}' },
38
+ 'from_update_date' => { 'possible_values' => '{date}', 'description' => "Metadata updated since (inclusive) {date} Currently the same as 'from_deposit_date'" },
39
+ 'until_update_date' => { 'possible_values' => '{date}', 'description' => "Metadata updated before (inclusive) {date} Currently the same as 'until_deposit_date'" },
40
+ 'from_created_date' => { 'possible_values' => '{date}', 'description' => 'metadata first deposited since (inclusive) {date}' },
41
+ 'until_created_date' => { 'possible_values' => '{date}', 'description' => 'metadata first deposited before (inclusive) {date}' },
42
+ 'from_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where published date is since (inclusive) {date}' },
43
+ 'until_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where published date is before (inclusive) {date}' },
44
+ 'from_online_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where online published date is since (inclusive) {date}' },
45
+ 'until_online_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where online published date is before (inclusive) {date}' },
46
+ 'from_print_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where print published date is since (inclusive) {date}' },
47
+ 'until_print_pub_date' => { 'possible_values' => '{date}', 'description' => 'metadata where print published date is before (inclusive) {date}' },
48
+ 'from_posted_date' => { 'possible_values' => '{date}', 'description' => 'metadata where posted date is since (inclusive) {date}' },
49
+ 'until_posted_date' => { 'possible_values' => '{date}', 'description' => 'metadata where posted date is before (inclusive) {date}' },
50
+ 'from_accepted_date' => { 'possible_values' => '{date}', 'description' => 'metadata where accepted date is since (inclusive) {date}' },
51
+ 'until_accepted_date' => { 'possible_values' => '{date}', 'description' => 'metadata where accepted date is before (inclusive) {date}' },
52
+ 'has_license' => { 'possible_values' => nil, 'description' => "metadata that includes any '<license_ref>' elements" },
53
+ 'license_url' => { 'possible_values' => '{url}', 'description' => "metadata where '<license_ref>' value equals {url}" },
54
+ 'license_version' => { 'possible_values' => '{string}', 'description' => "metadata where the '<license_ref>''s 'applies_to' attribute is '{string}'" },
55
+ 'license_delay' => { 'possible_values' => '{integer}', 'description' => "metadata where difference between publication date and the '<license_ref>''s 'start_date' attribute is <= '{integer}' (in days" },
56
+ 'has_full_text' => { 'possible_values' => nil, 'description' => "metadata that includes any full text '<resource>' elements_" },
57
+ 'full_text_version' => { 'possible_values' => '{string}', 'description' => "metadata where '<resource>' element's 'content_version' attribute is '{string}'" },
58
+ 'full_text_type' => { 'possible_values' => '{mime_type}', 'description' => "metadata where '<resource>' element's 'content_type' attribute is '{mime_type}' (e.g. 'application/pdf')" },
59
+ 'full_text_application' => { 'possible_values' => '{string}', 'description' => 'metadata where <resource> link has one of the following intended applications: text-mining, similarity-checking or unspecified' },
60
+ 'has_references' => { 'possible_values' => nil, 'description' => 'metadata for works that have a list of references' },
61
+ 'has_archive' => { 'possible_values' => nil, 'description' => 'metadata which include name of archive partner' },
62
+ 'archive' => { 'possible_values' => '{string}', 'description' => "metadata which where value of archive partner is '{string}'" },
63
+ 'has_orcid' => { 'possible_values' => nil, 'description' => 'metadata which includes one or more ORCIDs' },
64
+ 'has_authenticated_orcid' => { 'possible_values' => nil, 'description' => 'metadata which includes one or more ORCIDs where the depositing publisher claims to have witness the ORCID owner authenticate with ORCID' },
65
+ 'orcid' => { 'possible_values' => '{orcid}', 'description' => "metadata where '<orcid>' element's value = '{orcid}'" },
66
+ 'issn' => { 'possible_values' => '{issn}', 'description' => "metadata where record has an ISSN = '{issn}' Format is 'xxxx_xxxx'." },
67
+ 'directory' => { 'possible_values' => '{directory}', 'description' => "metadata records whose article or serial are mentioned in the given '{directory}'. Currently the only supported value is 'doaj'" },
68
+ 'doi' => { 'possible_values' => '{doi}', 'description' => "metadata describing the DOI '{doi}'" },
69
+ 'updates' => { 'possible_values' => '{doi}', 'description' => "metadata for records that represent editorial updates to the DOI '{doi}'" },
70
+ 'is_update' => { 'possible_values' => nil, 'description' => 'metadata for records that represent editorial updates' },
71
+ 'has_update_policy' => { 'possible_values' => nil, 'description' => 'metadata for records that include a link to an editorial update policy' },
72
+ 'container_title' => { 'possible_values' => nil, 'description' => 'metadata for records with a publication title exactly with an exact match' },
73
+ 'category_name' => { 'possible_values' => nil, 'description' => 'metadata for records with an exact matching category label' },
74
+ 'type' => { 'possible_values' => '{type}', 'description' => "metadata records whose type = '{type}' Type must be an ID value from the list of types returned by the '/types' resource" },
75
+ 'type_name' => { 'possible_values' => nil, 'description' => 'metadata for records with an exacty matching type label' },
76
+ 'award_number' => { 'possible_values' => '{award_number}', 'description' => "metadata for records with a matching award nunber_ Optionally combine with 'award_funder'" },
77
+ 'award_funder' => { 'possible_values' => '{funder doi or id}', 'description' => "metadata for records with an award with matching funder. Optionally combine with 'award_number'" },
78
+ 'has_assertion' => { 'possible_values' => nil, 'description' => 'metadata for records with any assertions' },
79
+ 'assertion_group' => { 'possible_values' => nil, 'description' => 'metadata for records with an assertion in a particular group' },
80
+ 'assertion' => { 'possible_values' => nil, 'description' => 'metadata for records with a particular named assertion' },
81
+ 'has_affiliation' => { 'possible_values' => nil, 'description' => 'metadata for records that have any affiliation information' },
82
+ 'alternative_id' => { 'possible_values' => nil, 'description' => 'metadata for records with the given alternative ID, which may be a publisher_specific ID, or any other identifier a publisher may have provided' },
83
+ 'article_number' => { 'possible_values' => nil, 'description' => 'metadata for records with a given article number' },
84
+ 'has_abstract' => { 'possible_values' => nil, 'description' => 'metadata for records which include an abstract' },
85
+ 'has_clinical_trial_number' => { 'possible_values' => nil, 'description' => 'metadata for records which include a clinical trial number' },
86
+ 'content_domain' => { 'possible_values' => nil, 'description' => 'metadata where the publisher records a particular domain name as the location Crossmark content will appear' },
87
+ 'has_content_domain' => { 'possible_values' => nil, 'description' => 'metadata where the publisher records a domain name location for Crossmark content' },
88
+ 'has_crossmark_restriction' => { 'possible_values' => nil, 'description' => 'metadata where the publisher restricts Crossmark usage to content domains' },
89
+ 'has_relation' => { 'possible_values' => nil, 'description' => 'metadata for records that either assert or are the object of a relation' },
90
+ 'relation_type' => { 'possible_values' => nil, 'description' => 'One of the relation types from the Crossref relations schema (e.g. is-referenced-by, is-parent-of, is-preprint-of)' },
91
+ 'relation_object' => { 'possible_values' => nil, 'description' => 'Relations where the object identifier matches the identifier provided' },
92
+ 'relation_object_type' => { 'possible_values' => nil, 'description' => 'One of the identifier types from the Crossref relations schema (e.g. doi, issn)' },
93
+ 'public_references' => { 'possible_values' => nil, 'description' => 'metadata where publishers allow references to be distributed publically' },
94
+ 'publisher_name' => { 'possible_values' => nil, 'description' => 'metadata for records with an exact matching publisher name' },
95
+ 'affiliation' => { 'possible_values' => nil, 'description' => 'metadata for records with at least one contributor with the given affiliation' }
94
96
  }
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # taken from: https://viget.com/extend/easy-gem-configuration-variables-with-defaults
2
4
  module Configuration
3
-
4
5
  def configuration
5
6
  yield self
6
7
  end
@@ -22,5 +23,4 @@ module Configuration
22
23
  define_method name, &block
23
24
  end
24
25
  end
25
-
26
26
  end
@@ -1,7 +1,9 @@
1
- require "faraday"
2
- require "multi_json"
3
- require "serrano/error"
4
- require "serrano/constants"
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'faraday'
5
+ require 'multi_json'
6
+ require 'serrano/error'
5
7
  require 'serrano/utils'
6
8
  require 'serrano/helpers/configuration'
7
9
 
@@ -11,7 +13,6 @@ require 'serrano/helpers/configuration'
11
13
  # Class to perform HTTP requests to the Crossref API
12
14
  module Serrano
13
15
  class Request #:nodoc:
14
-
15
16
  attr_accessor :endpt
16
17
  attr_accessor :id
17
18
  attr_accessor :query
@@ -29,8 +30,8 @@ module Serrano
29
30
  attr_accessor :verbose
30
31
 
31
32
  def initialize(endpt, id, query, filter, offset,
32
- limit, sample, sort, order, facet, select,
33
- works, agency, options, verbose)
33
+ limit, sample, sort, order, facet, select,
34
+ works, agency, options, verbose)
34
35
 
35
36
  self.endpt = endpt
36
37
  self.id = id
@@ -50,47 +51,48 @@ module Serrano
50
51
  end
51
52
 
52
53
  def perform
53
- filt = filter_handler(self.filter)
54
+ filt = filter_handler(filter)
54
55
 
55
- self.select = self.select.join(",") if self.select && self.select.class == Array
56
+ self.select = select.join(',') if select && select.class == Array
56
57
 
57
- args = { query: self.query, filter: filt, offset: self.offset,
58
- rows: self.limit, sample: self.sample, sort: self.sort,
59
- order: self.order, facet: self.facet,
60
- select: self.select }
61
- opts = args.delete_if { |k, v| v.nil? }
58
+ args = { query: query, filter: filt, offset: offset,
59
+ rows: limit, sample: sample, sort: sort,
60
+ order: order, facet: facet,
61
+ select: select }
62
+ opts = args.delete_if { |_k, v| v.nil? }
62
63
 
63
- if verbose
64
- conn = Faraday.new(:url => Serrano.base_url, :request => options || []) do |f|
65
- f.response :logger
66
- f.use FaradayMiddleware::RaiseHttpException
67
- f.adapter Faraday.default_adapter
68
- end
69
- else
70
- conn = Faraday.new(:url => Serrano.base_url, :request => options || []) do |f|
71
- f.use FaradayMiddleware::RaiseHttpException
72
- f.adapter Faraday.default_adapter
73
- end
74
- end
64
+ conn = if verbose
65
+ Faraday.new(url: Serrano.base_url, request: options || []) do |f|
66
+ f.response :logger
67
+ f.use FaradayMiddleware::RaiseHttpException
68
+ f.adapter Faraday.default_adapter
69
+ end
70
+ else
71
+ Faraday.new(url: Serrano.base_url, request: options || []) do |f|
72
+ f.use FaradayMiddleware::RaiseHttpException
73
+ f.adapter Faraday.default_adapter
74
+ end
75
+ end
75
76
 
76
77
  conn.headers[:user_agent] = make_ua
77
- conn.headers["X-USER-AGENT"] = make_ua
78
+ conn.headers['X-USER-AGENT'] = make_ua
78
79
 
79
- if self.id.nil?
80
- res = conn.get self.endpt, opts
80
+ if id.nil?
81
+ res = conn.get endpt, opts
81
82
  return MultiJson.load(res.body)
82
83
  else
84
+ self.id = Array(id)
85
+ # url encoding
86
+ self.id = id.map { |x| ERB::Util.url_encode(x) }
83
87
  coll = []
84
- Array(self.id).each do |x|
85
- if self.works
86
- endpt = self.endpt + '/' + x.to_s + "/works"
87
- else
88
- if self.agency
89
- endpt = self.endpt + '/' + x.to_s + "/agency"
90
- else
91
- endpt = self.endpt + '/' + x.to_s
92
- end
93
- end
88
+ id.each do |x|
89
+ endpt = if works
90
+ self.endpt + '/' + x.to_s + '/works'
91
+ elsif agency
92
+ self.endpt + '/' + x.to_s + '/agency'
93
+ else
94
+ self.endpt + '/' + x.to_s
95
+ end
94
96
 
95
97
  res = conn.get endpt, opts
96
98
  coll << MultiJson.load(res.body)
@@ -1,8 +1,10 @@
1
- require "faraday"
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'faraday'
2
5
  require 'faraday_middleware'
3
- require "multi_json"
4
- require "serrano/error"
5
- require "serrano/constants"
6
+ require 'multi_json'
7
+ require 'serrano/error'
6
8
  require 'serrano/helpers/configuration'
7
9
  require 'serrano/filterhandler'
8
10
  require 'serrano/error'
@@ -15,7 +17,6 @@ require 'serrano/utils'
15
17
  # Class to perform HTTP requests to the Crossref API
16
18
  module Serrano
17
19
  class RequestCursor #:nodoc:
18
-
19
20
  attr_accessor :endpt
20
21
  attr_accessor :id
21
22
  attr_accessor :query
@@ -36,9 +37,9 @@ module Serrano
36
37
  attr_accessor :args
37
38
 
38
39
  def initialize(endpt, id, query, filter, offset,
39
- limit, sample, sort, order, facet, select,
40
- works, agency, options, verbose, cursor,
41
- cursor_max, args)
40
+ limit, sample, sort, order, facet, select,
41
+ works, agency, options, verbose, cursor,
42
+ cursor_max, args)
42
43
 
43
44
  self.endpt = endpt
44
45
  self.id = id
@@ -61,89 +62,89 @@ module Serrano
61
62
  end
62
63
 
63
64
  def perform
64
- filt = filter_handler(self.filter)
65
- fieldqueries = field_query_handler(self.args)
66
- self.select = self.select.join(",") if self.select && self.select.class == Array
65
+ filt = filter_handler(filter)
66
+ fieldqueries = field_query_handler(args)
67
+ self.select = select.join(',') if select && select.class == Array
67
68
 
68
- if self.cursor_max.class != nil
69
- if !self.cursor_max.kind_of?(Integer)
70
- raise "cursor_max must be of class int"
71
- end
69
+ unless cursor_max.class.nil?
70
+ raise 'cursor_max must be of class int' unless cursor_max.is_a?(Integer)
72
71
  end
73
72
 
74
- arguments = { query: self.query, filter: filt, offset: self.offset,
75
- rows: self.limit, sample: self.sample, sort: self.sort,
76
- order: self.order, facet: self.facet, select: self.select,
77
- cursor: self.cursor }.tostrings
73
+ arguments = { query: query, filter: filt, offset: offset,
74
+ rows: limit, sample: sample, sort: sort,
75
+ order: order, facet: facet, select: select,
76
+ cursor: cursor }.tostrings
78
77
  arguments = arguments.merge(fieldqueries)
79
- opts = arguments.delete_if { |k, v| v.nil? }
78
+ opts = arguments.delete_if { |_k, v| v.nil? }
80
79
 
81
- if verbose
82
- $conn = Faraday.new(:url => Serrano.base_url, :request => options || []) do |f|
83
- f.response :logger
84
- f.use FaradayMiddleware::RaiseHttpException
85
- f.adapter Faraday.default_adapter
86
- end
87
- else
88
- $conn = Faraday.new(:url => Serrano.base_url, :request => options || []) do |f|
89
- f.use FaradayMiddleware::RaiseHttpException
90
- f.adapter Faraday.default_adapter
91
- end
92
- end
80
+ conn = if verbose
81
+ Faraday.new(url: Serrano.base_url, request: options || []) do |f|
82
+ f.response :logger
83
+ f.use FaradayMiddleware::RaiseHttpException
84
+ f.adapter Faraday.default_adapter
85
+ end
86
+ else
87
+ Faraday.new(url: Serrano.base_url, request: options || []) do |f|
88
+ f.use FaradayMiddleware::RaiseHttpException
89
+ f.adapter Faraday.default_adapter
90
+ end
91
+ end
93
92
 
94
- $conn.headers[:user_agent] = make_ua
95
- $conn.headers["X-USER-AGENT"] = make_ua
93
+ conn.headers[:user_agent] = make_ua
94
+ conn.headers['X-USER-AGENT'] = make_ua
96
95
 
97
- if self.id.nil?
98
- $endpt2 = self.endpt
99
- js = self._req(self.endpt, opts)
96
+ if id.nil?
97
+ endpt2 = endpt
98
+ js = _req(conn, endpt, opts)
100
99
  cu = js['message']['next-cursor']
101
100
  max_avail = js['message']['total-results']
102
- res = self._redo_req(js, opts, cu, max_avail)
101
+ res = _redo_req(conn, js, opts, cu, max_avail)
103
102
  return res
104
103
  else
104
+ self.id = Array(id)
105
+ # url encoding
106
+ self.id = id.map { |x| ERB::Util.url_encode(x) }
105
107
  coll = []
106
- Array(self.id).each do |x|
107
- if self.works
108
- $endpt2 = self.endpt + '/' + x.to_s + "/works"
109
- else
110
- if self.agency
111
- $endpt2 = self.endpt + '/' + x.to_s + "/agency"
112
- else
113
- $endpt2 = self.endpt + '/' + x.to_s
114
- end
115
- end
108
+ id.each do |x|
109
+ endpt2 = if works
110
+ endpt + '/' + x.to_s + '/works'
111
+ else
112
+ endpt2 = if agency
113
+ endpt + '/' + x.to_s + '/agency'
114
+ else
115
+ endpt + '/' + x.to_s
116
+ end
117
+ end
116
118
 
117
- js = self._req($endpt2, opts)
119
+ js = _req(conn, endpt2, opts)
118
120
  cu = js['message']['next-cursor']
119
121
  max_avail = js['message']['total-results']
120
- coll << self._redo_req(js, opts, cu, max_avail)
122
+ coll << _redo_req(conn, js, opts, cu, max_avail)
121
123
  end
122
124
  return coll
123
125
  end
124
126
  end
125
127
 
126
- def _redo_req(js, opts, cu, max_avail)
127
- if !cu.nil? and self.cursor_max > js['message']['items'].length
128
+ def _redo_req(conn, js, opts, cu, max_avail)
129
+ if !cu.nil? && (cursor_max > js['message']['items'].length)
128
130
  res = [js]
129
131
  total = js['message']['items'].length
130
- while !cu.nil? and self.cursor_max > total and total < max_avail do
132
+ while !cu.nil? && (cursor_max > total) && (total < max_avail)
131
133
  opts[:cursor] = cu
132
- out = self._req($endpt2, opts)
134
+ out = _req(conn, endpt2, opts)
133
135
  cu = out['message']['next-cursor']
134
136
  res << out
135
- total = res.collect {|x| x['message']['items'].length}.reduce(0, :+)
137
+ total = res.collect { |x| x['message']['items'].length }.reduce(0, :+)
136
138
  end
137
- return res
139
+ res
138
140
  else
139
- return js
141
+ js
140
142
  end
141
143
  end
142
144
 
143
- def _req(path, opts)
144
- res = $conn.get path, opts
145
- return MultiJson.load(res.body)
145
+ def _req(conn, path, opts)
146
+ res = conn.get path, opts
147
+ MultiJson.load(res.body)
146
148
  end
147
-
148
149
  end
149
150
  end