swiftype-app-search 0.1.0 → 0.1.1

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.
@@ -4,9 +4,6 @@
4
4
  module SwiftypeAppSearch
5
5
  class Client
6
6
  module Documents
7
- REQUIRED_TOP_LEVEL_KEYS = [
8
- 'id'
9
- ].map!(&:freeze).to_set.freeze
10
7
 
11
8
  # Retrieve Documents from the API by IDs for the {App Search API}[https://swiftype.com/documentation/app-search/]
12
9
  #
@@ -19,6 +16,22 @@ module SwiftypeAppSearch
19
16
  get("engines/#{engine_name}/documents", ids)
20
17
  end
21
18
 
19
+ # Index a document using the {App Search API}[https://swiftype.com/documentation/app-search/].
20
+ #
21
+ # @param [String] engine_name the unique Engine name
22
+ # @param [Array] document a Document Hash
23
+ #
24
+ # @return [Hash] processed Document Status hash
25
+ #
26
+ # @raise [SwiftypeAppSearch::InvalidDocument] when the document has processing errors returned from the api
27
+ # @raise [Timeout::Error] when timeout expires waiting for statuses
28
+ def index_document(engine_name, document)
29
+ response = index_documents(engine_name, [document])
30
+ errors = response.first['errors']
31
+ raise InvalidDocument.new(errors.join('; ')) if errors.any?
32
+ response.first.tap { |h| h.delete('errors') }
33
+ end
34
+
22
35
  # Index a batch of documents using the {App Search API}[https://swiftype.com/documentation/app-search/].
23
36
  #
24
37
  # @param [String] engine_name the unique Engine name
@@ -26,10 +39,10 @@ module SwiftypeAppSearch
26
39
  #
27
40
  # @return [Array<Hash>] an Array of processed Document Status hashes
28
41
  #
29
- # @raise [SwiftypeAppSearch::InvalidDocument] when a single document is missing required fields or contains unsupported fields
42
+ # @raise [SwiftypeAppSearch::InvalidDocument] when any documents have processing errors returned from the api
30
43
  # @raise [Timeout::Error] when timeout expires waiting for statuses
31
44
  def index_documents(engine_name, documents)
32
- documents.map! { |document| validate_and_normalize_document(document) }
45
+ documents.map! { |document| normalize_document(document) }
33
46
  post("engines/#{engine_name}/documents", documents)
34
47
  end
35
48
 
@@ -43,13 +56,9 @@ module SwiftypeAppSearch
43
56
  end
44
57
 
45
58
  private
46
- def validate_and_normalize_document(document)
47
- document = Utils.stringify_keys(document)
48
- document_keys = document.keys.to_set
49
- missing_keys = REQUIRED_TOP_LEVEL_KEYS - document_keys
50
- raise InvalidDocument.new("missing required fields (#{missing_keys.to_a.join(', ')})") if missing_keys.any?
51
59
 
52
- document
60
+ def normalize_document(document)
61
+ Utils.stringify_keys(document)
53
62
  end
54
63
  end
55
64
  end
@@ -14,6 +14,7 @@ module SwiftypeAppSearch
14
14
  class BadRequest < ClientException; end
15
15
  class Forbidden < ClientException; end
16
16
  class InvalidDocument < ClientException; end
17
+ class RequestEntityTooLarge < ClientException; end
17
18
 
18
19
  class UnexpectedHTTPException < ClientException
19
20
  def initialize(http_response)
@@ -67,6 +67,8 @@ module SwiftypeAppSearch
67
67
  raise SwiftypeAppSearch::NonExistentRecord, response_json
68
68
  when Net::HTTPForbidden
69
69
  raise SwiftypeAppSearch::Forbidden, response_json
70
+ when Net::HTTPRequestEntityTooLarge
71
+ raise SwiftypeAppSearch::RequestEntityTooLarge, response_json
70
72
  else
71
73
  raise SwiftypeAppSearch::UnexpectedHTTPException, response
72
74
  end
@@ -1,3 +1,3 @@
1
1
  module SwiftypeAppSearch
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -5,13 +5,64 @@ describe SwiftypeAppSearch::Client do
5
5
  let(:client) { SwiftypeAppSearch::Client.new(:account_host_key => as_account_host_key, :api_key => as_api_key) }
6
6
 
