Class: Client
- Inherits:
-
Object
- Object
- Client
- Defined in:
- lib/client.rb
Instance Attribute Summary collapse
-
#data_future ⇒ Object
readonly
Returns the value of attribute data_future.
-
#deserializer ⇒ Object
Returns the value of attribute deserializer.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#executor ⇒ Object
Returns the value of attribute executor.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
-
#promise ⇒ Object
readonly
Returns the value of attribute promise.
-
#query ⇒ Object
Returns the value of attribute query.
-
#serializer ⇒ Object
Returns the value of attribute serializer.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #context_data ⇒ Object
-
#initialize(config, http_client = nil) ⇒ Client
constructor
A new instance of Client.
- #publish(event) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(config, http_client = nil) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/client.rb', line 15 def initialize(config, http_client = nil) endpoint = config.endpoint raise ArgumentError.new("Missing Endpoint configuration") if endpoint.nil? || endpoint.empty? api_key = config.api_key raise ArgumentError.new("Missing APIKey configuration") if api_key.nil? || api_key.empty? application = config.application raise ArgumentError.new("Missing Application configuration") if application.nil? || application.empty? environment = config.environment raise ArgumentError.new("Missing Environment configuration") if environment.nil? || environment.empty? @url = "#{endpoint}/context" @http_client = http_client @deserializer = config.context_data_deserializer @serializer = config.context_event_serializer @executor = config.executor @deserializer = DefaultContextDataDeserializer.new if @deserializer.nil? @serializer = DefaultContextEventSerializer.new if @serializer.nil? @headers = { "Content-Type": "application/json", "X-API-Key": api_key, "X-Application": application, "X-Environment": environment, "X-Application-Version": "0", "X-Agent": "absmartly-ruby-sdk" } @query = { "application": application, "environment": environment } end |
Instance Attribute Details
#data_future ⇒ Object (readonly)
Returns the value of attribute data_future.
9 10 11 |
# File 'lib/client.rb', line 9 def data_future @data_future end |
#deserializer ⇒ Object
Returns the value of attribute deserializer.
8 9 10 |
# File 'lib/client.rb', line 8 def deserializer @deserializer end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
9 10 11 |
# File 'lib/client.rb', line 9 def exception @exception end |
#executor ⇒ Object
Returns the value of attribute executor.
8 9 10 |
# File 'lib/client.rb', line 8 def executor @executor end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/client.rb', line 8 def headers @headers end |
#http_client ⇒ Object
Returns the value of attribute http_client.
8 9 10 |
# File 'lib/client.rb', line 8 def http_client @http_client end |
#promise ⇒ Object (readonly)
Returns the value of attribute promise.
9 10 11 |
# File 'lib/client.rb', line 9 def promise @promise end |
#query ⇒ Object
Returns the value of attribute query.
8 9 10 |
# File 'lib/client.rb', line 8 def query @query end |
#serializer ⇒ Object
Returns the value of attribute serializer.
8 9 10 |
# File 'lib/client.rb', line 8 def serializer @serializer end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/client.rb', line 8 def url @url end |
Class Method Details
.create(config, http_client = nil) ⇒ Object
11 12 13 |
# File 'lib/client.rb', line 11 def self.create(config, http_client = nil) Client.new(config, http_client || DefaultHttpClient.create(config.http_client_config)) end |
Instance Method Details
#close ⇒ Object
72 73 74 |
# File 'lib/client.rb', line 72 def close @http_client.close end |
#context_data ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/client.rb', line 52 def context_data @promise = @http_client.get(@url, @query, @headers) unless @promise.success? @exception = Exception.new(@promise.body) return self end content = (@promise.body || {}).to_s @data_future = @deserializer.deserialize(content, 0, content.size) self end |
#publish(event) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/client.rb', line 64 def publish(event) content = @serializer.serialize(event) response = @http_client.put(@url, nil, @headers, content) return Exception.new(response.body) unless response.success? response end |
#success? ⇒ Boolean
76 77 78 |
# File 'lib/client.rb', line 76 def success? @promise&.success? || false end |