Class: Cts::Mpx::Driver::Response

Inherits:
Object
  • Object
show all
Includes:
Creatable
Defined in:
lib/cts/mpx/driver/response.rb

Overview

Class to contain a response from the services, has a few helper methods to make reading the data easier.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#originalExcon::Response

Returns copy of the original excon response.

Returns:

  • (Excon::Response)

    copy of the original excon response.



7
8
9
10
11
12
13
14
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
51
52
53
54
55
56
57
# File 'lib/cts/mpx/driver/response.rb', line 7

class Response
  include Creatable
  attribute name: 'original', kind_of: Excon::Response

  # Hash output of the data returned from the services.
  # @return [Hash] Hash including keys specific to the service and type of service.
  def data
    return @data if @data

    raise 'response does not appear to be healthy' unless healthy?

    # TODO: make the driver.load file become load string.
    begin
      @data = Oj.compat_load(original.body)
    rescue Oj::ParseError => e
      raise "could not parse data: #{e}"
    end

    raise ServiceError, "title: #{@data['title']} description: #{@data['description']} cid: (#{@data['correlationId']})" if @data['isException']

    @data
  end

  # Is the response healthy?   did it have a status code outside 2xx or 3xx.
  # @return [TrueFalse] false if status <= 199 or => 400, otherwise true.
  def healthy?
    return false if status <= 199 || status >= 400

    true
  end

  # Does this response contain a service exception
  # @return [TrueFalse] true if it does, false if it does not.
  def service_exception?
    original.body.include? '"isException":true,'
  end

  # a page of data, processes the response.data for any entries.
  # @return [Cts::Mpx::Driver::Page] a page of data.
  def page
    raise 'response does not appear to be healthy' unless healthy?

    Cts::Mpx::Driver::Page.create entries: data['entries'], xmlns: data['$xmlns']
  end

  # Status code of the response
  # @return [Fixnum] http status code
  def status
    original.status || nil
  end
end

Instance Method Details

#dataHash

Hash output of the data returned from the services.

Returns:

  • (Hash)

    Hash including keys specific to the service and type of service.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cts/mpx/driver/response.rb', line 13

def data
  return @data if @data

  raise 'response does not appear to be healthy' unless healthy?

  # TODO: make the driver.load file become load string.
  begin
    @data = Oj.compat_load(original.body)
  rescue Oj::ParseError => e
    raise "could not parse data: #{e}"
  end

  raise ServiceError, "title: #{@data['title']} description: #{@data['description']} cid: (#{@data['correlationId']})" if @data['isException']

  @data
end

#healthy?TrueFalse

Is the response healthy? did it have a status code outside 2xx or 3xx.

Returns:

  • (TrueFalse)

    false if status <= 199 or => 400, otherwise true.



32
33
34
35
36
# File 'lib/cts/mpx/driver/response.rb', line 32

def healthy?
  return false if status <= 199 || status >= 400

  true
end

#pageCts::Mpx::Driver::Page

a page of data, processes the response.data for any entries.

Returns:



46
47
48
49
50
# File 'lib/cts/mpx/driver/response.rb', line 46

def page
  raise 'response does not appear to be healthy' unless healthy?

  Cts::Mpx::Driver::Page.create entries: data['entries'], xmlns: data['$xmlns']
end

#service_exception?TrueFalse

Does this response contain a service exception

Returns:

  • (TrueFalse)

    true if it does, false if it does not.



40
41
42
# File 'lib/cts/mpx/driver/response.rb', line 40

def service_exception?
  original.body.include? '"isException":true,'
end

#statusFixnum

Status code of the response

Returns:

  • (Fixnum)

    http status code



54
55
56
# File 'lib/cts/mpx/driver/response.rb', line 54

def status
  original.status || nil
end