connect_client 0.2.2 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +39 -38
- data/Gemfile +2 -2
- data/LICENSE +22 -22
- data/README.md +45 -45
- data/Rakefile +21 -21
- data/connect_client.gemspec +24 -24
- data/data/cacert.pem +3988 -3988
- data/lib/connect_client.rb +48 -40
- data/lib/connect_client/client.rb +42 -42
- data/lib/connect_client/configuration.rb +11 -11
- data/lib/connect_client/event.rb +72 -72
- data/lib/connect_client/event_push_response.rb +48 -48
- data/lib/connect_client/http/deferred_http_response.rb +8 -8
- data/lib/connect_client/http/event_endpoint.rb +135 -131
- data/lib/connect_client/security/filtered_key_generation.rb +41 -0
- data/lib/connect_client/version.rb +1 -1
- data/spec/connect_client/client_spec.rb +68 -68
- data/spec/connect_client/configuration_spec.rb +54 -54
- data/spec/connect_client/event_push_response_spec.rb +149 -149
- data/spec/connect_client/event_spec.rb +93 -93
- data/spec/connect_client/http/http_event_endpoint_spec.rb +124 -124
- data/spec/connect_client/http/synchrony/{event_endpoint_spec.rb → http_event_endpoint_spec.rb} +59 -59
- data/spec/connect_client/security/filtered_key_generation_spec.rb +13 -0
- data/spec/connect_client_spec.rb +24 -24
- metadata +7 -4
data/lib/connect_client.rb
CHANGED
@@ -1,41 +1,49 @@
|
|
1
|
-
require_relative 'connect_client/client'
|
2
|
-
require_relative 'connect_client/configuration'
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@client =
|
16
|
-
end
|
17
|
-
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
@client
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
1
|
+
require_relative 'connect_client/client'
|
2
|
+
require_relative 'connect_client/configuration'
|
3
|
+
require_relative 'connect_client/security/filtered_key_generation.rb'
|
4
|
+
|
5
|
+
module ConnectClient
|
6
|
+
class << self
|
7
|
+
def gem_root
|
8
|
+
File.expand_path '../..', __FILE__
|
9
|
+
end
|
10
|
+
|
11
|
+
def configure
|
12
|
+
config = Configuration.new
|
13
|
+
yield(config)
|
14
|
+
|
15
|
+
@client = ConnectClient::Client.new config
|
16
|
+
end
|
17
|
+
|
18
|
+
def reset
|
19
|
+
@client = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_filtered_key(key_json, master_key)
|
23
|
+
ConnectClient::Security.generate_filtered_key key_json, master_key
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method, *args, &block)
|
27
|
+
return super unless client.respond_to?(method)
|
28
|
+
client.send(method, *args, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to?(method)
|
32
|
+
return (!@client.nil? && @client.respond_to?(method)) || super
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def client
|
38
|
+
raise UnconfiguredError if @client.nil?
|
39
|
+
|
40
|
+
@client
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class UnconfiguredError < StandardError
|
45
|
+
def message
|
46
|
+
"Connect must configured before it can be used, please call Connect.configure"
|
47
|
+
end
|
48
|
+
end
|
41
49
|
end
|
@@ -1,43 +1,43 @@
|
|
1
|
-
require_relative 'http/event_endpoint'
|
2
|
-
require_relative 'event'
|
3
|
-
|
4
|
-
module ConnectClient
|
5
|
-
class Client
|
6
|
-
|
7
|
-
def initialize(config)
|
8
|
-
@end_point = ConnectClient::Http::EventEndpoint.new config
|
9
|
-
end
|
10
|
-
|
11
|
-
def push(collection_name_or_batches, event_or_events = nil)
|
12
|
-
has_multiple_events = event_or_events.is_a?(Array)
|
13
|
-
has_collection_name = collection_name_or_batches.is_a?(String) || collection_name_or_batches.is_a?(Symbol)
|
14
|
-
is_batch = !has_collection_name || has_multiple_events
|
15
|
-
|
16
|
-
if is_batch
|
17
|
-
batch = create_batch(has_collection_name, collection_name_or_batches, event_or_events)
|
18
|
-
@end_point.push_batch batch
|
19
|
-
else
|
20
|
-
@end_point.push(collection_name_or_batches, ConnectClient::Event.new(event_or_events))
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def create_batch(has_collection_name, collection_name_or_batches, event_or_events)
|
27
|
-
|
28
|
-
batches = has_collection_name ?
|
29
|
-
{ collection_name_or_batches.to_sym => event_or_events } :
|
30
|
-
collection_name_or_batches
|
31
|
-
|
32
|
-
create_event = Proc.new do |event_data|
|
33
|
-
ConnectClient::Event.new event_data
|
34
|
-
end
|
35
|
-
|
36
|
-
map_all_events = Proc.new do |col_name, events|
|
37
|
-
[col_name, events.map(&create_event)]
|
38
|
-
end
|
39
|
-
|
40
|
-
Hash[batches.map(&map_all_events)]
|
41
|
-
end
|
42
|
-
end
|
1
|
+
require_relative 'http/event_endpoint'
|
2
|
+
require_relative 'event'
|
3
|
+
|
4
|
+
module ConnectClient
|
5
|
+
class Client
|
6
|
+
|
7
|
+
def initialize(config)
|
8
|
+
@end_point = ConnectClient::Http::EventEndpoint.new config
|
9
|
+
end
|
10
|
+
|
11
|
+
def push(collection_name_or_batches, event_or_events = nil)
|
12
|
+
has_multiple_events = event_or_events.is_a?(Array)
|
13
|
+
has_collection_name = collection_name_or_batches.is_a?(String) || collection_name_or_batches.is_a?(Symbol)
|
14
|
+
is_batch = !has_collection_name || has_multiple_events
|
15
|
+
|
16
|
+
if is_batch
|
17
|
+
batch = create_batch(has_collection_name, collection_name_or_batches, event_or_events)
|
18
|
+
@end_point.push_batch batch
|
19
|
+
else
|
20
|
+
@end_point.push(collection_name_or_batches, ConnectClient::Event.new(event_or_events))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def create_batch(has_collection_name, collection_name_or_batches, event_or_events)
|
27
|
+
|
28
|
+
batches = has_collection_name ?
|
29
|
+
{ collection_name_or_batches.to_sym => event_or_events } :
|
30
|
+
collection_name_or_batches
|
31
|
+
|
32
|
+
create_event = Proc.new do |event_data|
|
33
|
+
ConnectClient::Event.new event_data
|
34
|
+
end
|
35
|
+
|
36
|
+
map_all_events = Proc.new do |col_name, events|
|
37
|
+
[col_name, events.map(&create_event)]
|
38
|
+
end
|
39
|
+
|
40
|
+
Hash[batches.map(&map_all_events)]
|
41
|
+
end
|
42
|
+
end
|
43
43
|
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
module ConnectClient
|
2
|
-
class Configuration
|
3
|
-
attr_accessor :base_url, :api_key, :project_id, :async
|
4
|
-
|
5
|
-
def initialize(api_key = '', project_id = '', async = false, base_url = 'https://api.getconnect.io')
|
6
|
-
@base_url = base_url
|
7
|
-
@api_key = api_key
|
8
|
-
@project_id = project_id
|
9
|
-
@async = async
|
10
|
-
end
|
11
|
-
end
|
1
|
+
module ConnectClient
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :base_url, :api_key, :project_id, :async
|
4
|
+
|
5
|
+
def initialize(api_key = '', project_id = '', async = false, base_url = 'https://api.getconnect.io')
|
6
|
+
@base_url = base_url
|
7
|
+
@api_key = api_key
|
8
|
+
@project_id = project_id
|
9
|
+
@async = async
|
10
|
+
end
|
11
|
+
end
|
12
12
|
end
|
data/lib/connect_client/event.rb
CHANGED
@@ -1,73 +1,73 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
require 'json'
|
3
|
-
require 'time'
|
4
|
-
|
5
|
-
module ConnectClient
|
6
|
-
class Event
|
7
|
-
|
8
|
-
@@RESERVED_PROPERTY_REGEX = /tp_.+/i
|
9
|
-
|
10
|
-
attr_reader :data
|
11
|
-
|
12
|
-
def initialize(data)
|
13
|
-
event_data_defaults = { id: SecureRandom.uuid, timestamp: Time.now }
|
14
|
-
@data = map_iso_dates event_data_defaults.merge(data)
|
15
|
-
validate
|
16
|
-
end
|
17
|
-
|
18
|
-
def validate
|
19
|
-
invalid_properties = @data.keys.grep(@@RESERVED_PROPERTY_REGEX)
|
20
|
-
|
21
|
-
raise EventDataValidationError.new(invalid_properties) if invalid_properties.any?
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_json(options = nil)
|
25
|
-
@data.to_json
|
26
|
-
end
|
27
|
-
|
28
|
-
def to_s
|
29
|
-
"Event Data: #{@data}"
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def map_iso_dates(data)
|
35
|
-
utc_converter = lambda { |value|
|
36
|
-
value_to_convert = value
|
37
|
-
map_utc = lambda { |value_item| utc_converter.call(value_item) }
|
38
|
-
|
39
|
-
return value_to_convert.map(&map_utc) if value_to_convert.respond_to? :map
|
40
|
-
|
41
|
-
value_to_convert = value_to_convert.to_time if value_to_convert.is_a?(DateTime) || value_to_convert.is_a?(Date)
|
42
|
-
value_to_convert = value_to_convert.utc.iso8601 if value_to_convert.is_a? Time
|
43
|
-
|
44
|
-
value_to_convert
|
45
|
-
}
|
46
|
-
|
47
|
-
mappedData = data.map do |key, value|
|
48
|
-
if value.is_a? Hash
|
49
|
-
[key, map_iso_dates(value)]
|
50
|
-
else
|
51
|
-
[key, utc_converter.call(value)]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
Hash[mappedData]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class EventDataValidationError < StandardError
|
60
|
-
attr_reader :invalid_property_names
|
61
|
-
|
62
|
-
def initialize(invalid_property_names)
|
63
|
-
@invalid_property_names = invalid_property_names
|
64
|
-
end
|
65
|
-
|
66
|
-
def message
|
67
|
-
messages = ['The following properties use the reserved prefix tp_:'] + @invalid_property_names.map do |property_name|
|
68
|
-
"->#{property_name}"
|
69
|
-
end
|
70
|
-
messages.join "\n"
|
71
|
-
end
|
72
|
-
end
|
1
|
+
require 'securerandom'
|
2
|
+
require 'json'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module ConnectClient
|
6
|
+
class Event
|
7
|
+
|
8
|
+
@@RESERVED_PROPERTY_REGEX = /tp_.+/i
|
9
|
+
|
10
|
+
attr_reader :data
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
event_data_defaults = { id: SecureRandom.uuid, timestamp: Time.now }
|
14
|
+
@data = map_iso_dates event_data_defaults.merge(data)
|
15
|
+
validate
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate
|
19
|
+
invalid_properties = @data.keys.grep(@@RESERVED_PROPERTY_REGEX)
|
20
|
+
|
21
|
+
raise EventDataValidationError.new(invalid_properties) if invalid_properties.any?
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_json(options = nil)
|
25
|
+
@data.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
"Event Data: #{@data}"
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def map_iso_dates(data)
|
35
|
+
utc_converter = lambda { |value|
|
36
|
+
value_to_convert = value
|
37
|
+
map_utc = lambda { |value_item| utc_converter.call(value_item) }
|
38
|
+
|
39
|
+
return value_to_convert.map(&map_utc) if value_to_convert.respond_to? :map
|
40
|
+
|
41
|
+
value_to_convert = value_to_convert.to_time if value_to_convert.is_a?(DateTime) || value_to_convert.is_a?(Date)
|
42
|
+
value_to_convert = value_to_convert.utc.iso8601 if value_to_convert.is_a? Time
|
43
|
+
|
44
|
+
value_to_convert
|
45
|
+
}
|
46
|
+
|
47
|
+
mappedData = data.map do |key, value|
|
48
|
+
if value.is_a? Hash
|
49
|
+
[key, map_iso_dates(value)]
|
50
|
+
else
|
51
|
+
[key, utc_converter.call(value)]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Hash[mappedData]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class EventDataValidationError < StandardError
|
60
|
+
attr_reader :invalid_property_names
|
61
|
+
|
62
|
+
def initialize(invalid_property_names)
|
63
|
+
@invalid_property_names = invalid_property_names
|
64
|
+
end
|
65
|
+
|
66
|
+
def message
|
67
|
+
messages = ['The following properties use the reserved prefix tp_:'] + @invalid_property_names.map do |property_name|
|
68
|
+
"->#{property_name}"
|
69
|
+
end
|
70
|
+
messages.join "\n"
|
71
|
+
end
|
72
|
+
end
|
73
73
|
end
|
@@ -1,49 +1,49 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
|
-
module ConnectClient
|
4
|
-
class EventPushResponse
|
5
|
-
attr_reader :data
|
6
|
-
attr_reader :http_status_code
|
7
|
-
|
8
|
-
def initialize(code, content_type, response_body, events_pushed)
|
9
|
-
@http_status_code = code.to_s
|
10
|
-
|
11
|
-
if content_type.include? 'application/json'
|
12
|
-
body = response_body
|
13
|
-
body = '{}' if response_body.to_s.empty?
|
14
|
-
parse_body(body, events_pushed)
|
15
|
-
else
|
16
|
-
@data = response_body
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def success?
|
21
|
-
@http_status_code.start_with? '2'
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_s
|
25
|
-
%{
|
26
|
-
Status: #{@http_status_code}
|
27
|
-
Successful: #{success?}
|
28
|
-
Data: #{data}
|
29
|
-
}
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def parse_body(body, events_pushed)
|
35
|
-
@data = JSON.parse(body, :symbolize_names => true)
|
36
|
-
|
37
|
-
if (events_pushed.is_a?(Hash) && @data.is_a?(Hash))
|
38
|
-
@data.merge!(events_pushed) do |collection_name, responses, events|
|
39
|
-
responses.zip(events).map do |response, event|
|
40
|
-
response[:event] = event.data
|
41
|
-
response
|
42
|
-
end
|
43
|
-
end
|
44
|
-
else
|
45
|
-
@data[:event] = events_pushed.data
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ConnectClient
|
4
|
+
class EventPushResponse
|
5
|
+
attr_reader :data
|
6
|
+
attr_reader :http_status_code
|
7
|
+
|
8
|
+
def initialize(code, content_type, response_body, events_pushed)
|
9
|
+
@http_status_code = code.to_s
|
10
|
+
|
11
|
+
if content_type.include? 'application/json'
|
12
|
+
body = response_body
|
13
|
+
body = '{}' if response_body.to_s.empty?
|
14
|
+
parse_body(body, events_pushed)
|
15
|
+
else
|
16
|
+
@data = response_body
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def success?
|
21
|
+
@http_status_code.start_with? '2'
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
%{
|
26
|
+
Status: #{@http_status_code}
|
27
|
+
Successful: #{success?}
|
28
|
+
Data: #{data}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parse_body(body, events_pushed)
|
35
|
+
@data = JSON.parse(body, :symbolize_names => true)
|
36
|
+
|
37
|
+
if (events_pushed.is_a?(Hash) && @data.is_a?(Hash))
|
38
|
+
@data.merge!(events_pushed) do |collection_name, responses, events|
|
39
|
+
responses.zip(events).map do |response, event|
|
40
|
+
response[:event] = event.data
|
41
|
+
response
|
42
|
+
end
|
43
|
+
end
|
44
|
+
else
|
45
|
+
@data[:event] = events_pushed.data
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
49
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
module ConnectClient
|
2
|
-
module Http
|
3
|
-
class DeferredHttpResponse
|
4
|
-
include EventMachine::Deferrable
|
5
|
-
alias_method :response_received, :callback
|
6
|
-
alias_method :error_occured, :errback
|
7
|
-
end
|
8
|
-
end
|
1
|
+
module ConnectClient
|
2
|
+
module Http
|
3
|
+
class DeferredHttpResponse
|
4
|
+
include EventMachine::Deferrable
|
5
|
+
alias_method :response_received, :callback
|
6
|
+
alias_method :error_occured, :errback
|
7
|
+
end
|
8
|
+
end
|
9
9
|
end
|