connect_client 0.2.2 → 0.3.1

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.
@@ -1,41 +1,49 @@
1
- require_relative 'connect_client/client'
2
- require_relative 'connect_client/configuration'
3
-
4
- module ConnectClient
5
- class << self
6
-
7
- def configure
8
- config = Configuration.new
9
- yield(config)
10
-
11
- @client = ConnectClient::Client.new config
12
- end
13
-
14
- def reset
15
- @client = nil
16
- end
17
-
18
- def method_missing(method, *args, &block)
19
- return super unless client.respond_to?(method)
20
- client.send(method, *args, &block)
21
- end
22
-
23
- def respond_to?(method)
24
- return (!@client.nil? && @client.respond_to?(method)) || super
25
- end
26
-
27
- private
28
-
29
- def client
30
- raise UnconfiguredError if @client.nil?
31
-
32
- @client
33
- end
34
- end
35
-
36
- class UnconfiguredError < StandardError
37
- def message
38
- "Connect must configured before it can be used, please call Connect.configure"
39
- end
40
- end
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
@@ -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