api-ai-ruby 1.2.1 → 1.2.2

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
2
  SHA1:
3
- metadata.gz: 8d8eb3cb912b9b4ea97e7cf1e228e222fe55c79b
4
- data.tar.gz: a841d4b046350758e633366b427f5a04c501d63d
3
+ metadata.gz: 522e5912efc69543bb84c18c85e7be17a3858640
4
+ data.tar.gz: bae85fbe0f16379fef9ceeb70a0b2035a85f26d8
5
5
  SHA512:
6
- metadata.gz: a00820e65bed040550cbc68e87219b03a1974d8ddcbf61e216a721879d63d6cf5cc45a4ffdd0cf6ebd742da7a9a5171d45afb02abcc795e397bd343ecbe9fe8c
7
- data.tar.gz: 2ee7eecfa121825412a5de7f9030c02c86986ea7c36dc89cdd7f4fcbf774eca85c5986b3043ea7c69bf745e3654f55a1b0c0095cfc0ef47675d0e091e7b67b79
6
+ metadata.gz: 33292054e5c042f63fa248b53bedd68d2d009e1eeb16d79aae63cf798829f9aeb500b031189a530887c2be37c7827867269270630866a56f7eba1f4cff823628
7
+ data.tar.gz: 1358d1cf73154909c6be7e903828e349fd9bad9342b010344ce92f0d2c825672dd6c867bb4edb86c1a9e23ceff665cc3f043f5dedd4b74ba74020f90a2ae2777
data/README.md CHANGED
@@ -140,6 +140,20 @@ uer.retrieve('contacts') # will return current state of user entity
140
140
  uer.delete('contacts') # will remove user entities for given session
141
141
 
