Module: Soaspec::ExchangeExtractor

Included in:
Exchange
Defined in:
lib/soaspec/exchange/exchange_extractor.rb

Overview

Methods for extracting aspects of the traffic for a request / response in an exchange from the ExchangeHandler that it’s tied to

Instance Method Summary collapse

Instance Method Details

#[](path) ⇒ String Also known as: value_from_path

Extract value from path api class

Examples:

Extract unique value

@exchange['unique_value_name']

Extract value via JSON path

@exchange['$..path.to.element']

Extract value via XPath

@exchange['//path/to/element']

Parameters:

  • path (Object)

    Path to return element for api class E.g - for SOAP this is XPath string. For JSON, this is Hash dig Array

Returns:

  • (String)

    Value at path



33
34
35
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 33

def [](path)
  exchange_handler.value_from_path(response, path.to_s)
end

#element?(path) ⇒ Boolean

Using same path syntax as []. Returns true of false depending on whether an element is found

Returns:

  • (Boolean)

    Whether an element exists at the path



46
47
48
49
50
51
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 46

def element?(path)
  self[path]
  true
rescue NoElementAtPath
  false
end

#formatSymbol

Returns Type of response. XML, JSON, etc.

Returns:

  • (Symbol)

    Type of response. XML, JSON, etc



40
41
42
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 40

def format
  Interpreter.response_type_for(response)
end

#pretty_response_bodyString

Returns Get multiline pretty version of response.

Returns:

  • (String)

    Get multiline pretty version of response



77
78
79
80
81
82
83
84
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 77

def pretty_response_body
  case format
  when :json then JSON.pretty_generate to_hash
  when :xml then response.body # TODO: Single line XML make multiline
  else
    response.body
  end
end

#requestObject

Request of API call. Either intended request or actual request

Returns:

  • (Object)

    Object representing request of API



9
10
11
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 9

def request
  exchange_handler.request(@response)
end

#status_codeInteger

Get status code from api class. This is http response code for Web Api

Returns:

  • (Integer)

    Status code from api class



15
16
17
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 15

def status_code
  exchange_handler.status_code_for(response)
end

#successful_status_code?Boolean

Returns Whether Api success code is successful.

Returns:

  • (Boolean)

    Whether Api success code is successful



20
21
22
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 20

def successful_status_code?
  (200..299).cover? status_code
end

#to_hashHash

Return the response equivalent of the response. XML, JSON will be converted to a Hash

Examples:

Counting items in a JSON list

# Say there is JSON response {"notes":[{"title":"note1","note":"A note"},{"title":"note2"}]}
hash = @exchange.to_hash
expect(hash['notes'].count).to eq 2
expect(hash['notes'].first['title']).to eq 'note1'

Returns:

  • (Hash)

    Hash representing the response of the API



72
73
74
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 72

def to_hash
  exchange_handler.to_hash(response)
end

#values_from_path(path, attribute: nil) ⇒ Array

Returns List of values found at path.

Examples:

Counting items in a JSON list

# Say there is JSON response {"notes":[{"title":"note1","note":"A note"},{"title":"note2"}]}
titles = @exchange.values_at_path('$..title')
expect(titles.count).to eq 2
expect(titles.first).to eq 'note1'

Parameters:

  • path (String)

    XPath, JSONPath to extract value

  • attribute (String) (defaults to: nil)

    Attribute to obtain from XML element

Returns:

  • (Array)

    List of values found at path



61
62
63
# File 'lib/soaspec/exchange/exchange_extractor.rb', line 61

def values_from_path(path, attribute: nil)
  exchange_handler.values_from_path(response, path, attribute: attribute)
end