swiftype 0.0.5 → 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.
Files changed (97) hide show
  1. data/.gitignore +5 -0
  2. data/.travis.yml +6 -0
  3. data/Gemfile +1 -1
  4. data/README.md +202 -283
  5. data/lib/data/ca-bundle.crt +3554 -0
  6. data/lib/swiftype.rb +7 -31
  7. data/lib/swiftype/client.rb +439 -11
  8. data/lib/swiftype/configuration.rb +29 -4
  9. data/lib/swiftype/exceptions.rb +2 -0
  10. data/lib/swiftype/ext/backport-uri.rb +33 -0
  11. data/lib/swiftype/request.rb +88 -20
  12. data/lib/swiftype/result_set.rb +43 -6
  13. data/lib/swiftype/sso.rb +20 -0
  14. data/lib/swiftype/version.rb +1 -1
  15. data/spec/client_spec.rb +576 -0
  16. data/spec/configuration_spec.rb +36 -0
  17. data/spec/deprecated_spec.rb +20 -0
  18. data/spec/fixtures/vcr/analytics_autoselects.yml +55 -0
  19. data/spec/fixtures/vcr/analytics_autoselects_with_document_type.yml +55 -0
  20. data/spec/fixtures/vcr/analytics_autoselects_with_document_type_and_time_range.yml +55 -0
  21. data/spec/fixtures/vcr/analytics_autoselects_with_time_range.yml +55 -0
  22. data/spec/fixtures/vcr/analytics_clicks.yml +55 -0
  23. data/spec/fixtures/vcr/analytics_clicks_with_document_type.yml +55 -0
  24. data/spec/fixtures/vcr/analytics_clicks_with_document_type_and_time_range.yml +55 -0
  25. data/spec/fixtures/vcr/analytics_clicks_with_time_range.yml +55 -0
  26. data/spec/fixtures/vcr/analytics_searches.yml +55 -0
  27. data/spec/fixtures/vcr/analytics_searches_with_document_type_and_time_range.yml +55 -0
  28. data/spec/fixtures/vcr/analytics_searches_with_time_range.yml +55 -0
  29. data/spec/fixtures/vcr/analytics_searchs_with_document_type.yml +55 -0
  30. data/spec/fixtures/vcr/analytics_top_no_result_queries.yml +55 -0
  31. data/spec/fixtures/vcr/analytics_top_no_result_queries_paginated.yml +55 -0
  32. data/spec/fixtures/vcr/analytics_top_queries.yml +55 -0
  33. data/spec/fixtures/vcr/analytics_top_queries_paginated.yml +55 -0
  34. data/spec/fixtures/vcr/analytics_top_queries_too_large.yml +51 -0
  35. data/spec/fixtures/vcr/bulk_create_documents.yml +49 -0
  36. data/spec/fixtures/vcr/bulk_create_or_update_documents_failure.yml +45 -0
  37. data/spec/fixtures/vcr/bulk_create_or_update_documents_success.yml +49 -0
  38. data/spec/fixtures/vcr/bulk_destroy_documents.yml +45 -0
  39. data/spec/fixtures/vcr/crawl_url.yml +45 -0
  40. data/spec/fixtures/vcr/create_document.yml +48 -0
  41. data/spec/fixtures/vcr/create_document_type.yml +45 -0
  42. data/spec/fixtures/vcr/create_domain.yml +45 -0
  43. data/spec/fixtures/vcr/create_engine.yml +45 -0
  44. data/spec/fixtures/vcr/create_or_update_document_create.yml +47 -0
  45. data/spec/fixtures/vcr/create_or_update_document_update.yml +47 -0
  46. data/spec/fixtures/vcr/create_user.yml +45 -0
  47. data/spec/fixtures/vcr/destroy_document.yml +41 -0
  48. data/spec/fixtures/vcr/destroy_document_type.yml +41 -0
  49. data/spec/fixtures/vcr/destroy_domain.yml +41 -0
  50. data/spec/fixtures/vcr/destroy_engine.yml +41 -0
  51. data/spec/fixtures/vcr/destroy_non_existent_document_type.yml +43 -0
  52. data/spec/fixtures/vcr/document_type_search.yml +60 -0
  53. data/spec/fixtures/vcr/document_type_search_pagination.yml +45 -0
  54. data/spec/fixtures/vcr/document_type_suggest.yml +50 -0
  55. data/spec/fixtures/vcr/document_type_suggest_pagination.yml +45 -0
  56. data/spec/fixtures/vcr/engine_search.yml +64 -0
  57. data/spec/fixtures/vcr/engine_search_facets.yml +162 -0
  58. data/spec/fixtures/vcr/engine_search_pagination.yml +45 -0
  59. data/spec/fixtures/vcr/engine_suggest.yml +52 -0
  60. data/spec/fixtures/vcr/engine_suggest_pagination.yml +45 -0
  61. data/spec/fixtures/vcr/find_document.yml +47 -0
  62. data/spec/fixtures/vcr/find_document_type.yml +45 -0
  63. data/spec/fixtures/vcr/find_domain.yml +45 -0
  64. data/spec/fixtures/vcr/find_domain_failure.yml +43 -0
  65. data/spec/fixtures/vcr/find_engine.yml +45 -0
  66. data/spec/fixtures/vcr/list_document_type.yml +45 -0
  67. data/spec/fixtures/vcr/list_documents.yml +68 -0
  68. data/spec/fixtures/vcr/list_documents_with_pagination.yml +68 -0
  69. data/spec/fixtures/vcr/list_domains.yml +45 -0
  70. data/spec/fixtures/vcr/list_engines.yml +46 -0
  71. data/spec/fixtures/vcr/list_users.yml +45 -0
  72. data/spec/fixtures/vcr/list_users_with_pagination.yml +45 -0
  73. data/spec/fixtures/vcr/log_clickthrough_failure.yml +43 -0
  74. data/spec/fixtures/vcr/log_clickthrough_success.yml +45 -0
  75. data/spec/fixtures/vcr/recrawl_domain_failure.yml +44 -0
  76. data/spec/fixtures/vcr/recrawl_domain_success.yml +45 -0
  77. data/spec/fixtures/vcr/show_user.yml +45 -0
  78. data/spec/fixtures/vcr/update_document.yml +47 -0
  79. data/spec/fixtures/vcr/update_document_unknown_field_failure.yml +45 -0
  80. data/spec/fixtures/vcr/update_documents_failure_non_existent_document.yml +45 -0
  81. data/spec/fixtures/vcr/update_documents_success.yml +45 -0
  82. data/spec/fixtures/vcr/users_client_secret_incorrect.yml +42 -0
  83. data/spec/fixtures/vcr/users_no_api_key.yml +42 -0
  84. data/spec/fixtures/vcr/users_no_client_id_or_secret.yml +43 -0
  85. data/spec/platform_spec.rb +95 -0
  86. data/spec/spec_helper.rb +26 -0
  87. data/spec/ssl_spec.rb +34 -0
  88. data/spec/sso_spec.rb +24 -0
  89. data/swiftype.gemspec +10 -11
  90. metadata +183 -52
  91. data/lib/swiftype/base_model.rb +0 -89
  92. data/lib/swiftype/connection.rb +0 -47
  93. data/lib/swiftype/document.rb +0 -17
  94. data/lib/swiftype/document_type.rb +0 -69
  95. data/lib/swiftype/easy.rb +0 -77
  96. data/lib/swiftype/engine.rb +0 -33
  97. data/lib/swiftype/search.rb +0 -23