142
142
  ```
143
+ #Timeouts
144
+ **ApiAiRuby::Client** uses the [http gem](https://github.com/httprb/http) under the hood. You can use ```timeout_options``` on the client to set these.
145
+ ```ruby
146
+ ApiAiRuby::Client.new(
147
+ client_access_token: 'YOUR_ACCESS_TOKEN',
148
+ api_lang: 'FR',
149
+ api_base_url: 'http://example.com/v1/',
150
+ api_version: 'YYYYMMDD',
151
+ api_session_id: 'some_uuid_or_whatever',
152
+ timeout_options: [:global, { write: 1, connect: 1, read: 1 }]
153
+ )
154
+ ```
155
+
156
+ Please see the [httprb wiki on timeouts](https://github.com/httprb/http/wiki/Timeouts) for more information.
143
157
 
144
158
 
145
159
  #Error handling
@@ -148,6 +162,7 @@ uer.delete('contacts') # will remove user entities for given session
148
162
 
149
163
  #Changelog
150
164
 
165
+ * 1.2.2 - added configurable timeouts for requests (thanks [bramski](https://github.com/bramski))
151
166
  * 1.2.1 - fixed UTF-8 in text-requests
152
167
  * 1.2.0 - added configurable session_id and full userEntities support
153
168
  * 1.1.4 - removed unused dependency and updated default API version
@@ -2,7 +2,7 @@ require 'securerandom'
2
2
 
3
3
  module ApiAiRuby
4
4
  class Client
5
- attr_accessor :client_access_token, :subscription_key
5
+ attr_accessor :client_access_token, :subscription_key, :timeout_options
6
6
  attr_writer :user_agent, :api_version, :api_lang, :api_base_url, :api_session_id
7
7
 
8
8
  # Initializes a new Client object
@@ -1,6 +1,6 @@
1
1
  module ApiAiRuby
2
2
  class Constants
3
- VERSION = '1.2.1'
3
+ VERSION = '1.2.2'
4
4
  DEFAULT_BASE_URL = 'https://api.api.ai/v1/'
5
5
  DEFAULT_API_VERSION = '20150910'
6
6
  DEFAULT_CLIENT_LANG = 'en'
@@ -3,7 +3,7 @@ require 'http'
3
3
  module ApiAiRuby
4
4
  class RequestQuery
5
5
 
6
- attr_accessor :client, :headers, :options, :request_method, :uri
6
+ attr_accessor :client, :headers, :options, :request_method, :uri
7
7
 
8
8
  # @param client [ApiAiRuby::Client]
9
9
  # @param options [Hash]
@@ -18,20 +18,21 @@ module ApiAiRuby
18
18
  @headers = {
19
19
  Authorization: 'Bearer ' + client.client_access_token,
20
20
  }
21
+ @timeout_options = client.timeout_options || options[:timeout_options]
21
22
  end
22
23
 
23
24
  # @return [Array, Hash]
24
25
  def perform
25
-
26
26
  if @options.has_key?(:voiceData)
27
27
  options_key = :form
28
28
  else
29
29
  options_key = (@request_method === :get) ? :params : :json
30
30
  end
31
31
 
32
- response = HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
32
+ request = HTTP.with(@headers)
33
+ request = request.timeout(*@timeout_options) if @timeout_options
34
+ response = request.public_send(@request_method, @uri.to_s, options_key => @options)
33
35
  response_body = symbolize_keys!(response.parse)
34
- response_headers = response.headers
35
36
  fail_or_return_response_body(response.code, response_body)
36
37
  end
37
38
 
@@ -48,13 +48,15 @@ describe ApiAiRuby::Client do
48
48
  api_lang: 'RU',
49
49
  api_base_url: 'http://localhost',
50
50
  api_version: '1234',
51
- api_session_id: '555'
51
+ api_session_id: '555',
52
+ timeout_options: [:global, { write: 1, connect: 1, read: 1}]
52
53
  )
53
54
 
54
55
  expect(client.api_base_url).to eq 'http://localhost'
55
56
  expect(client.api_version).to eq '1234'
56
57
  expect(client.api_lang).to eq 'RU'
57
58
  expect(client.api_session_id).to eq '555'
59
+ expect(client.timeout_options).to eq [:global, { write: 1, connect: 1, read: 1}]
58
60
  end
59
61
 
60
62
 
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+
3
+ describe ApiAiRuby::TextRequest do
4
+ let(:client) do
5
+ ApiAiRuby::Client.new(
6
+ client_access_token: 'CS',
7
+ api_lang: 'EN',
8
+ api_session_id: '555',
9
+ timeout_options: [:global, { write: 1, connect: 1, read: 1 }]
10
+ )
11
+ end
12
+
13
+ subject(:text_request) { described_class.new(client, options) }
14
+ let(:options) { { query: "hello" } }
15
+
16
+ around do |test|
17
+ begin
18
+ WebMock.disable_net_connect!(allow: 'coveralls.io')
19
+ test.run
20
+ ensure
21
+ WebMock.allow_net_connect!
22
+ end
23
+ end
24
+
25
+ let(:response_headers) do
26
+ { "Content-Type" => "application/json;charset=UTF-8", "Content-Length" => "437", "Connection" => "close", "Access-Control-Allow-Credentials" => "true", "Cache-Control" => "no-cache=\"set-cookie\"", "Date" => "Wed, 12 Oct 2016 20:07:54 GMT", "Server" => "nginx/1.9.7", "Set-Cookie" => "AWSELB=9D5B4D210CCFFAF1BE1E0CD7C7E6FCBD7B46140CAAF64A202A005B9079598B549F7A5EC269DD0FF88508DA57410EFC7882B7860453691E7ACC870186C9D1589D2A332B51EC;PATH=/", "X-Cache" => "Miss from cloudfront", "Via" => "1.1 978198446b6fdba8a499c04f84a3a7e6.cloudfront.net (CloudFront)", "X-Amz-Cf-Id" => "ilwhpG75Ea4iXumklw7484nYt2jbx-L6ZaeiO9naUOstx45ia_nuaQ==" }
27
+ end
28
+ let(:body) do
29
+ { :id => "94613973-930f-4a53-9286-6e9efcbb5c57", :timestamp => "2016-10-12T20:07:54.876Z", :result => { :source => "domains", :resolvedQuery => "hello", :action => "smalltalk.greetings", :parameters => { :simplified => "hello" }, :metadata => {}, :fulfillment => { :speech => "Good day!" }, :score => 0.0 }, :status => { :code => 200, :errorType => "success" }, :sessionId => "555" }
30
+ end
31
+ let(:expected_headers) do
32
+ { 'Authorization' => 'Bearer CS', 'Connection' => 'close', 'Content-Type' => 'application/json; charset=UTF-8', 'Host' => 'api.api.ai' }
33
+ end
34
+ let(:expected_url) { "https://api.api.ai/v1/query?v=20150910" }
35
+ let(:expected_body) { '{"query":"hello","lang":"EN","sessionId":"555"}' }
36
+
37
+ describe "#perform" do
38
+ it "performs a request" do
39
+ stub = stub_request(:post, expected_url).
40
+ with(:body => expected_body,
41
+ :headers => expected_headers).
42
+ to_return(:status => 200, :body => body.to_json, :headers => response_headers)
43
+ subject.perform
44
+ expect(stub).to have_been_requested
45
+ end
46
+
47
+ context "it times out" do
48
+ it "times out properly" do
49
+ stub = stub_request(:post, expected_url).
50
+ with(:body => expected_body,
51
+ :headers => expected_headers).to_timeout
52
+ expect {
53
+ subject.perform
54
+ }.to raise_error(Errno::ETIMEDOUT)
55
+ expect(stub).to have_been_requested
56
+ end
57
+ end
58
+ end
59
+ end
@@ -12,9 +12,10 @@ end
12
12
 
13
13
  require 'api-ai-ruby'
14
14
  require 'rspec'
15
- #require 'webmock/rspec'
15
+ require 'webmock'
16
+ require 'webmock/rspec'
16
17
 
17
- #WebMock.disable_net_connect!(allow: 'coveralls.io')
18
+ WebMock.allow_net_connect!
18
19
 
19
20
  RSpec.configure do |config|
20
21
  config.expect_with :rspec do |c|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-ai-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - api.ai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler