connect_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,150 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'connect_client/event'
4
+ require 'connect_client/event_push_response'
5
+
6
+
7
+ describe ConnectClient::EventPushResponse do
8
+ before do
9
+ @json_content_type = 'application/json; charset=utf-8'
10
+ @sample_event = ConnectClient::Event.new({name: 'test'})
11
+ @sample_event_data = @sample_event.data
12
+ end
13
+
14
+ it "should be successful when status code is 201" do
15
+
16
+ body = ''
17
+ code = 201
18
+
19
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
20
+
21
+ event_response.success?.must_equal true
22
+ end
23
+
24
+ it "should pass through string body if content type is not json" do
25
+
26
+ body = ''
27
+ code = 500
28
+
29
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
30
+
31
+ event_response.success?.must_equal false
32
+ end
33
+
34
+ it "should not be successful when status code is 500" do
35
+
36
+ body = ''
37
+ code = 500
38
+
39
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
40
+
41
+ event_response.success?.must_equal false
42
+ end
43
+
44
+ it "should not parse body that is not json content" do
45
+
46
+ content_type = 'text/html'
47
+ body = '<html><html>'
48
+ code = 500
49
+
50
+ event_response = ConnectClient::EventPushResponse.new code, content_type, body, @sample_event
51
+
52
+ event_response.data.must_equal body
53
+ end
54
+
55
+ it "should set status code even if body is not json content" do
56
+
57
+ content_type = 'text/html'
58
+ body = '<html><html>'
59
+ code = 500
60
+
61
+ event_response = ConnectClient::EventPushResponse.new code, content_type, body, @sample_event
62
+
63
+ event_response.http_status_code.must_equal '500'
64
+ end
65
+
66
+
67
+ it "should pass through error message in data" do
68
+
69
+ body = '{ "errorMessage": "something went wrong" }'
70
+ code = 500
71
+
72
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
73
+
74
+ event_response.data[:errorMessage].must_equal 'something went wrong'
75
+ end
76
+
77
+ it "should pass back event in response when successful" do
78
+
79
+ body = ''
80
+ code = 200
81
+
82
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
83
+
84
+ event_response.data[:event].must_be_same_as @sample_event_data
85
+ end
86
+
87
+ it "should pass through original event with error" do
88
+
89
+
90
+ body = '{ "errorMessage": "something went wrong" }'
91
+ code = 500
92
+
93
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, @sample_event
94
+
95
+ event_response.data[:event].must_be_same_as @sample_event_data
96
+ end
97
+
98
+ it "should pass through success status under collection name for batch" do
99
+
100
+
101
+ body = %@
102
+ {
103
+ "collectionName": [{
104
+ "success": true
105
+ }]
106
+ }
107
+ @
108
+ code = 200
109
+
110
+ events = { :collectionName => [@sample_event] }
111
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, events
112
+
113
+ event_response.data[:collectionName][0][:success].must_equal true
114
+ end
115
+
116
+ it "should pass through duplicate status under collection name for batch" do
117
+
118
+
119
+ body = %@
120
+ {
121
+ "collectionName": [{
122
+ "duplicate": true
123
+ }]
124
+ }
125
+ @
126
+ code = 200
127
+
128
+ events = { :collectionName => [@sample_event] }
129
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, events
130
+
131
+ event_response.data[:collectionName][0][:duplicate].must_equal true
132
+ end
133
+
134
+ it "should show event under collection name for batch" do
135
+
136
+ body = %@
137
+ {
138
+ "collectionName": [{
139
+ "success": true
140
+ }]
141
+ }
142
+ @
143
+ code = 200
144
+
145
+ events = { :collectionName => [@sample_event] }
146
+ event_response = ConnectClient::EventPushResponse.new code, @json_content_type, body, events
147
+
148
+ event_response.data[:collectionName][0][:event].must_be_same_as @sample_event_data
149
+ end
150
+ end
@@ -0,0 +1,47 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'connect_client/event'
4
+
5
+ describe ConnectClient::Event do
6
+ it "should throw a validation exception if a property starts with tp_" do
7
+ connect_event = proc { ConnectClient::Event.new({tp_foo: 'bar'}) }.must_raise ConnectClient::EventDataValidationError
8
+ end
9
+
10
+ it "should supply an id if none present" do
11
+ connect_event = ConnectClient::Event.new({foo: 'bar'})
12
+ uuid_regex = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
13
+
14
+ connect_event.data[:id].must_match uuid_regex
15
+ end
16
+
17
+ it "should pass through id supplied" do
18
+ connect_event = ConnectClient::Event.new({id: :bar})
19
+
20
+ connect_event.data[:id].must_equal :bar
21
+ end
22
+
23
+ it "should supply timestamp if none present" do
24
+ iso_date = '2015-07-01T04:07:57Z'
25
+ a_time = Time.parse(iso_date);
26
+
27
+ Time.stub :now, a_time do
28
+ connect_event = ConnectClient::Event.new({foo: 'bar'})
29
+ connect_event.data[:timestamp].must_equal iso_date
30
+ end
31
+ end
32
+
33
+ it "should turn timestamp into iso string" do
34
+ iso_date = '2015-07-01T04:07:57Z'
35
+ a_time = Time.parse(iso_date);
36
+
37
+ connect_event = ConnectClient::Event.new({foo: 'bar', timestamp: a_time})
38
+ connect_event.data[:timestamp].must_equal iso_date
39
+ end
40
+
41
+ it "should pass through timestamp string if supplied" do
42
+ iso_date = '2015-07-01T04:07:57Z'
43
+
44
+ connect_event = ConnectClient::Event.new({foo: 'bar', timestamp: iso_date})
45
+ connect_event.data[:timestamp].must_equal iso_date
46
+ end
47
+ end
@@ -0,0 +1,121 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'webmock/minitest'
4
+ require 'connect_client/http/event_endpoint'
5
+ require 'connect_client/event_push_response'
6
+ require 'connect_client/event'
7
+ require 'connect_client/configuration'
8
+ require 'securerandom'
9
+
10
+ describe ConnectClient::Http::EventEndpoint do
11
+ before do
12
+ @endpoint = ConnectClient::Http::EventEndpoint.new (ConnectClient::Configuration.new)
13
+ @async_endpoint = ConnectClient::Http::EventEndpoint.new (ConnectClient::Configuration.new '', '', true)
14
+ @sample_event_data = { id: SecureRandom.uuid, timestamp: Time.now.utc.iso8601, name: 'sample' }
15
+ @sample_event = ConnectClient::Event.new(@sample_event_data)
16
+ @sample_events_reponse = '{"sample": [{"success": true}]}'
17
+ @sample_collection = 'sample'
18
+ @sample_batch_data = { @sample_collection.to_sym => [@sample_event_data] }
19
+ @sample_batch = { @sample_collection.to_sym => [@sample_event] }
20
+ end
21
+
22
+ it "should get a push response back when pushing a single event to a collection" do
23
+ stub_request(:post, "https://api.getconnect.io/events/#{@sample_collection}").
24
+ with(:body => @sample_event_data, :headers => { 'Accept' => 'application/json' }).
25
+ to_return(:status => 200, :body => "", :headers => { 'Content-Type'=>'application/json' })
26
+
27
+ response = @endpoint.push @sample_collection, @sample_event
28
+
29
+ response.must_be_instance_of ConnectClient::EventPushResponse
30
+ end
31
+
32
+ it "should get push to overriden base_url" do
33
+ overriden_url_config = ConnectClient::Configuration.new '', '', false, 'https://whatever.test'
34
+ endpoint = ConnectClient::Http::EventEndpoint.new overriden_url_config
35
+ stub_request(:post, "https://whatever.test/events/#{@sample_collection}").
36
+ with(:body => @sample_event_data, :headers => { 'Accept' => 'application/json' }).
37
+ to_return(:status => 200, :body => "", :headers => { 'Content-Type'=>'application/json' })
38
+
39
+ response = endpoint.push @sample_collection, @sample_event
40
+
41
+ response.must_be_instance_of ConnectClient::EventPushResponse
42
+ end
43
+
44
+ it "should get push response for a non sucessful error code" do
45
+ overriden_url_config = ConnectClient::Configuration.new '', '', false, 'https://whatever.test'
46
+ endpoint = ConnectClient::Http::EventEndpoint.new overriden_url_config
47
+ stub_request(:post, "https://whatever.test/events/#{@sample_collection}").
48
+ with(:body => @sample_event_data, :headers => { 'Accept' => 'application/json' }).
49
+ to_return(:status => 500, :body => "", :headers => { 'Content-Type'=>'application/json' })
50
+
51
+ response = endpoint.push @sample_collection, @sample_event
52
+
53
+ response.http_status_code.to_s.must_equal '500'
54
+ end
55
+
56
+ it "should get a push response back when pushing batches" do
57
+ stub_request(:post, "https://api.getconnect.io/events").
58
+ with(:body => @sample_batch_data, :headers => { 'Accept' => 'application/json' }).
59
+ to_return(:status => 200, :body => @sample_events_reponse, :headers => { 'Content-Type'=>'application/json' })
60
+
61
+ response = @endpoint.push_batch @sample_batch
62
+ response.must_be_instance_of ConnectClient::EventPushResponse
63
+ end
64
+
65
+ it "should get a push response back when pushing a single event to a collection async" do
66
+ stub_request(:post, "https://api.getconnect.io/events/#{@sample_collection}").
67
+ with(:body => @sample_event_data, :headers => { 'Accept' => 'application/json' }).
68
+ to_return(:status => 200, :body => "", :headers => { 'Content-Type'=>'application/json' })
69
+
70
+ EM.run do
71
+ @async_endpoint.push(@sample_collection, @sample_event).response_received { |response|
72
+ begin
73
+ response.must_be_instance_of ConnectClient::EventPushResponse
74
+ ensure
75
+ EM.stop
76
+ end
77
+ }.error_occured { |error|
78
+ EM.stop
79
+ raise error
80
+ }
81
+ end
82
+ end
83
+
84
+ it "should get push response for a non sucessful error code async" do
85
+ stub_request(:post, "https://api.getconnect.io/events/#{@sample_collection}").
86
+ with(:body => @sample_event_data, :headers => { 'Accept' => 'application/json' }).
87
+ to_return(:status => 500, :body => "", :headers => { 'Content-Type'=>'application/json' })
88
+
89
+ EM.run do
90
+ @async_endpoint.push(@sample_collection, @sample_event).response_received { |response|
91
+ begin
92
+ response.http_status_code.to_s.must_equal '500'
93
+ ensure
94
+ EM.stop
95
+ end
96
+ }.error_occured { |error|
97
+ EM.stop
98
+ raise error
99
+ }
100
+ end
101
+ end
102
+
103
+ it "should get a push response back when pushing batches" do
104
+ stub_request(:post, "https://api.getconnect.io/events").
105
+ with(:body => @sample_batch_data, :headers => { 'Accept' => 'application/json' }).
106
+ to_return(:status => 200, :body => @sample_events_reponse, :headers => { 'Content-Type'=>'application/json' })
107
+
108
+ EM.run do
109
+ @async_endpoint.push_batch(@sample_batch).response_received { |response|
110
+ begin
111
+ response.must_be_instance_of ConnectClient::EventPushResponse
112
+ ensure
113
+ EM.stop
114
+ end
115
+ }.error_occured { |error|
116
+ EM.stop
117
+ raise error
118
+ }
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,60 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'webmock/minitest'
4
+ require 'connect_client/client'
5
+ require 'connect_client/configuration'
6
+ require 'connect_client/event_push_response'
7
+ require 'securerandom'
8
+ require 'em-synchrony'
9
+ require 'em-synchrony/em-http'
10
+
11
+ describe ConnectClient::Http::EventEndpoint, "Test with syncrony defined" do
12
+ before do
13
+ @async_endpoint = ConnectClient::Http::EventEndpoint.new (ConnectClient::Configuration.new '', '', true)
14
+ @sample_event_data = { id: SecureRandom.uuid, timestamp: Time.now.utc.iso8601, name: 'sample' }
15
+ @sample_event = ConnectClient::Event.new(@sample_event_data)
16
+ @sample_events_reponse = '{"sample": [{"success": true}]}'
17
+ @sample_collection = 'sample'
18
+ @sample_batch_data = { @sample_collection.to_sym => [@sample_event_data] }
19
+ @sample_batch = { @sample_collection.to_sym => [@sample_event] }
20
+ end
21
+
22
+ it "should get a push response back when pushing a single event to a collection" do
23
+ stub_request(:post, "https://api.getconnect.io/events/#{@sample_collection}").
24
+ with(:body => @sample_event_data).
25
+ to_return(:status => 200, :body => "", :headers => { 'Content-Type'=>'application/json' })
26
+
27
+ response = nil
28
+ EM.synchrony do
29
+ response = @async_endpoint.push @sample_collection, @sample_event
30
+ EM.stop
31
+ end
32
+ response.must_be_instance_of ConnectClient::EventPushResponse
33
+ end
34
+
35
+ it "should get push response for a non sucessful error code" do
36
+ stub_request(:post, "https://api.getconnect.io/events/#{@sample_collection}").
37
+ with(:body => @sample_event_data).
38
+ to_return(:status => 500, :body => "", :headers => { 'Content-Type'=>'application/json' })
39
+
40
+ response = nil
41
+ EM.synchrony do
42
+ response = @async_endpoint.push @sample_collection, @sample_event
43
+ EM.stop
44
+ end
45
+ response.http_status_code.to_s.must_equal '500'
46
+ end
47
+
48
+ it "should get a push response back when pushing batches" do
49
+ stub_request(:post, "https://api.getconnect.io/events").
50
+ with(:body => @sample_batch_data).
51
+ to_return(:status => 200, :body => @sample_events_reponse, :headers => { 'Content-Type'=>'application/json' })
52
+
53
+ response = nil
54
+ EM.synchrony do
55
+ response = @async_endpoint.push_batch @sample_batch
56
+ EM.stop
57
+ end
58
+ response.must_be_instance_of ConnectClient::EventPushResponse
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'connect_client/configuration'
4
+ require 'connect_client'
5
+
6
+ describe ConnectClient do
7
+ before do
8
+ ConnectClient.reset
9
+ end
10
+
11
+ it "should throw a configuration error configuration has not been called" do
12
+ connect_event = proc { ConnectClient.push 'test', {} }.must_raise ConnectClient::UnconfiguredError
13
+ end
14
+
15
+ it "should respond to push" do
16
+ ConnectClient.configure {}
17
+ ConnectClient.respond_to?(:push).must_equal true
18
+ end
19
+
20
+ it "should support configuration via a block" do
21
+ ConnectClient.configure do |config|
22
+ config.must_be_instance_of ConnectClient::Configuration
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: connect_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Team Connect
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: eventmachine
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: em-http-request
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: em-synchrony
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruby Connect SDK for interacting with the Connect API
98
+ email: team@getconnect.io
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - connect_client.gemspec
109
+ - data/cacert.pem
110
+ - lib/connect_client.rb
111
+ - lib/connect_client/client.rb
112
+ - lib/connect_client/configuration.rb
113
+ - lib/connect_client/event.rb
114
+ - lib/connect_client/event_push_response.rb
115
+ - lib/connect_client/http/event_endpoint.rb
116
+ - lib/connect_client/version.rb
117
+ - spec/connect_client/client_spec.rb
118
+ - spec/connect_client/configuration_spec.rb
119
+ - spec/connect_client/event_push_response_spec.rb
120
+ - spec/connect_client/event_spec.rb
121
+ - spec/connect_client/http/http_event_endpoint_spec.rb
122
+ - spec/connect_client/http/synchrony/event_endpoint_spec.rb
123
+ - spec/connect_client_spec.rb
124
+ homepage: https://github.com/getconnect/connect_client-rb
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.5
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Connect SDK
148
+ test_files:
149
+ - spec/connect_client/client_spec.rb
150
+ - spec/connect_client/configuration_spec.rb
151
+ - spec/connect_client/event_push_response_spec.rb
152
+ - spec/connect_client/event_spec.rb
153
+ - spec/connect_client/http/http_event_endpoint_spec.rb
154
+ - spec/connect_client/http/synchrony/event_endpoint_spec.rb
155
+ - spec/connect_client_spec.rb