firmenwissen 1.0.3 → 1.2.1

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: 6499228c4827e93416dd1c35718fb4ecd483c67e5ef14f1bd35be6596a6b8acd
4
- data.tar.gz: 990c0a68b091c84e7d7046ee8606705c01ab8c67753afac1809621f2a94ba794
3
+ metadata.gz: 42e291ddf7bad7c2089c96877b831d5ab022482cd002018ded618e3a82be2f5a
4
+ data.tar.gz: 3448ce23abb5250d597f99c8fba7617ab98b7a2c51ca28df0b5f1778f239a4d7
5
5
  SHA512:
6
- metadata.gz: f242d64753d4dd251562c8550b5e7b0547579acb8d53f338d42fa6e42ad5d48655d607925e3bcfb855b9c9d15f02e7b59ca09f24eb295b0e1fc63edfe0be3c9a
7
- data.tar.gz: 55c3ca5f9fed873de5570fb0e80e70bee788bbbdf5e88372d365380ab67bca61af6d561a7513237aa6300f8043c7b312454024be16c74e3181dd46fdc18e6cf4
6
+ metadata.gz: 9ba9040ccff5e27f74c7329e32e1e40e0fb4ef7a394ceb9afca0db95f91928928ef1d7839ebecbc51d7a40c4cec1187e97d1ad8a4a8bfd6a49134e07e1593067
7
+ data.tar.gz: 58478133681a39f6c479a981463351e88a019d197b5989ed89d55b9ad20364a7ed450cdda012d56a70ada0e845ce8ae7d4851f3cad96c0721ca9fe6c037c401c
data/README.md CHANGED
@@ -45,17 +45,32 @@ suggestion.address # => 'Louise-Dumon-Straße 5'
45
45
 
46
46
  suggestion.to_h # => { crefo_id: '1234567890', name: 'COMPEON GmbH', ... }
47
47
  ```
48
- ### Configuration
48
+ ### Basic Auth Configuration
49
49
  ```ruby
50
50
  Firmenwissen.configure do |config|
51
- config.user = 'username' # Username for Firmenwissen basic auth (required)
52
- config.password = 'password' # Password for Firmenwissen basic auth (required)
53
- config.timeout = 5 # Request timeout in seconds
51
+ config.authentication_strategy = 'basic' # This is set to 'basic' by default, eliminating the need for explicit configuration.
52
+ config.user = 'username' # Username for Firmenwissen basic auth (required)
53
+ config.password = 'password' # Password for Firmenwissen basic auth (required)
54
+ config.timeout = 5 # Request timeout in seconds
55
+ config.persistent_session = false # Whether to store/use session information for subsequent requests
54
56
 
55
57
  # Configure the endpoint yourself. %s will be replaced by the actual query
56
58
  config.endpoint = 'https://example.com/search?query={query}'
57
59
  end
58
60
  ```
61
+ ### Api Key Configuration
62
+ ```ruby
63
+ Firmenwissen.configure do |config|
64
+ config.authentication_strategy = 'api_key' # Mandatory for all options except 'basic' (required)
65
+ config.api_key = 'your api key' # API Key for Firmenwissen API Key authentication (required)
66
+ config.timeout = 5 # Request timeout in seconds
67
+ config.persistent_session = false # Whether to store/use session information for subsequent requests
68
+
69
+ # Configure the endpoint yourself. %s will be replaced by the actual query
70
+ config.endpoint = 'https://example.com/search?query={query}'
71
+ end
72
+ ```
73
+
59
74
  ### Mocking results
60
75
  In a non production-like environment you will not want to perform real requests to the API. Thus you can configure the gem to respond with mocked data.
61
76
 
@@ -93,5 +108,8 @@ end
93
108
  ```
94
109
  **Note:** All configuration options can be overridden on a per-request basis by passing an options hash to the `search` method.
95
110
 
111
+ ### On sessions
112
+ Session information is stored globally and used for _all_ subsequent requests made via the Firmenwissen gem if `persistent_session` is active.
113
+
96
114
  ## License
97
115
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -10,13 +10,15 @@ module Firmenwissen
10
10
  end
11
11
  end
12
12
 
13
- SETTINGS = %i[endpoint password mock_data mock_requests timeout user]
13
+ SETTINGS = %i[endpoint password mock_data mock_requests persistent_session timeout user authentication_strategy api_key]
14
14
 
15
15
  DEFAULT_SETTINGS = {
16
16
  endpoint: 'https://www.firmenwissen.de/search/suggest/companywithaddress/{query}{?country}',
17
17
  mock_requests: false,
18
18
  mock_data: [],
19
- timeout: 5
19
+ persistent_session: false,
20
+ timeout: 5,
21
+ authentication_strategy: "basic"
20
22
  }.freeze
21
23
 
22
24
  SETTINGS.each do |setting|
@@ -46,5 +48,13 @@ module Firmenwissen
46
48
  def mock_requests?
47
49
  mock_requests
48
50
  end
51
+
52
+ def persistent_session?
53
+ persistent_session
54
+ end
55
+
56
+ def api_key_present?
57
+ api_key.is_a?(String) && !api_key.empty?
58
+ end
49
59
  end
50
60
  end
