swiftype-app-search 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fd624bd0145b983734fdd65725ad222fb07752b5
4
- data.tar.gz: d02e4f1e25f9e6a8d06eb83f33bc38b5aa132ec5
2
+ SHA256:
3
+ metadata.gz: 1986746b954002b52e3ddc39165f4b9ffd6e97004f91ab467e078ce93e61c97f
4
+ data.tar.gz: f7f082f36f27c87b9fc9db7f2de3ece9200ee11635a93116919b7c9e72abb644
5
5
  SHA512:
6
- metadata.gz: 5afb7ec9fc723e2fceaaa629d3dc948857ad3f4546db3ea67b659870f4aac7115d1185a7927194d2e18d4697e2026f026697b061c8c5a13f7bd94ca4a49577ea
7
- data.tar.gz: 7d88608afa49080ad259930b299d2d78774a3188a26aadeb6ea9db58af94d45c8581df8329e9d54314918107578247554ed6dbd1f6f88f863b376f8f1ed5b2d8
6
+ metadata.gz: 1452ee050399d1e34f5febf9a33893bc990c4846a56e56d00e4b31f207673725f9015b4a67134f22d6ff5172830ca6e96662f421950540499ebef55678218562
7
+ data.tar.gz: 1338b25a69535c36561e0197ea0befdbacaa80cb8955f9e0eaa7bf01b2687a329fe572863e9dd70af607a3aff421dedbc3a86317d166c94873703dc5d2cfa92c
data/README.md CHANGED
@@ -22,7 +22,13 @@ It also requires a valid `API_KEY`, which authenticates requests to the API:
22
22
  client = SwiftypeAppSearch::Client.new(:account_host_key => 'host-c5s2mj', :api_key => 'api-mu75psc5egt9ppzuycnc2mc3')
23
23
  ```
24
24
 
25
- ### Indexing Creating or Updating a Single Document
25
+ ### API Methods
26
+
27
+ This client is a thin interface to the Swiftype App Search Api. Additional details for requests and responses can be
28
+ found in the [documentation](https://swiftype.com/documentation/app-search).
29
+
30
+ #### Indexing: Creating or Updating a Single Document
31
+
26
32
  ```ruby
27
33
  engine_name = 'favorite-videos'