data/lib/swiftype.rb CHANGED
@@ -1,37 +1,13 @@
1
- require 'ostruct'
2
- require 'faraday'
3
- require 'faraday_middleware'
4
- require 'swiftype/exceptions'
1
+ require 'swiftype/client'
2
+ require 'swiftype/sso'
5
3
 
6
4
  module Swiftype
7
- autoload :Configuration, 'swiftype/configuration'
8
- autoload :Connection, 'swiftype/connection'
9
- autoload :Client, 'swiftype/client'
10
- autoload :BaseModel, 'swiftype/base_model'
11
- autoload :Request, 'swiftype/request'
12
- autoload :ResultSet, 'swiftype/result_set'
13
- autoload :Search, 'swiftype/search'
14
- autoload :Engine, 'swiftype/engine'
15
- autoload :DocumentType, 'swiftype/document_type'
16
- autoload :Document, 'swiftype/document'
17
- autoload :Easy, 'swiftype/easy'
18
-
5
+ extend Swiftype::Configuration
19
6
 
20
- extend Configuration
21
-
22
- class << self
23
- def new(options={})
24
- Swiftype::Client.new(options)
25
- end
26
-
27
- def method_missing(method, *args, &block)
28
- return super unless new.respond_to?(method)
29
- new.send(method, *args, &block)
30
- end
31
-
32
- def respond_to?(method, include_private=false)
33
- new.respond_to?(method, include_private) || super(method, include_private)
34
- end
7
+ def self.const_missing(const_name)
8
+ super unless const_name == :Easy
9
+ warn "`Swiftype::Easy` has been deprecated. Use `Swiftype::Client` instead."
10
+ Client
35
11
  end