7
7
  context 'Documents' do
8
- %i[index_documents].each do |method|
9
- context "##{method}" do
10
- it 'should validate required document fields' do
11
- documents = [{'url' => 'http://www.youtube.com/watch?v=v1uyQZNg2vE'}]
8
+ let(:document) { { 'url' => 'http://www.youtube.com/watch?v=v1uyQZNg2vE' } }
9
+
10
+ before do
11
+ client.create_engine(engine_name) rescue SwiftypeAppSearch::BadRequest
12
+ end
13
+
14
+ after do
15
+ client.destroy_engine(engine_name) rescue SwiftypeAppSearch::NonExistentRecord
16
+ end
17
+
18
+ describe '#index_document' do
19
+ subject { client.index_document(engine_name, document) }
20
+
21
+ it 'should return a processed document status hash' do
22
+ expect(subject).to match('id' => anything)
23
+ end
24
+
25
+ context 'when the document has an id' do
26
+ let(:id) { 'some_id' }
27
+ let(:document) { { 'id' => id, 'url' => 'http://www.youtube.com/watch?v=v1uyQZNg2vE' } }
28
+
29
+ it 'should return a processed document status hash with the same id' do
30
+ expect(subject).to eq('id' => id)
31
+ end
32
+ end
33
+
34
+ context 'when a document has processing errors' do
35
+ let(:document) { { 'bad' => { 'no' => 'nested hashes' } } }
36
+
37
+ it 'should raise an error when the API returns errors in the response' do
12
38
  expect do
13
- client.public_send(method, engine_name, documents)
14
- end.to raise_error(SwiftypeAppSearch::InvalidDocument, 'Error: missing required fields (id)')
39
+ subject
40
+ end.to raise_error(SwiftypeAppSearch::InvalidDocument, /Invalid field value/)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#index_documents' do
46
+ let(:documents) { [document, second_document] }
47
+ let(:second_document_id) { 'another_id' }
48
+ let(:second_document) { { 'id' => second_document_id, 'url' => 'https://www.youtube.com/watch?v=9T1vfsHYiKY' } }
49
+ subject { client.index_documents(engine_name, documents) }
50
+
51
+ it 'should return an array of document status hashes' do
52
+ expect(subject).to match([
53
+ { 'id' => anything, 'errors' => [] },
54
+ { 'id' => second_document_id, 'errors' => [] }
55
+ ])
56
+ end
57
+
58
+ context 'when one of the documents has processing errors' do
59
+ let(:second_document) { { 'bad' => { 'no' => 'nested hashes' } } }
60
+
61
+ it 'should return respective errors in an array of document processing hashes' do
62
+ expect(subject).to match([
63
+ { 'id' => anything, 'errors' => [] },
64
+ { 'id' => anything, 'errors' => ['Invalid field value: Value \'{"no"=>"nested hashes"}\' cannot be an object'] },
65
+ ])
15
66
  end
16
67
  end
17
68
  end
@@ -19,11 +70,7 @@ describe SwiftypeAppSearch::Client do
19
70
 
20
71
  context 'Engines' do
21
72
  after do
22
- # Clean up the test engine from our account
23
- begin
24
- client.destroy_engine(engine_name)
25
- rescue SwiftypeAppSearch::NonExistentRecord
26
- end
73
+ client.destroy_engine(engine_name) rescue SwiftypeAppSearch::NonExistentRecord
27
74
  end
28
75
 
29
76
  context '#create_engine' do
@@ -20,5 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency 'awesome_print', '~> 1.8'
21
21
  s.add_development_dependency 'webmock', '~> 3.3'
22
22
 
23
- s.add_runtime_dependency 'jwt', '~> 1.5'
23
+ s.add_runtime_dependency 'jwt', '~> 2.1'
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swiftype-app-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quin Hoxie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-01 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.5'
61
+ version: '2.1'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.5'
68
+ version: '2.1'
69
69
  description: API client for accessing the Swiftype App Search API with no dependencies.
70
70
  email:
71
71
  - support@swiftype.com
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  requirements: []
116
116
  rubyforge_project:
117
- rubygems_version: 2.4.5.3
117
+ rubygems_version: 2.4.5
118
118
  signing_key:
119
119
  specification_version: 4
120
120
  summary: Official gem for accessing the Swiftype App Search API