@@ -0,0 +1,7 @@
1
+ module Firmenwissen
2
+ class ApiKeyError < StandardError
3
+ def initialize(message = 'Firmenwissen API Key is missing')
4
+ super
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Firmenwissen
2
+ class AuthenticationStrategyError < StandardError
3
+ def initialize(message = 'Invalid authentication strategy')
4
+ super
5
+ end
6
+ end
7
+ end
@@ -8,7 +8,7 @@ module Firmenwissen
8
8
  def execute
9
9
  http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.use_ssl?)
10
10
  http.read_timeout = config.timeout
11
- http.request(request)
11
+ http.request(request, &method(:extract_session))
12
12
  ensure
13
13
  http.finish
14
14
  http
@@ -21,7 +21,9 @@ module Firmenwissen
21
21
  def request
22
22
  @request ||= begin
23
23
  Net::HTTP::Get.new(uri).tap do |req|
24
- req.basic_auth(config.user, config.password)
24
+ req.basic_auth(config.user, config.password) if config.authentication_strategy == "basic"
25
+ req.add_field('API-KEY', config.api_key) if config.authentication_strategy == "api_key"
26
+ req.add_field('Cookie', session_cookie) if config.persistent_session? && !session_cookie.empty?
25
27
  end
26
28
  end
27
29
  end
@@ -29,5 +31,15 @@ module Firmenwissen
29
31
  def config
30
32
  @config ||= Firmenwissen.configuration.merge(options)
31
33
  end
34
+
35
+ def extract_session(response)
36
+ return unless config.persistent_session?
37
+
38
+ Firmenwissen::Session.update_from_set_cookie_headers(response.get_fields('Set-Cookie'))
39
+ end
40
+
41
+ def session_cookie
42
+ @session_cookie ||= Firmenwissen::Session.to_cookie_string
43
+ end
32
44
  end
33
45
  end
@@ -6,7 +6,14 @@ module Firmenwissen
6
6
  @options = options
7
7
  @params = options.fetch(:params, {})
8
8
 
9
- raise CredentialsError unless config.credentials_present?
9
+ case config.authentication_strategy
10
+ when "basic"
11
+ raise CredentialsError unless config.credentials_present?
12
+ when "api_key"
13
+ raise ApiKeyError unless config.api_key_present?
14
+ else
15
+ raise AuthenticationStrategyError
16
+ end
10
17
  end
11
18
 
12
19
  def execute
@@ -0,0 +1,26 @@
1
+ module Firmenwissen
2
+ module Session
3
+ module_function
4
+
5
+ def cookies
6
+ @cookies ||= {}
7
+ end
8
+
9
+ def update_from_set_cookie_headers(set_cookie_headers)
10
+ return if set_cookie_headers.nil?
11
+
12
+ set_cookie_headers.each do |header|
13
+ name_and_value, _other_attrs = header.split(/;\s?/)
14
+ name, value = name_and_value.split('=', 2)
15
+
16
+ next if name.empty? || value.nil? || value.empty?
17
+
18
+ cookies[name] = value
19
+ end
20
+ end
21
+
22
+ def to_cookie_string
23
+ cookies.map { |entry| [entry].join('=') }.join('; ')
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,5 @@
1
+ require 'delegate'
2
+
1
3
  class URIDecorator < SimpleDelegator
2
4
  def port
3
5
  inferred_port
@@ -1,3 +1,3 @@
1
1
  module Firmenwissen
2
- VERSION = '1.0.3'
2
+ VERSION = '1.2.1'
3
3
  end
data/lib/firmenwissen.rb CHANGED
@@ -6,6 +6,7 @@ require 'firmenwissen/configuration'
6
6
  require 'firmenwissen/http_request'
7
7
  require 'firmenwissen/key_mapper'
8
8
  require 'firmenwissen/request'
9
+ require 'firmenwissen/session'
9
10
  require 'firmenwissen/suggestion'
10
11
  require 'firmenwissen/uri_decorator'
11
12
  require 'firmenwissen/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firmenwissen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerrit Seger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-07 00:00:00.000000000 Z
11
+ date: 2024-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rexml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rspec
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +120,8 @@ files:
92
120
  - Rakefile
93
121
  - lib/firmenwissen.rb
94
122
  - lib/firmenwissen/configuration.rb
123
+ - lib/firmenwissen/errors/api_key_error.rb
124
+ - lib/firmenwissen/errors/authentication_strategy_error.rb
95
125
  - lib/firmenwissen/errors/credentials_error.rb
96
126
  - lib/firmenwissen/errors/unprocessable_response_error.rb
97
127
  - lib/firmenwissen/http_request.rb
@@ -101,6 +131,7 @@ files:
101
131
  - lib/firmenwissen/request/mock.rb
102
132
  - lib/firmenwissen/response/base.rb
103
133
  - lib/firmenwissen/response/mock.rb
134
+ - lib/firmenwissen/session.rb
104
135
  - lib/firmenwissen/suggestion.rb
105
136
  - lib/firmenwissen/uri_decorator.rb
106
137
  - lib/firmenwissen/version.rb
@@ -123,8 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
154
  - !ruby/object:Gem::Version
124
155
  version: '0'
125
156
  requirements: []
126
- rubyforge_project:
127
- rubygems_version: 2.7.3
157
+ rubygems_version: 3.1.4
128
158
  signing_key:
129
159
  specification_version: 4
130
160
  summary: Ruby client for the FirmenWissen API