algolia 2.0.0.pre.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +146 -0
  3. data/.github/ISSUE_TEMPLATE.md +20 -0
  4. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  5. data/.gitignore +38 -0
  6. data/.rubocop.yml +186 -0
  7. data/.rubocop_todo.yml +14 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +18 -0
  10. data/LICENSE +21 -0
  11. data/README.md +56 -0
  12. data/Rakefile +45 -0
  13. data/Steepfile +6 -0
  14. data/algolia.gemspec +41 -0
  15. data/bin/console +21 -0
  16. data/bin/setup +8 -0
  17. data/lib/algolia.rb +42 -0
  18. data/lib/algolia/account_client.rb +65 -0
  19. data/lib/algolia/analytics_client.rb +105 -0
  20. data/lib/algolia/config/algolia_config.rb +40 -0
  21. data/lib/algolia/config/analytics_config.rb +20 -0
  22. data/lib/algolia/config/insights_config.rb +20 -0
  23. data/lib/algolia/config/recommendation_config.rb +20 -0
  24. data/lib/algolia/config/search_config.rb +40 -0
  25. data/lib/algolia/defaults.rb +35 -0
  26. data/lib/algolia/enums/call_type.rb +4 -0
  27. data/lib/algolia/enums/retry_outcome_type.rb +5 -0
  28. data/lib/algolia/error.rb +29 -0
  29. data/lib/algolia/helpers.rb +83 -0
  30. data/lib/algolia/http/http_requester.rb +84 -0
  31. data/lib/algolia/http/response.rb +23 -0
  32. data/lib/algolia/insights_client.rb +238 -0
  33. data/lib/algolia/iterators/base_iterator.rb +19 -0
  34. data/lib/algolia/iterators/object_iterator.rb +27 -0
  35. data/lib/algolia/iterators/paginator_iterator.rb +44 -0
  36. data/lib/algolia/iterators/rule_iterator.rb +9 -0
  37. data/lib/algolia/iterators/synonym_iterator.rb +9 -0
  38. data/lib/algolia/logger_helper.rb +14 -0
  39. data/lib/algolia/recommendation_client.rb +60 -0
  40. data/lib/algolia/responses/add_api_key_response.rb +38 -0
  41. data/lib/algolia/responses/base_response.rb +9 -0
  42. data/lib/algolia/responses/delete_api_key_response.rb +40 -0
  43. data/lib/algolia/responses/indexing_response.rb +28 -0
  44. data/lib/algolia/responses/multiple_batch_indexing_response.rb +29 -0
  45. data/lib/algolia/responses/multiple_response.rb +45 -0
  46. data/lib/algolia/responses/restore_api_key_response.rb +36 -0
  47. data/lib/algolia/responses/update_api_key_response.rb +39 -0
  48. data/lib/algolia/search_client.rb +614 -0
  49. data/lib/algolia/search_index.rb +1094 -0
  50. data/lib/algolia/transport/request_options.rb +94 -0
  51. data/lib/algolia/transport/retry_strategy.rb +117 -0
  52. data/lib/algolia/transport/stateful_host.rb +26 -0
  53. data/lib/algolia/transport/transport.rb +161 -0
  54. data/lib/algolia/user_agent.rb +25 -0
  55. data/lib/algolia/version.rb +3 -0
  56. data/sig/config/algolia_config.rbs +24 -0
  57. data/sig/config/analytics_config.rbs +11 -0
  58. data/sig/config/insights_config.rbs +11 -0
  59. data/sig/config/recommendation_config.rbs +11 -0
  60. data/sig/config/search_config.rbs +11 -0
  61. data/sig/enums/call_type.rbs +5 -0
  62. data/sig/helpers.rbs +12 -0
  63. data/sig/http/http_requester.rbs +17 -0
  64. data/sig/http/response.rbs +14 -0
  65. data/sig/interfaces/_connection.rbs +16 -0
  66. data/sig/iterators/base_iterator.rbs +15 -0
  67. data/sig/iterators/object_iterator.rbs +6 -0
  68. data/sig/iterators/paginator_iterator.rbs +8 -0
  69. data/sig/iterators/rule_iterator.rbs +5 -0
  70. data/sig/iterators/synonym_iterator.rbs +5 -0
  71. data/sig/transport/request_options.rbs +33 -0
  72. data/sig/transport/stateful_host.rbs +21 -0
  73. data/test/algolia/integration/account_client_test.rb +47 -0
  74. data/test/algolia/integration/analytics_client_test.rb +113 -0
  75. data/test/algolia/integration/base_test.rb +9 -0
  76. data/test/algolia/integration/insights_client_test.rb +80 -0
  77. data/test/algolia/integration/mocks/mock_requester.rb +45 -0
  78. data/test/algolia/integration/recommendation_client_test.rb +30 -0
  79. data/test/algolia/integration/search_client_test.rb +361 -0
  80. data/test/algolia/integration/search_index_test.rb +698 -0
  81. data/test/algolia/unit/helpers_test.rb +69 -0
  82. data/test/algolia/unit/retry_strategy_test.rb +139 -0
  83. data/test/algolia/unit/user_agent_test.rb +16 -0
  84. data/test/test_helper.rb +89 -0
  85. data/upgrade_guide.md +595 -0
  86. metadata +307 -0