28
34
  document = {
@@ -33,13 +39,14 @@ document = {
33
39
  }
34
40
 
35
41
  begin
36
- client.index_document(engine_name, document)
42
+ response = client.index_document(engine_name, document)
43
+ puts response
37
44
  rescue SwiftypeAppSearch::ClientException => e
38
- # handle error
45
+ puts e
39
46
  end
40
47
  ```
41
48
 
42
- ### Indexing: Creating or Updating Documents
49
+ #### Indexing: Creating or Updating Documents
43
50
 
44
51
  ```ruby
45
52
  engine_name = 'favorite-videos'
@@ -59,42 +66,92 @@ documents = [
59
66
  ]
60
67
 
61
68
  begin
62
- index_document_results = client.index_documents(engine_name, documents)
63
- # handle index document results
69
+ response = client.index_documents(engine_name, documents)
70
+ puts response
64
71
  rescue SwiftypeAppSearch::ClientException => e
65
- # handle error
72
+ puts e
66
73
  end
67
74
  ```
68
75
 
69
- ### Listing Documents
76
+ #### Listing Documents
70
77
 
71
78
  ```ruby
72
79
  engine_name = 'favorite-videos'
73
80
  document_ids = ['INscMGmhmX4', 'JNDFojsd02']
74
81
 
75
82
  begin
76
- document_contents = client.get_documents(engine_name, document_ids)
77
- # handle document contents
83
+ response = client.get_documents(engine_name, document_ids)
84
+ puts response
78
85
  rescue SwiftypeAppSearch::ClientException => e
79
- # handle error
86
+ puts e
80
87
  end
81
88
  ```
82
89
 
83
- ### Destroying Documents
90
+ #### Destroying Documents
84
91
 
85
92
  ```ruby
86
93
  engine_name = 'favorite-videos'
87
94
  document_ids = ['INscMGmhmX4', 'JNDFojsd02']
88
95
 
89
96
  begin
90
- destroy_document_results = client.destroy_documents(engine_name, document_ids)
91
- # handle destroy document results
97
+ response = client.destroy_documents(engine_name, document_ids)
98
+ puts response
99
+ rescue SwiftypeAppSearch::ClientException => e
100
+ puts e
101
+ end
102
+ ```
103
+
104
+ #### Listing Engines
105
+
106
+ ```ruby
107
+ begin
108
+ response = client.list_engines
109
+ puts response
110
+ rescue SwiftypeAppSearch::ClientException => e
111
+ puts e
112
+ end
113
+ ```
114
+
115
+ #### Retrieving Engines
116
+
117
+ ```ruby
118
+ engine_name = 'favorite-videos'
119
+
120
+ begin
121
+ response = client.get_engine(engine_name)
122
+ puts response
92
123
  rescue SwiftypeAppSearch::ClientException => e
93
- # handle error
124
+ puts e
94
125
  end
95
126
  ```
96
127
 
97
- ### Searching
128
+ #### Creating Engines
129
+
130
+ ```ruby
131
+ engine_name = 'favorite-videos'
132
+
133
+ begin
134
+ response = client.create_engine(engine_name)
135
+ puts response
136
+ rescue SwiftypeAppSearch::ClientException => e
137
+ puts e
138
+ end
139
+ ```
140
+
141
+ #### Destroying Engines
142
+
143
+ ```ruby
144
+ engine_name = 'favorite-videos'
145
+
146
+ begin
147
+ response = client.destroy_engine(engine_name)
148
+ puts response
149
+ rescue SwiftypeAppSearch::ClientException => e
150
+ puts e
151
+ end
152
+ ```
153
+
154
+ #### Searching
98
155
 
99
156
  ```ruby
100
157
  engine_name = 'favorite-videos'
@@ -104,20 +161,27 @@ result_fields = { :title => { :raw => {} } }
104
161
  options = { :search_fields => search_fields, :result_fields => result_fields }
105
162
 
106
163
  begin
107
- search_results = client.search(engine_name, query, options)
108
- # handle search results
164
+ response = client.search(engine_name, query, options)
165
+ puts response
109
166
  rescue SwiftypeAppSearch::ClientException => e
110
- # handle error
167
+ puts e
111
168
  end
112
169
  ```
113
170
 
114
-
115
171
  ## Running Tests
116
172
 
117
173
  ```bash
118
174
  export AS_API_KEY="your API key"
119
175
  export AS_ACCOUNT_HOST_KEY="your account host key"
120
- rspec
176
+ bundle exec rspec
177
+ ```
178
+
179
+ You can also run tests against a local environment by passing a `AS_API_ENDPOINT` environment variable
180
+
181
+ ```bash
182
+ export AS_API_KEY="your API key"
183
+ export AS_API_ENDPOINT="http://<your account host key>.api.127.0.0.1.ip.es.io:3002/api/as/v1"
184
+ bundle exec rspec
121
185
  ```
122
186
 
123
187
  ## Debugging API calls
@@ -135,4 +199,4 @@ If you need to debug an API call made by the client, there are a few things you
135
199
 
136
200
  ## Contributions
137
201
 
138
- To contribute code to this gem, please fork the repository and submit a pull request.
202
+ To contribute code, please fork the repository and submit a pull request.
@@ -1,5 +1,6 @@
1
1
  require 'net/https'
2
2
  require 'json'
3
+ require 'time'
3
4
  require 'swiftype-app-search/exceptions'
4
5
  require 'swiftype-app-search/version'
5
6
  require 'openssl'
@@ -46,7 +47,10 @@ module SwiftypeAppSearch
46
47
 
47
48
  if uri.scheme == 'https'
48
49
  http.use_ssl = true
49
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
50
+ # st_ssl_verify_none provides a means to disable SSL verification for debugging purposes. An example
51
+ # is Charles, which uses a self-signed certificate in order to inspect https traffic. This will
52
+ # not be part of this client's public API, this is more of a development enablement option
53
+ http.verify_mode = ENV['st_ssl_verify_none'] == 'true' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
50
54
  http.ca_file = File.join(File.dirname(__FILE__), '..', 'data', 'ca-bundle.crt')
51
55
  http.ssl_timeout = open_timeout
52
56
  end
@@ -86,6 +90,32 @@ module SwiftypeAppSearch
86
90
  @debug ||= (ENV['AS_DEBUG'] == 'true')
87
91
  end
88
92
 
93
+ def serialize_json(object)
94
+ JSON.generate(clean_json(object))
95
+ end
96
+
97
+ def clean_json(object)
98
+ case object
99
+ when Hash
100
+ object.inject({}) do |builder, (key, value)|
101
+ builder[key] = clean_json(value)
102
+ builder
103
+ end
104
+ when Enumerable
105
+ object.map { |value| clean_json(value) }
106
+ else
107
+ clean_atom(object)
108
+ end
109
+ end
110
+
111
+ def clean_atom(atom)
112
+ if atom.is_a?(Time)
113
+ atom.to_datetime
114
+ else
115
+ atom
116
+ end
117
+ end
118
+
89
119
  def build_request(method, uri, params)
90
120
  klass = case method
91
121
  when :get
@@ -101,7 +131,7 @@ module SwiftypeAppSearch
101
131
  end
102
132
 
103
133
  req = klass.new(uri.request_uri)
104
- req.body = JSON.generate(params) unless params.length == 0
134
+ req.body = serialize_json(params) unless params.length == 0
105
135
 
106
136
  req['User-Agent'] = DEFAULT_USER_AGENT
107
137
  req['Content-Type'] = 'application/json'
@@ -1,3 +1,3 @@
1
1
  module SwiftypeAppSearch
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -2,7 +2,7 @@ describe SwiftypeAppSearch::Client do
2
2
  let(:engine_name) { "ruby-client-test-#{Time.now.to_i}" }
3
3
 
4
4
  include_context "App Search Credentials"
5
- let(:client) { SwiftypeAppSearch::Client.new(:account_host_key => as_account_host_key, :api_key => as_api_key) }
5
+ let(:client) { SwiftypeAppSearch::Client.new(client_options) }
6
6
 
7
7
  context 'Documents' do
8
8
  let(:document) { { 'url' => 'http://www.youtube.com/watch?v=v1uyQZNg2vE' } }
@@ -40,6 +40,23 @@ describe SwiftypeAppSearch::Client do
40
40
  end.to raise_error(SwiftypeAppSearch::InvalidDocument, /Invalid field/)
41
41
  end
42
42
  end
43
+
44
+ context 'when a document has a Ruby Time object' do
45
+ let(:time_rfc3339) { '2018-01-01T01:01:01+00:00' }
46
+ let(:time_object) { Time.parse(time_rfc3339) }
47
+ let(:document) { { 'created_at' => time_object } }
48
+
49
+ it 'should serialize the time object in RFC 3339' do
50
+ response = subject
51
+ expect(response).to have_key('id')
52
+ document_id = response.fetch('id')
53
+ expect do
54
+ documents = client.get_documents(engine_name, [document_id])
55
+ expect(documents.size).to eq(1)
56
+ expect(documents.first['created_at']).to eq(time_rfc3339)
57
+ end.to_not raise_error
58
+ end
59
+ end
43
60
  end
44
61
 
45
62
  describe '#index_documents' do
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,15 @@ require 'swiftype-app-search'
7
7
  RSpec.shared_context "App Search Credentials" do
8
8
  let(:as_api_key) { ENV.fetch('AS_API_KEY', 'API_KEY') }
9
9
  let(:as_account_host_key) { ENV.fetch('AS_ACCOUNT_HOST_KEY', 'ACCOUNT_HOST_KEY') }
10
+ let(:as_api_endpoint) { ENV.fetch('AS_API_ENDPOINT', nil) }
11
+ let(:client_options) do
12
+ {
13
+ :api_key => as_api_key,
14
+ :account_host_key => as_account_host_key
15
+ }.tap do |opts|
16
+ opts[:api_endpoint] = as_api_endpoint unless as_api_endpoint.nil?
17
+ end
18
+ end
10
19
  end
11
20
 
12
21
  RSpec.configure do |config|
@@ -16,8 +16,9 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_development_dependency 'rspec', '~> 3.0'
20
19
  s.add_development_dependency 'awesome_print', '~> 1.8'
20
+ s.add_development_dependency 'pry', '~> 0.11.3'
21
+ s.add_development_dependency 'rspec', '~> 3.0'
21
22
  s.add_development_dependency 'webmock', '~> 3.3'
22
23
 
23
24
  s.add_runtime_dependency 'jwt', '~> 2.1'
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swiftype-app-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-05-16 00:00:00.000000000 Z
11
+ date: 2018-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: awesome_print
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '1.8'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.0'
26
+ version: '1.8'
27
27
  - !ruby/object:Gem::Dependency
28
- name: awesome_print
28
+ name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.8'
33
+ version: 0.11.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.8'
40
+ version: 0.11.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: webmock
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -114,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
128
  version: '0'
115
129
  requirements: []
116
130
  rubyforge_project:
117
- rubygems_version: 2.4.5
131
+ rubygems_version: 2.7.6
118
132
  signing_key:
119
133
  specification_version: 4
120
134
  summary: Official gem for accessing the Swiftype App Search API