36
12
  end
37
13
 
@@ -1,30 +1,458 @@
1
+ require 'swiftype/configuration'
2
+ require 'swiftype/result_set'
3
+ require 'swiftype/request'
4
+
1
5
  module Swiftype
6
+ # API client for the {Swiftype API}[https://swiftype.com/documentation/overview].
2
7
  class Client
3
- include Swiftype::Connection
8
+ include Swiftype::Request
9
+
10
+ def self.configure(&block)
11
+ warn "`Swiftype::Easy.configure` has been deprecated. Use `Swiftype.configure` instead."
12
+ Swiftype.configure &block
13
+ end
4
14
 
15
+ # Create a new Swiftype::Client client
16
+ #
17
+ # @param options [Hash] a hash of configuration options that will overrided what is set on the Swiftype class.
18
+ # @option options [String] :api_key an API Key to use for this client
19
+ # @option options [String] :platform_access_token a user's access token, will be used instead of API key for authenticating requests
5
20
  def initialize(options={})
21
+ @options = options
22
+ end
23
+
24
+ def api_key
25
+ @options[:api_key] || Swiftype.api_key
26
+ end
27
+
28
+ def platform_access_token
29
+ @options[:platform_access_token]
30
+ end
31
+
32
+ # Methods wrapping the Swiftype private search and API endpoints. Using these methods, you can perform full-text
33
+ # and prefix searches over the Documents in your Engine, in a specific DocumentType, or any subset of DocumentTypes.
34
+ # You can also filter results and get faceted counts for results.
35
+ #
36
+ # For more information, visit the {REST API documentation on searching}[https://swiftype.com/documentation/searching].
37
+ module Search
38
+ # Perform an autocomplete (prefix) search over all the DocumentTypes of the provided engine.
39
+ # This can be used to implement type-ahead autocompletion. However, if your data is not sensitive,
40
+ # you should consider using the {Swiftype public JSONP API}[https://swiftype.com/documentation/public_api]
41
+ # in the user's web browser for suggest queries.
42
+ #
43
+ # results = client.suggest("swiftype-api-example", "gla")
44
+ # results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
45
+ #
46
+ # @param [String] engine_id the Engine slug or ID
47
+ # @param [String] query the search terms
48
+ # @param [Hash] options search options (see {the REST API docs}[https://swiftype.com/documentation/searching] for a complete list)
49
+ # @option options [Integer] :page page number of results to fetch (server defaults to 1)
50
+ # @option options [Integer] :per_page number of results per page (server defaults to 20)
51
+ # @option options [Array] :document_types an array of DocumentType slugs to search.
52
+ # The server defaults to searching all DocumentTypes in the engine. To search a single document type,
53
+ # the +suggest_document_type+ method is more convenient.
54
+ # @option options [Hash] :fetch_fields a Hash of DocumentType slug to array of the fields to return with results
55
+ # (example: <code>{'videos' => ['title', 'channel_id']}</code>)
56
+ # @option options [Hash] :search_fields a Hash of DocumentType slug to array of the fields to search.
57
+ # May contain {field weight boosts}[https://swiftype.com/documentation/searching#field_weights]
58
+ # (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>).
59
+ # The server defaults to searching all +string+ fields for suggest queries.
60
+ # @option options [Hash] :filters a Hash of DocumentType slug to filter definition Hash.
61
+ # See {filters in the REST API documentation}[https://swiftype.com/documentation/searching#filters] for more details
62
+ # (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
63
+ # @option options [Hash] :functional_boosts a Hash of DocumentType slug to {functional boost}[https://swiftype.com/documentation/searching#functional_boosts] definition
64
+ # (example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
65
+ # @option options [Hash] :sort_field a Hash of DocumentType slug to field name to sort on
66
+ # (example: <code>{'videos' => 'view_count'}</code>)
67
+ # @option options [Hash] :sort_direction a Hash of DocumentType slug to direction to sort
68
+ # (example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
69
+ #
70
+ # @return [Swiftype::ResultSet]
71
+ def suggest(engine_id, query, options={})
72
+ search_params = { :q => query }.merge(options)
73
+ response = post("engines/#{engine_id}/suggest.json", search_params)
74
+ ResultSet.new(response)
75
+ end
76
+
77
+ # Perform a full-text search over all the DocumentTypes of the provided engine.
78
+ #
79
+ # results = client.search("swiftype-api-example", "glass")
80
+ # results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
81
+ #
82
+ # @param [String] engine_id the Engine slug or ID
83
+ # @param [String] query the search terms (may be nil)
84
+ # @param [Hash] options search options (see {the REST API docs}[https://swiftype.com/documentation/searching] for a complete list)
85
+ # @option options [Integer] :page page number of results to fetch (server defaults to 1)
86
+ # @option options [Integer] :per_page number of results per page (server defaults to 20)
87
+ # @option options [Array] :document_types an array of DocumentType slugs to search.
88
+ # The server defaults to searching all DocumentTypes in the engine. To search a single document type,
89
+ # the +search_document_type+ method is more convenient.
90
+ # @option options [Hash] :fetch_fields a Hash of DocumentType slug to array of the fields to return with results
91
+ # (example: <code>{'videos' => ['title', 'channel_id']}</code>)
92
+ # @option options [Hash] :search_fields a Hash of DocumentType slug to array of the fields to search.
93
+ # May contain {field weight boosts}[https://swiftype.com/documentation/searching#field_weights]
94
+ # (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>).
95
+ # The server defaults to searching all +string+ and +text+ fields for search queries.
96
+ # @option options [Hash] :filters a Hash of DocumentType slug to filter definition Hash.
97
+ # See {filters in the REST API documentation}[https://swiftype.com/documentation/searching#filters] for more details
98
+ # (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
99
+ # @option options [Hash] :functional_boosts a Hash of DocumentType slug to {functional boost}[https://swiftype.com/documentation/searching#functional_boosts] definition
100
+ # (example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
101
+ # @option options [Hash] :facets a Hash of DocumentType slug to an Array of field names to provide facetted counts for
102
+ # (example: <code>{'videos' => ['category_id', 'channel_id']}</code>)
103
+ # @option options [Hash] :sort_field a Hash of DocumentType slug to field name to sort on
104
+ # (example: <code>{'videos' => 'view_count'}</code>)
105
+ # @option options [Hash] :sort_direction a Hash of DocumentType slug to direction to sort
106
+ # (example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
107
+ #
108
+ # @return [Swiftype::ResultSet]
109
+ def search(engine_id, query, options={})
110
+ search_params = { :q => query }.merge(options)
111
+ response = post("engines/#{engine_id}/search.json", search_params)
112
+ ResultSet.new(response)
113
+ end
114
+
115
+ # Perform an autocomplete (prefix) search over a single DocumentType in an Engine.
116
+ # This can be used to implement type-ahead autocompletion. However, if your data is not sensitive,
117
+ # you should consider using the {Swiftype public JSONP API}[https://swiftype.com/documentation/public_api]
118
+ # in the user's web browser for suggest queries.
119
+ #
120
+ # results = client.suggest_document_type("swiftype-api-example", "videos", "gla")
121
+ # results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
122
+ #
123
+ # @param [String] engine_id the Engine slug or ID
124
+ # @param [String] query the search terms
125
+ # @param [Hash] options search options (see {the REST API docs}[https://swiftype.com/documentation/searching] for a complete list)
126
+ # @option options [Integer] :page page number of results to fetch (server defaults to 1)
127
+ # @option options [Integer] :per_page number of results per page (server defaults to 20)
128
+ # @option options [Array] :document_types an array of DocumentType slugs to search.
129
+ # The server defaults to searching all DocumentTypes in the engine. To search a single document type,
130
+ # the +suggest_document_type+ method is more convenient.
131
+ # @option options [Hash] :fetch_fields a Hash of DocumentType slug to array of the fields to return with results
132
+ # (example: <code>{'videos' => ['title', 'channel_id']}</code>)
133
+ # @option options [Hash] :search_fields a Hash of DocumentType slug to array of the fields to search.
134
+ # May contain {field weight boosts}[https://swiftype.com/documentation/searching#field_weights]
135
+ # (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>).
136
+ # The server defaults to searching all +string+ fields for suggest queries.
137
+ # @option options [Hash] :filters a Hash of DocumentType slug to filter definition Hash.
138
+ # See {filters in the REST API documentation}[https://swiftype.com/documentation/searching#filters] for more details
139
+ # (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
140
+ # @option options [Hash] :functional_boosts a Hash of DocumentType slug to {functional boost}[https://swiftype.com/documentation/searching#functional_boosts] definition
141
+ # (example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
142
+ # @option options [Hash] :sort_field a Hash of DocumentType slug to field name to sort on
143
+ # (example: <code>{'videos' => 'view_count'}</code>)
144
+ # @option options [Hash] :sort_direction a Hash of DocumentType slug to direction to sort
145
+ # (example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
146
+ #
147
+ # @return [Swiftype::ResultSet]
148
+ def suggest_document_type(engine_id, document_type_id, query, options={})
149
+ search_params = { :q => query }.merge(options)
150
+ response = post("engines/#{engine_id}/document_types/#{document_type_id}/suggest.json", search_params)
151
+ ResultSet.new(response)
152
+ end
153
+
154
+ # Perform a full-text search over a single DocumentType in an Engine.
155
+ #
156
+ # results = client.search_document_type("swiftype-api-example", "videos", "glass")
157
+ # results['videos'] # => [{'external_id' => 'v1uyQZNg2vE', 'title' => 'How It Feels [through Glass]', ...}, ...]
158
+ #
159
+ # @param [String] engine_id the Engine slug or ID
160
+ # @param [String] document_type_id the DocumentType slug or ID
161
+ # @param [String] query the search terms (may be nil)
162
+ # @param [Hash] options search options (see {the REST API docs}[https://swiftype.com/documentation/searching] for a complete list)
163
+ # @option options [Integer] :page page number of results to fetch (server defaults to 1)
164
+ # @option options [Integer] :per_page number of results per page (server defaults to 20)
165
+ # @option options [Hash] :fetch_fields a Hash of DocumentType slug to array of the fields to return with results
166
+ # (example: <code>{'videos' => ['title', 'channel_id']}</code>)
167
+ # @option options [Hash] :search_fields a Hash of DocumentType slug to array of the fields to search.
168
+ # May contain {field weight boosts}[https://swiftype.com/documentation/searching#field_weights]
169
+ # (example: <code>{'videos' => ['title^5', 'tags^2', 'caption']}</code>).
170
+ # The server defaults to searching all +string+ and +text+ fields for search queries.
171
+ # @option options [Hash] :filters a Hash of DocumentType slug to filter definition Hash.
172
+ # See {filters in the REST API documentation}[https://swiftype.com/documentation/searching#filters] for more details
173
+ # (example: <code>{'videos' => {'category_id' => ['23', '25']}}</code>)
174
+ # @option options [Hash] :functional_boosts a Hash of DocumentType slug to {functional boost}[https://swiftype.com/documentation/searching#functional_boosts] definition
175
+ # (example: <code>{'videos' => {'view_count' => 'logarithmic'}}</code>).
176
+ # @option options [Hash] :facets a Hash of DocumentType slug to an Array of field names to provide facetted counts for
177
+ # (example: <code>{'videos' => ['category_id', 'channel_id']}</code>)
178
+ # @option options [Hash] :sort_field a Hash of DocumentType slug to field name to sort on
179
+ # (example: <code>{'videos' => 'view_count'}</code>)
180
+ # @option options [Hash] :sort_direction a Hash of DocumentType slug to direction to sort
181
+ # (example: <code>'videos' => 'desc'</code>). Usually used with +:sort_field+.
182
+ #
183
+ # @return [Swiftype::ResultSet]
184
+ def search_document_type(engine_id, document_type_id, query, options={})
185
+ search_params = { :q => query }.merge(options)
186
+ response = post("engines/#{engine_id}/document_types/#{document_type_id}/search.json", search_params)
187
+ ResultSet.new(response)
188
+ end
189
+ end
190
+
191
+ module User
192
+ # List users for the configured application.
193
+ #
194
+ # @param options [Hash]
195
+ # @option options [Integer] :page page number of users to fetch (server defaults to 1)
196
+ # @option options [Integer] :per_page users to return per page (server defaults to 50)
197
+ def users(options={})
198
+ params = {
199
+ :client_id => Swiftype.platform_client_id,
200
+ :client_secret => Swiftype.platform_client_secret
201
+ }
202
+ get("users.json", params.merge(options))
203
+ end
204
+
205
+ # Create a new user for the configured application.
206
+ def create_user
207
+ params = {
208
+ :client_id => Swiftype.platform_client_id,
209
+ :client_secret => Swiftype.platform_client_secret
210
+ }
211
+ post("users.json", params)
212
+ end
213
+
214
+ # Return a user created by the configured application.
215
+ #
216
+ # @param user_id [String] the Swiftype User ID
217
+ def user(user_id)
218
+ params = {
219
+ :client_id => Swiftype.platform_client_id,
220
+ :client_secret => Swiftype.platform_client_secret
221
+ }
222
+ get("users/#{user_id}.json", params)
223
+ end
6
224
  end
