wikirate4ruby 1.0.0

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.
@@ -0,0 +1,43 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class Metric < Entities::Card
6
+ ATTRIBUTES = %i[metric_name metric_designer metric_type question about methodology value_type value_options report_type
7
+ research_policy unit range hybrid topics scores formula rubric variables answers bookmarkers datasets
8
+ companies calculations answers_url].freeze
9
+ attr_reader(*ATTRIBUTES)
10
+
11
+ def initialize(metric)
12
+ super metric
13
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not a Metric but a #{@type}" if @type != 'Metric'
14
+
15
+ @metric_name = @data['title']
16
+ @metric_designer = @data['designer']
17
+ @metric_type = get_content 'metric_type'
18
+ @question = get_content 'question'
19
+ @about = get_content 'about'
20
+ @methodology = get_content 'methodology'
21
+ @value_options = get_content 'value_options'
22
+ @value_type = get_content 'value_type'
23
+ @report_type = get_content 'report_type'
24
+ @research_policy = get_content 'research_policy'
25
+ @unit = get_content 'unit'
26
+ @range = get_content 'range'
27
+ @hybrid = get_content 'hybrid'
28
+ @topics = get_content 'topics'
29
+ @scores = get_content 'scores'
30
+ @formula = get_content 'formula'
31
+ @rubric = get_content 'rubric'
32
+ @variables = get_content 'variables'
33
+ @answers = get_content 'answers'
34
+ @bookmarkers = get_content 'bookmarkers'
35
+ @datasets = get_content 'datasets'
36
+ @companies = get_content 'companies'
37
+ @calculations = get_content 'calculations'
38
+ @answers_url = get_content 'answers_url'
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class Region < Entities::Card
6
+ ATTRIBUTES = %i[jurisdiction_code ilo_region country].freeze
7
+ attr_reader(*ATTRIBUTES)
8
+
9
+ def initialize(region)
10
+ super region
11
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not a Region but a #{@type}" if @type != 'Region'
12
+
13
+ @jurisdiction_code = @data["items"][1]["content"]
14
+ @ilo_region = @data["items"][2]["content"]
15
+ @country = @data["items"][3]["content"][0]
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class RelationshipAnswer < Entities::Answer
6
+ ATTRIBUTES = %i[metric_id inverse_metric_id subject_company subject_company_id object_company object_company_id].freeze
7
+ attr_reader(*ATTRIBUTES)
8
+
9
+ def initialize(relationship_answer)
10
+ super relationship_answer
11
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not an Relationship Answer but a #{@type}" if @type != 'Relationship Answer'
12
+
13
+ @metric_id = get_content 'metric_id'
14
+ @inverse_metric_id = get_content 'inverse_metric_id'
15
+ @subject_company = get_name 'subject_company'
16
+ @subject_company_id = get_content 'subject_company_id'
17
+ @object_company = get_name 'object_company'
18
+ @object_company_id = get_content 'object_company_id'
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class ResearchGroup < Entities::Card
6
+ ATTRIBUTES = %i[researchers].freeze
7
+ attr_reader(*ATTRIBUTES)
8
+
9
+ def initialize(research_group)
10
+ super research_group
11
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not a Research Group but a #{@type}" if @type != 'Research Group'
12
+
13
+ @researchers = get_content 'researchers'
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class Source < Entities::Card
6
+ ATTRIBUTES = %i[title file link description report_type metrics companies
7
+ answers year].freeze
8
+ attr_reader(*ATTRIBUTES)
9
+
10
+ def initialize(source)
11
+ super source
12
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not a Source but a #{@type}" if @type != 'Source'
13
+
14
+ @title = get_content 'title'
15
+ @file = get_content 'file'
16
+ @link = get_content 'link'
17
+ @description = get_content 'description'
18
+ @report_type = get_content 'report_type'
19
+ @metrics = get_content 'metric'
20
+ @companies = get_content 'company'
21
+ @answers = get_content 'answer'
22
+ @year = get_content 'year'
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../error'
2
+
3
+ module Wikirate4ruby
4
+ module Entities
5
+ class Topic < Entities::Card
6
+ ATTRIBUTES = %i[bookmarkers metrics datasets].freeze
7
+ attr_reader(*ATTRIBUTES)
8
+
9
+ def initialize(topic)
10
+ super topic
11
+ raise parsing_error name = "IncompatibleCardType", message = "The input Card is not a Topic but a #{@type}" if @type != 'Topic'
12
+
13
+ @bookmarkers = get_content 'bookmarkers'
14
+ @metrics = get_content 'metrics'
15
+ @datasets = get_content 'datasets'
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,98 @@
1
+ module Wikirate4ruby
2
+ class Error < StandardError
3
+
4
+ # @return [Integer]
5
+ attr_reader :code
6
+
7
+ Wikirate4rubyError = Class.new(self)
8
+
9
+ # Raised when a Parsing error is occured
10
+ ParsingError = Class.new(self)
11
+
12
+ #Raised when an Incompatible Card Type is given as a type when try to parse an API response
13
+ IncompatibleCardTypeError = Class.new(ParsingError)
14
+
15
+ # Raised when Wikirate returns a 4xx HTTP status code
16
+ ClientError = Class.new(self)
17
+
18
+ # Raised when Wikirate returns the HTTP status code 400
19
+ BadRequest = Class.new(ClientError)
20
+
21
+ # Raised when Wikirate returns the HTTP status code 401
22
+ Unauthorized = Class.new(ClientError)
23
+
24
+ # Raised when Wikirate returns the HTTP status code 403
25
+ Forbidden = Class.new(ClientError)
26
+
27
+ # Raised when Wikirate returns the HTTP status code 413
28
+ RequestEntityTooLarge = Class.new(ClientError)
29
+
30
+ # Raised when Wikirate returns the HTTP status code 404
31
+ NotFound = Class.new(ClientError)
32
+
33
+ # Raised when Wikirate returns the HTTP status code 406
34
+ NotAcceptable = Class.new(ClientError)
35
+
36
+ # Raised when Wikirate returns the HTTP status code 422
37
+ UnprocessableEntity = Class.new(ClientError)
38
+
39
+ # Raised when Wikirate returns the HTTP status code 429
40
+ TooManyRequests = Class.new(ClientError)
41
+
42
+ # Raised when Wikirate returns a 5xx HTTP status code
43
+ ServerError = Class.new(self)
44
+
45
+ # Raised when Wikirate returns the HTTP status code 500
46
+ InternalServerError = Class.new(ServerError)
47
+
48
+ # Raised when Wikirate returns the HTTP status code 502
49
+ BadGateway = Class.new(ServerError)
50
+
51
+ # Raised when Wikirate returns the HTTP status code 503
52
+ ServiceUnavailable = Class.new(ServerError)
53
+
54
+ # Raised when Wikirate returns the HTTP status code 504
55
+ GatewayTimeout = Class.new(ServerError)
56
+
57
+ # Raised when an operation subject to timeout takes too long
58
+ TimeoutError = Class.new(self)
59
+
60
+ HTTP_ERRORS = {
61
+ 400 => Wikirate4ruby::Error::BadRequest,
62
+ 401 => Wikirate4ruby::Error::Unauthorized,
63
+ 403 => Wikirate4ruby::Error::Forbidden,
64
+ 404 => Wikirate4ruby::Error::NotFound,
65
+ 406 => Wikirate4ruby::Error::NotAcceptable,
66
+ 413 => Wikirate4ruby::Error::RequestEntityTooLarge,
67
+ 422 => Wikirate4ruby::Error::UnprocessableEntity,
68
+ 429 => Wikirate4ruby::Error::TooManyRequests,
69
+ 500 => Wikirate4ruby::Error::InternalServerError,
70
+ 502 => Wikirate4ruby::Error::BadGateway,
71
+ 503 => Wikirate4ruby::Error::ServiceUnavailable,
72
+ 504 => Wikirate4ruby::Error::GatewayTimeout
73
+ }.freeze
74
+
75
+ PARSING_ERRORS = {
76
+ 'IncompatibleCardType' => Wikirate4ruby::Error::IncompatibleCardTypeError
77
+ }.freeze
78
+
79
+ class << self
80
+
81
+ def from_processing_response(error)
82
+ klass = PARSING_ERRORS[error[:name]] || self
83
+ message = error[:message]
84
+ klass.new(message)
85
+ end
86
+ end
87
+
88
+ # Initializes a new Error object
89
+ #
90
+ # @param message [Exception, String]
91
+ # @param code [Integer]
92
+ # @return [Wikirate4ruby::Error]
93
+ def initialize(message = '', code = nil)
94
+ super(message)
95
+ @code = code
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,99 @@
1
+ require 'faraday'
2
+ require 'logger'
3
+ require_relative './request_utils'
4
+
5
+ module Wikirate4ruby
6
+ module REST
7
+ class Request
8
+ include Wikirate4ruby::RequestUtils
9
+
10
+ attr :wikirate_api_url, :api_key, :auth, :logger
11
+
12
+ def initialize(api_key, wikirate_api_url, auth = {})
13
+ @wikirate_api_url = wikirate_api_url
14
+ @api_key = api_key
15
+ @auth = auth
16
+ @logger = Logger.new(STDOUT)
17
+ end
18
+
19
+ # @param [String] endpoint
20
+ # @param [Array] endpoint_params
21
+ # @param [Array] filters
22
+ # @param [Hash] params
23
+ def get(endpoint, endpoint_params = [], filters = [], params = {})
24
+ data = {}
25
+ params.each do |key, value|
26
+ next if value.nil?
27
+
28
+ if endpoint_params.include? key
29
+ data[key] = value.to_s
30
+ elsif filters.include? key
31
+ if %w[value_from value_to].include?(key)
32
+ data["filter[value][#{key.gsub(/.*_/, '')}]"] = value.to_s
33
+ else
34
+ data["filter[#{key}]"] = value.to_s
35
+ end
36
+ else
37
+ @logger.warn("Unexpected parameter #{key}")
38
+ end
39
+ end
40
+ request(:get, endpoint, data)
41
+ end
42
+
43
+ def post(endpoint, data = {})
44
+ request(:post, endpoint, data)
45
+ end
46
+
47
+ private
48
+
49
+ # @param [String] method
50
+ # @param [String] endpoint
51
+ # @param [Array] data
52
+ # @param [Hash] _options
53
+ # @return [Hash]
54
+ def request(method, endpoint, data = {}, **_options)
55
+ url = URI.parse("#{@wikirate_api_url}#{endpoint}")
56
+
57
+ connection = Faraday.new do |conn|
58
+ conn.request :authorization, :basic, @auth[:username], @auth[:password] unless auth.nil?
59
+ conn.request :url_encoded
60
+ conn.request :json
61
+ conn.response :json
62
+ end
63
+
64
+ response = case method
65
+ when :get
66
+ connection.get(url) do |req|
67
+ req.params = data
68
+ req.headers = headers(@api_key)
69
+ end
70
+ when :post
71
+ connection.post(url) do |req|
72
+ req.params = data
73
+ req.headers = headers(@api_key)
74
+ end
75
+ when :put
76
+ connection.put(url) do |req|
77
+ req.data = data
78
+ req.headers = headers(@api_key)
79
+ end
80
+ when :delete
81
+ connection.delete(url) do |req|
82
+ req.data = data
83
+ req.headers = headers(@api_key)
84
+ end
85
+ end
86
+
87
+ #follow redirect
88
+ if response.status == 303
89
+ endpoint = response.headers['location']
90
+ endpoint[wikirate_api_url] = ''
91
+ return get(endpoint)
92
+ end
93
+
94
+ return response.body unless response.status != 200
95
+ raise(error(response.status, response.body))
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,36 @@
1
+ module Wikirate4ruby
2
+ module RequestUtils
3
+
4
+ private
5
+
6
+ def creation_params(entity, optional_params = {}, allowed_params = [])
7
+ params = {
8
+ 'card[type]' => entity,
9
+ 'format' => 'json',
10
+ 'success[format]' => 'json'
11
+ }
12
+
13
+ optional_params.each do |key, value|
14
+ next if value.nil?
15
+
16
+ if allowed_params.include? key
17
+ params["card[subcards][+#{key}]"] = (%w[company subject_company object_company].include? key) ? str_identifier(value) : value.to_s
18
+ else
19
+ @logger.warn("Unexpected parameter: #{key}")
20
+ end
21
+ end
22
+ params
23
+ end
24
+
25
+ def error(code, body)
26
+ Wikirate4ruby::Error::HTTP_ERRORS[code].new(JSON.pretty_generate(body))
27
+ end
28
+
29
+ def headers(api_key)
30
+ {
31
+ 'Content-Type' => 'application/json',
32
+ 'X-API-KEY' => api_key
33
+ }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module Wikirate4ruby
2
+ module Utils
3
+
4
+ private
5
+
6
+ def str_identifier(identifier)
7
+ (identifier.is_a? String) ? "#{tranform_to_wr_friendly_name(identifier)}" : "~#{identifier}"
8
+ end
9
+
10
+ def tranform_to_wr_friendly_name(name)
11
+ name = name.gsub(%r{,|\.|/|\(|\)|&|;|@|#|\^|!|=|"|'|%|-}, ' ').squeeze(' ')
12
+ name.strip!
13
+ name.gsub(/ /, '_')
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Wikirate4ruby
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "wikirate4ruby/version"
2
+ require_relative "wikirate4ruby/entities/card"
3
+ require_relative "wikirate4ruby/entities/answer"
4
+ require_relative "wikirate4ruby/entities/checked_by"
5
+ require_relative "wikirate4ruby/entities/company_group"
6
+ require_relative "wikirate4ruby/entities/company"
7
+ require_relative "wikirate4ruby/entities/dataset"
8
+ require_relative "wikirate4ruby/entities/metric"
9
+ require_relative "wikirate4ruby/entities/region"
10
+ require_relative "wikirate4ruby/entities/relationship_answer"
11
+ require_relative "wikirate4ruby/entities/source"
12
+ require_relative "wikirate4ruby/entities/topic"
13
+ require_relative "wikirate4ruby/client"
14
+ require_relative "wikirate4ruby/error"
15
+ require_relative "wikirate4ruby/request_utils"
16
+ require_relative "wikirate4ruby/utils"
17
+ require_relative "wikirate4ruby/request"
18
+
19
+ module Wikirate4ruby
20
+ module REST
21
+ end
22
+ module Entities
23
+ end
24
+ module RequestUtils
25
+ end
26
+ module StringUtils
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module Wikirate4ruby
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wikirate4ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vasiliki Gkatziaki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.4
27
+ description: 'The Wikirate4ruby is an open-source ruby library for the Wikirate API. '
28
+ email:
29
+ - vasgat@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".standard.yml"
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - Gemfile
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - lib/wikirate4ruby.rb
42
+ - lib/wikirate4ruby/client.rb
43
+ - lib/wikirate4ruby/entities/answer.rb
44
+ - lib/wikirate4ruby/entities/card.rb
45
+ - lib/wikirate4ruby/entities/checked_by.rb
46
+ - lib/wikirate4ruby/entities/company.rb
47
+ - lib/wikirate4ruby/entities/company_group.rb
48
+ - lib/wikirate4ruby/entities/dataset.rb
49
+ - lib/wikirate4ruby/entities/metric.rb
50
+ - lib/wikirate4ruby/entities/region.rb
51
+ - lib/wikirate4ruby/entities/relationship_answer.rb
52
+ - lib/wikirate4ruby/entities/research_group.rb
53
+ - lib/wikirate4ruby/entities/source.rb
54
+ - lib/wikirate4ruby/entities/topic.rb
55
+ - lib/wikirate4ruby/error.rb
56
+ - lib/wikirate4ruby/request.rb
57
+ - lib/wikirate4ruby/request_utils.rb
58
+ - lib/wikirate4ruby/utils.rb
59
+ - lib/wikirate4ruby/version.rb
60
+ - sig/wikirate4ruby.rbs
61
+ homepage: https://github.com/wikirate/wikirate4ruby
62
+ licenses:
63
+ - GPL-3.0-or-later
64
+ metadata:
65
+ homepage_uri: https://github.com/wikirate/wikirate4ruby
66
+ source_code_uri: https://github.com/wikirate/wikirate4ruby
67
+ changelog_uri: https://github.com/wikirate/wikirate4ruby
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.3.7
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A Simple API wrapper for Wikirate's API
87
+ test_files: []