@@ -0,0 +1,19 @@
1
+ module Algolia
2
+ class BaseIterator
3
+ include Helpers
4
+ include Enumerable
5
+
6
+ attr_reader :transporter, :index_name, :opts
7
+
8
+ # @param transporter [Transport::Transport] transporter used for requests
9
+ # @param index_name [String] Name of the index
10
+ # @param opts [Hash] contains extra parameters to send with your query
11
+ #
12
+ def initialize(transporter, index_name, opts)
13
+ @transporter = transporter
14
+ @index_name = index_name
15
+ @opts = opts
16
+ @response = nil
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module Algolia
2
+ class ObjectIterator < BaseIterator
3
+ # Custom each function to iterate through the objects
4
+ #
5
+ def each
6
+ loop do
7
+ data = {}
8
+
9
+ if @response
10
+ if @response[:hits].length
11
+ @response[:hits].each do |hit|
12
+ yield hit
13
+ end
14
+
15
+ if @response[:cursor].nil?
16
+ @response = nil
17
+ raise StopIteration
18
+ else
19
+ data[:cursor] = @response[:cursor]
20
+ end
21
+ end
22
+ end
23
+ @response = @transporter.read(:POST, path_encode('1/indexes/%s/browse', @index_name), data, @opts)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,44 @@
1
+ module Algolia
2
+ class PaginatorIterator < BaseIterator
3
+ # @param transporter [Transport::Transport] transporter used for requests
4
+ # @param index_name [String] Name of the index
5
+ # @param opts [Hash] contains extra parameters to send with your query
6
+ #
7
+ def initialize(transporter, index_name, opts)
8
+ super(transporter, index_name, opts)
9
+
10
+ @data = {
11
+ hitsPerPage: 1000,
12
+ page: 0
13
+ }
14
+ end
15
+
16
+ def each
17
+ loop do
18
+ if @response
19
+ if @response[:hits].length
20
+ @response[:hits].each do |hit|
21
+ hit.delete(:_highlightResult)
22
+ yield hit
23
+ end
24
+
25
+ if @response[:nbHits] < @data[:hitsPerPage]
26
+ @response = nil
27
+ @data = {
28
+ hitsPerPage: 1000,
29
+ page: 0
30
+ }
31
+ raise StopIteration
32
+ end
33
+ end
34
+ end
35
+ @response = @transporter.read(:POST, get_endpoint, @data, @opts)
36
+ @data[:page] += 1
37
+ end
38
+ end
39
+
40
+ def get_endpoint
41
+ raise AlgoliaError, 'Method must be implemented'
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ module Algolia
2
+ class RuleIterator < PaginatorIterator
3
+ # Creates the endpoint on which to fetch rules
4
+ #
5
+ def get_endpoint
6
+ path_encode('1/indexes/%s/rules/search', @index_name)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Algolia
2
+ class SynonymIterator < PaginatorIterator
3
+ # Creates the endpoint on which to fetch synonyms
4
+ #
5
+ def get_endpoint
6
+ path_encode('1/indexes/%s/synonyms/search', @index_name)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require 'logger'
2
+
3
+ module Algolia
4
+ class LoggerHelper
5
+ # @param debug_file [nil|String] file used to output the logs
6
+ #
7
+ def self.create(debug_file = nil)
8
+ file = debug_file || File.new('debug.log')
9
+ instance = ::Logger.new file
10
+ instance.progname = 'algolia'
11
+ instance
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ module Algolia
2
+ module Recommendation
3
+ class Client
4
+ # Initializes the Recommendation client
5
+ #
6
+ # @param recommendation_config [Recommendation::Config] a Recommendation::Config object which contains your APP_ID and API_KEY
7
+ # @option adapter [Object] adapter object used for the connection
8
+ # @option logger [Object]
9
+ # @option http_requester [Object] http_requester object used for the connection
10
+ #
11
+ def initialize(recommendation_config, opts = {})
12
+ @config = recommendation_config
13
+ adapter = opts[:adapter] || Defaults::ADAPTER
14
+ logger = opts[:logger] || LoggerHelper.create('debug.log')
15
+ requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, logger)
16
+ @transporter = Transport::Transport.new(@config, requester)
17
+ end
18
+
19
+ # Create a new client providing only app ID and API key
20
+ #
21
+ # @param app_id [String] Algolia application ID
22
+ # @param api_key [String] Algolia API key
23
+ #
24
+ # @return self
25
+ #
26
+ def self.create(app_id, api_key)
27
+ config = Recommendation::Config.new(app_id: app_id, api_key: api_key)
28
+ create_with_config(config)
29
+ end
30
+
31
+ # Create a new client providing only an Recommendation::Config object
32
+ #
33
+ # @param config [Recommendation::Config]
34
+ #
35
+ # @return self
36
+ #
37
+ def self.create_with_config(config)
38
+ new(config)
39
+ end
40
+
41
+ # Set the personalization strategy.
42
+ #
43
+ # @param personalization_strategy [Hash] A strategy object.
44
+ #
45
+ # @return [Hash]
46
+ #
47
+ def set_personalization_strategy(personalization_strategy, opts = {})
48
+ @transporter.write(:POST, '1/strategies/personalization', personalization_strategy, opts)
49
+ end
50
+
51
+ # Get the personalization strategy.
52
+ #
53
+ # @return [Hash]
54
+ #
55
+ def get_personalization_strategy(opts = {})
56
+ @transporter.read(:GET, '1/strategies/personalization', {}, opts)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,38 @@
1
+ module Algolia
2
+ class AddApiKeyResponse < BaseResponse
3
+ attr_reader :raw_response
4
+
5
+ # @param client [Search::Client] Algolia Search Client used for verification
6
+ # @param response [Hash] Raw response from the client
7
+ #
8
+ def initialize(client, response)
9
+ @client = client
10
+ @raw_response = response
11
+ @done = false
12
+ end
13
+
14
+ # Wait for the task to complete
15
+ #
16
+ # @param opts [Hash] contains extra parameters to send with your query
17
+ #
18
+ def wait(opts = {})
19
+ retries_count = 1
20
+
21
+ until @done
22
+ begin
23
+ @client.get_api_key(@raw_response[:key], opts)
24
+ @done = true
25
+ rescue AlgoliaError => e
26
+ if e.code != 404
27
+ raise e
28
+ end
29
+ retries_count += 1
30
+ time_before_retry = retries_count * Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY
31
+ sleep(time_before_retry / 1000)
32
+ end
33
+ end
34
+
35
+ self
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module Algolia
2
+ class BaseResponse
3
+ include Helpers
4
+
5
+ def wait
6
+ raise StandardError, 'This method should be implemented'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module Algolia
2
+ class DeleteApiKeyResponse < BaseResponse
3
+ attr_reader :raw_response
4
+
5
+ # @param client [Search::Client] Algolia Search Client used for verification
6
+ # @param response [Hash] Raw response from the client
7
+ # @param key [String] the key to check
8
+ #
9
+ def initialize(client, response, key)
10
+ @client = client
11
+ @raw_response = response
12
+ @key = key
13
+ @done = false
14
+ end
15
+
16
+ # Wait for the task to complete
17
+ #
18
+ # @param opts [Hash] contains extra parameters to send with your query
19
+ #
20
+ def wait(opts = {})
21
+ retries_count = 1
22
+
23
+ until @done
24
+ begin
25
+ @client.get_api_key(@key, opts)
26
+ rescue AlgoliaError => e
27
+ @done = e.code == 404
28
+
29
+ unless @done
30
+ retries_count += 1
31
+ time_before_retry = retries_count * Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY
32
+ sleep(time_before_retry / 1000)
33
+ end
34
+ end
35
+ end
36
+
37
+ self
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ module Algolia
2
+ class IndexingResponse < BaseResponse
3
+ attr_reader :raw_response
4
+
5
+ # @param index [Search::Index] Algolia Search Index used for verification
6
+ # @param response [Hash] Raw response from the client
7
+ #
8
+ def initialize(index, response)
9
+ @index = index
10
+ @raw_response = response
11
+ @done = false
12
+ end
13
+
14
+ # Wait for the task to complete
15
+ #
16
+ # @param opts [Hash] contains extra parameters to send with your query
17
+ #
18
+ def wait(opts = {})
19
+ unless @done
20
+ task_id = get_option(@raw_response, 'taskID')
21
+ @index.wait_task(task_id, Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts)
22
+ end
23
+
24
+ @done = true
25
+ self
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ module Algolia
2
+ class MultipleIndexBatchIndexingResponse < BaseResponse
3
+ attr_reader :raw_response
4
+
5
+ # @param client [Search::Client] Algolia Search Client used for verification
6
+ # @param response [Hash] Raw response from the client
7
+ #
8
+ def initialize(client, response)
9
+ @client = client
10
+ @raw_response = response
11
+ @done = false
12
+ end
13
+
14
+ # Wait for the task to complete
15
+ #
16
+ # @param opts [Hash] contains extra parameters to send with your query
17
+ #
18
+ def wait(opts = {})
19
+ unless @done
20
+ @raw_response[:taskID].each do |index_name, task_id|
21
+ @client.wait_task(index_name, task_id, Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts)
22
+ end
23
+ end
24
+
25
+ @done = true
26
+ self
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ module Algolia
2
+ class MultipleResponse < BaseResponse
3
+ include Enumerable
4
+
5
+ # @param responses [nil|Array] array of raw responses, when provided
6
+ #
7
+ def initialize(responses = nil)
8
+ @raw_responses = responses || []
9
+ end
10
+
11
+ # Fetch the last element of the responses
12
+ #
13
+ def last
14
+ @raw_responses[@raw_responses.length - 1]
15
+ end
16
+
17
+ # Add a new response to responses
18
+ #
19
+ def push(response)
20
+ @raw_responses.push(response)
21
+ end
22
+
23
+ # Wait for the task to complete
24
+ #
25
+ # @param opts [Hash] contains extra parameters to send with your query
26
+ #
27
+ def wait(opts = {})
28
+ @raw_responses.each do |response|
29
+ response.wait(opts)
30
+ end
31
+
32
+ @raw_responses = []
33
+
34
+ self
35
+ end
36
+
37
+ # Iterates through the responses
38
+ #
39
+ def each
40
+ @raw_responses.each do |response|
41
+ yield response
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ module Algolia
2
+ class RestoreApiKeyResponse < BaseResponse
3
+ # @param index [Search::Index] Algolia Search Index used for verification
4
+ # @param key [String] the key to check
5
+ #
6
+ def initialize(client, key)
7
+ @client = client
8
+ @key = key
9
+ @done = false
10
+ end
11
+
12
+ # Wait for the task to complete
13
+ #
14
+ # @param opts [Hash] contains extra parameters to send with your query
15
+ #
16
+ def wait(opts = {})
17
+ retries_count = 1
18
+
19
+ until @done
20
+ begin
21
+ @client.get_api_key(@key, opts)
22
+ @done = true
23
+ rescue AlgoliaError => e
24
+ if e.code != 404
25
+ raise e
26
+ end
27
+ retries_count += 1
28
+ time_before_retry = retries_count * Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY
29
+ sleep(time_before_retry / 1000)
30
+ end
31
+ end
32
+
33
+ self
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ module Algolia
2
+ class UpdateApiKeyResponse < BaseResponse
3
+ include Helpers
4
+ attr_reader :raw_response
5
+
6
+ # @param client [Search::Client] Algolia Search Client used for verification
7
+ # @param response [Hash] Raw response from the client
8
+ # @param request_options [Hash] request_options used to find the api key
9
+ #
10
+ def initialize(client, response, request_options)
11
+ @client = client
12
+ @raw_response = response
13
+ @request_options = request_options
14
+ @done = false
15
+ end
16
+
17
+ # Wait for the task to complete
18
+ #
19
+ # @param opts [Hash] contains extra parameters to send with your query
20
+ #
21
+ def wait(opts = {})
22
+ retries_count = 1
23
+
24
+ until @done
25
+ begin
26
+ api_key = @client.get_api_key(@raw_response[:key], opts)
27
+ @done = hash_includes_subset?(api_key, @request_options)
28
+ rescue AlgoliaError => e
29
+ raise e unless e.code == 404
30
+ retries_count += 1
31
+ time_before_retry = retries_count * Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY
32
+ sleep(time_before_retry / 1000)
33
+ end
34
+ end
35
+
36
+ self
37
+ end
38
+ end
39
+ end