7
225
 
226
+ # An Engine is a search engine that lets you search and filter the Documents it contains.
227
+ # For more information, see the {REST API overview}[https://swiftype.com/documentation/overview].
8
228
  module Engine
9
- def engine(id)
10
- Swiftype::Engine.find(id)
229
+ def engines
230
+ get("engines.json")
231
+ end
232
+
233
+ def engine(engine_id)
234
+ get("engines/#{engine_id}.json")
235
+ end
236
+
237
+ def create_engine(name)
238
+ post("engines.json", :engine => {:name => name})
239
+ end
240
+
241
+ def destroy_engine(engine_id)
242
+ delete("engines/#{engine_id}.json")
243
+ end
244
+ end
245
+
246
+ # Every Document must belong to a DocumentType. For more information, see the {REST API overview}[https://swiftype.com/documentation/overview].
247
+ module DocumentType
248
+ def document_types(engine_id)
249
+ get("engines/#{engine_id}/document_types.json")
250
+ end
251
+
252
+ def document_type(engine_id, document_type_id)
253
+ get("engines/#{engine_id}/document_types/#{document_type_id}.json")
254
+ end
255
+
256
+ def create_document_type(engine_id, name)
257
+ post("engines/#{engine_id}/document_types.json", :document_type => {:name => name})
258
+ end
259
+
260
+ def destroy_document_type(engine_id, document_type_id)
261
+ delete("engines/#{engine_id}/document_types/#{document_type_id}.json")
262
+ end
263
+ end
264
+
265
+ # Documents have fields that can be searched or filtered.
266
+ #
267
+ # For more information on indexing documents, see the {REST API indexing documentation}[https://swiftype.com/documentation/indexing].
268
+ module Document
269
+ def documents(engine_id, document_type_id, page=nil, per_page=nil)
270
+ options = {}
271
+ options[:page] = page if page
272
+ options[:per_page] = per_page if per_page
273
+ get("engines/#{engine_id}/document_types/#{document_type_id}/documents.json", options)
274
+ end
275
+
276
+ def document(engine_id, document_type_id, document_id)
277
+ get("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}.json")
278
+ end
279
+
280
+ def create_document(engine_id, document_type_id, document={})
281
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents.json", :document => document)
282
+ end
283
+
284
+ def create_documents(engine_id, document_type_id, documents=[])
285
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_create.json", :documents => documents)
286
+ end
287
+
288
+ def destroy_document(engine_id, document_type_id, document_id)
289
+ delete("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}.json")
290
+ end
291
+
292
+ def destroy_documents(engine_id, document_type_id, document_ids=[])
293
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_destroy.json", :documents => document_ids)
294
+ end
295
+
296
+ def create_or_update_document(engine_id, document_type_id, document={})
297
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents/create_or_update.json", :document => document)
298
+ end
299
+
300
+ def create_or_update_documents(engine_id, document_type_id, documents=[])
301
+ post("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_create_or_update.json", :documents => documents)
302
+ end
303
+
304
+ def update_document(engine_id, document_type_id, document_id, fields)
305
+ put("engines/#{engine_id}/document_types/#{document_type_id}/documents/#{document_id}/update_fields.json", { :fields => fields })
306
+ end
307
+
308
+ def update_documents(engine_id, document_type_id, documents={})
309
+ put("engines/#{engine_id}/document_types/#{document_type_id}/documents/bulk_update.json", { :documents => documents })
310
+ end
311
+ end
312
+
313
+ # The analytics API provides a way to export analytics data similar to what is found in the Swiftype Dashboard.
314
+ # See the {REST API Documentation}[https://swiftype.com/documentation/analytics] for details.
315
+ module Analytics
316
+ # Return the number of searches that occurred on each day in the time range for the provided Engine and optional DocumentType.
317
+ # The maximum time range between start and end dates is 30 days.
318
+ #
319
+ # @param [String] engine_id the Engine slug or ID
320
+ # @param [Hash] options
321
+ # @option options [String] :document_type_id the DocumentType slug or ID
322
+ # @option options [String] :start_date a date formatted like '2013-01-01'
323
+ # @option options [String] :end_date to a date formatted like '2013-01-01'
324
+ def analytics_searches(engine_id, options={})
325
+ document_type_id = options.delete(:document_type_id)
326
+ if document_type_id
327
+ get("engines/#{engine_id}/document_types/#{document_type_id}/analytics/searches.json", options)
328
+ else
329
+ get("engines/#{engine_id}/analytics/searches.json", options)
330
+ end
331
+ end
332
+
333
+ # Return the number of autoselects (when a user clicks a result from an autocomplete dropdown)
334
+ # that occurred on each day in the time range for the provided Engine and optional DocumentType.
335
+ # The maximum time range between start and end dates is 30 days.
336
+ #
337
+ # @param [String] engine_id the Engine slug or ID
338
+ # @param [Hash] options
339
+ # @option options [String] :document_type_id the DocumentType slug or ID
340
+ # @option options [String] :start_date a date formatted like '2013-01-01'
341
+ # @option options [String] :end_date to a date formatted like '2013-01-01'
342
+ def analytics_autoselects(engine_id, options={})
343
+ document_type_id = options.delete(:document_type_id)
344
+ if document_type_id
345
+ get("engines/#{engine_id}/document_types/#{document_type_id}/analytics/autoselects.json", options)
346
+ else
347
+ get("engines/#{engine_id}/analytics/autoselects.json", options)
348
+ end
349
+ end
350
+
351
+ # Return the number of clickthroughs (when a user clicks a result from a search results page)
352
+ # that occurred on each day in the time range for the provided Engine and optional DocumentType.
353
+ # The maximum time range between start and end dates is 30 days.
354
+ #
355
+ # @param [String] engine_id the Engine slug or ID
356
+ # @param [Hash] options
357
+ # @option options [String] :document_type_id the DocumentType slug or ID
358
+ # @option options [String] :start_date a date formatted like '2013-01-01'
359
+ # @option options [String] :end_date to a date formatted like '2013-01-01'
360
+ def analytics_clicks(engine_id, options={})
361
+ document_type_id = options.delete(:document_type_id)
362
+ if document_type_id
363
+ get("engines/#{engine_id}/document_types/#{document_type_id}/analytics/clicks.json", options)
364
+ else
365
+ get("engines/#{engine_id}/analytics/clicks.json", options)
366
+ end
367
+ end
368
+
369
+ # Return top queries for an engine.
370
+ #
371
+ # @param [String] engine_id the engine slug or ID
372
+ # @param [Hash] options
373
+ # @option options [String] :start_date a date formatted like '2013-01-01'
374
+ # @option options [String] :end_date a date formatted like '2013-01-01'
375
+ # @option options [Integer] :page page number. The server defaults to page 1 and the maximum is 50.
376
+ # @option options [Integer] :per_page number of results per page. The server defaults to 20 and the maximum is 100.
377
+ def analytics_top_queries(engine_id, options={})
378
+ get("engines/#{engine_id}/analytics/top_queries.json", options)
379
+ end
380
+
381
+ # Return top queries with no results for an engine.
382
+ #
383
+ # @param [String] engine_id the engine slug or ID
384
+ # @param [Hash] options
385
+ # @option options [String] :start_date a date formatted like '2013-01-01'
386
+ # @option options [String] :end_date a date formatted like '2013-01-01'
387
+ # @option options [Integer] :page page number. The server defaults to page 1 and the maximum is 50.
388
+ # @option options [Integer] :per_page number of results per page. The server defaults to 20 and the maximum is 100.
389
+ def analytics_top_no_result_queries(engine_id, options={})
390
+ get("engines/#{engine_id}/analytics/top_no_result_queries.json", options)
391
+ end
392
+ end
393
+
394
+ # A Domain represents a host in a crawler-based Engine. Domains
395
+ # are only relevant to crawler-base engines, but you can
396
+ # manipulate them through the REST API.
397
+ module Domain
398
+ def domains(engine_id)
399
+ get("engines/#{engine_id}/domains.json")
400
+ end
401
+
402
+ def domain(engine_id, domain_id)
403
+ get("engines/#{engine_id}/domains/#{domain_id}.json")
11
404
  end
