Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, http_client = nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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
51
# File 'lib/client.rb', line 16

def initialize(config = nil, 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_futureObject (readonly)

Returns the value of attribute data_future.



10
11
12
# File 'lib/client.rb', line 10

def data_future
  @data_future
end

#deserializerObject

Returns the value of attribute deserializer.



9
10
11
# File 'lib/client.rb', line 9

def deserializer
  @deserializer
end

#exceptionObject (readonly)

Returns the value of attribute exception.



10
11
12
# File 'lib/client.rb', line 10

def exception
  @exception
end

#executorObject

Returns the value of attribute executor.



9
10
11
# File 'lib/client.rb', line 9

def executor
  @executor
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/client.rb', line 9

def headers
  @headers
end

#http_clientObject

Returns the value of attribute http_client.



9
10
11
# File 'lib/client.rb', line 9

def http_client
  @http_client
end

#promiseObject (readonly)

Returns the value of attribute promise.



10
11
12
# File 'lib/client.rb', line 10

def promise
  @promise
end

#queryObject

Returns the value of attribute query.



9
10
11
# File 'lib/client.rb', line 9

def query
  @query
end

#serializerObject

Returns the value of attribute serializer.



9
10
11
# File 'lib/client.rb', line 9

def serializer
  @serializer
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/client.rb', line 9

def url
  @url
end

Class Method Details

.create(config, http_client = nil) ⇒ Object



12
13
14
# File 'lib/client.rb', line 12

def self.create(config, http_client = nil)
  Client.new(config, http_client || DefaultHttpClient.create(DefaultHttpClientConfig.create))
end

Instance Method Details

#closeObject



73
74
75
# File 'lib/client.rb', line 73

def close
  @http_client.close
end

#context_dataObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/client.rb', line 53

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



65
66
67
68
69
70
71
# File 'lib/client.rb', line 65

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/client.rb', line 77

def success?
  @promise.success?
end