12
405
 
13
- def create_engine(attributes)
14
- Swiftype::Engine.new(attributes).create!
406
+ def create_domain(engine_id, url)
407
+ post("engines/#{engine_id}/domains.json", {:domain => {:submitted_url => url}})
15
408
  end
16
409
 
17
- def update_engine(id, attributes)
18
- engine = Swiftype::Engine.find(id)
19
- engine.merge!(attributes)
20
- engine.update!(attributes)
410
+ def destroy_domain(engine_id, domain_id)
411
+ delete("engines/#{engine_id}/domains/#{domain_id}.json")
21
412
  end
22
413
 
23
- def destroy_engine(id)
24
- Swiftype::Engine.find(id).destroy!
414
+ # Trigger a recrawl request for a Domain. Note that this will fail if you have exceeded your recrawl limit.
415
+ def recrawl_domain(engine_id, domain_id)
416
+ put("engines/#{engine_id}/domains/#{domain_id}/recrawl.json")
417
+ end
418
+
419
+ # Request to add or update a URL on a Domain. The host of the URL must match the host of the Domain.
420
+ #
421
+ # @param [String] engine_id the Engine slug or ID
422
+ # @param [String] domain_id the Domain ID
423
+ # @param [String] url the URL to crawl
424
+ def crawl_url(engine_id, domain_id, url)
425
+ put("engines/#{engine_id}/domains/#{domain_id}/crawl_url.json", {:url => url})
426
+ end
427
+ end
428
+
429
+ # A Clickthrough represents a user clicking on a full-text search result.
430
+ #
431
+ # If you are routing searches through your own server instead of
432
+ # executing them client-side with the Swiftype JavaScript API, you
433
+ # will need to record clickthroughs yourself.
434
+ module Clickthrough
435
+ # Log a clickthrough for a Document.
436
+ #
437
+ # @param [String] engine_id the Engine slug or ID
438
+ # @param [String] document_type the DocumentType slug or ID
439
+ # @param [String] q the query that generated the search result
440
+ # @param [String] id the external_id or ID of the Document
441
+ def log_clickthrough(engine_id, document_type, q, id)
442
+ post(
443
+ "engines/#{engine_id}/document_types/#{document_type}/analytics/log_clickthrough.json",
444
+ {:q => q, :id => id}
445
+ )
25
446
  end
26
447
  end
27
448
 
449
+ include Swiftype::Client::User
450
+ include Swiftype::Client::Search
28
451
  include Swiftype::Client::Engine
452
+ include Swiftype::Client::DocumentType
453
+ include Swiftype::Client::Document
454
+ include Swiftype::Client::Analytics
455
+ include Swiftype::Client::Domain
456
+ include Swiftype::Client::Clickthrough
29
457
  end
30
458
  end