Class: Curbala::Action

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

Direct Known Subclasses

SERVICE_CLASS_NAME::ACTION_CLASS_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url_segment, input_config, input_args_hash, logger, simulated_status = 200, simulated_response = "simulated response") ⇒ Action

Returns a new instance of Action.



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
# File 'lib/curbala/action.rb', line 8

def initialize(service_url_segment, input_config, input_args_hash, logger, simulated_status=200, simulated_response="simulated response")
  begin
    
    @payload, @args_hash, @config = '', input_args_hash, input_config
    @url = "#{@config['url']}#{service_url_segment}#{action_url_segment}"
    
    @config['simulate'] == true ?
      inject_status_and_response(simulated_status, simulated_response) :
      invoke_curl_action
    
  rescue Exception => @exception

    @success = false
    @message = "Service Not Available: #{@exception.message}" # : BACKTRACE: #{@exception.backtrace[0..500]}"
    logger.debug "Exception occurred: #{@exception.message}: BACKTRACE: #{@exception.backtrace}"
    @raw_response_string ||= 'exception occurred in action processing'
    @response ||= @raw_response_string
    @http_status ||= -1

  end
  
  self
  
ensure
  
  logger.debug ''
  logger.debug("#{self.class.name}: #{url}")
  logger.debug("config:        #{config.inspect}")
  logger.debug("args_hash:     #{args_hash.inspect}")
  logger.debug("payload:       #{payload.inspect}") unless payload.nil? || payload.empty?
  logger.debug("http status:   #{http_status},   success: #{success},   message: #{message}")
  logger.debug("raw response string:")
  logger.debug(raw_response_string.strip)
  logger.debug("unpacked response:  #{response.class.name}")
  logger.debug(response.inspect)
  logger.debug ''
  
end

Instance Attribute Details

#args_hashObject

Returns the value of attribute args_hash.



6
7
8
# File 'lib/curbala/action.rb', line 6

def args_hash
  @args_hash
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/curbala/action.rb', line 6

def config
  @config
end

#curlObject

Returns the value of attribute curl.



6
7
8
# File 'lib/curbala/action.rb', line 6

def curl
  @curl
end

#exceptionObject

Returns the value of attribute exception.



6
7
8
# File 'lib/curbala/action.rb', line 6

def exception
  @exception
end

#http_statusObject

Returns the value of attribute http_status.



6
7
8
# File 'lib/curbala/action.rb', line 6

def http_status
  @http_status
end

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/curbala/action.rb', line 6

def message
  @message
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/curbala/action.rb', line 6

def payload
  @payload
end

#raw_response_stringObject

Returns the value of attribute raw_response_string.



6
7
8
# File 'lib/curbala/action.rb', line 6

def raw_response_string
  @raw_response_string
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/curbala/action.rb', line 6

def response
  @response
end

#successObject

Returns the value of attribute success.



6
7
8
# File 'lib/curbala/action.rb', line 6

def success
  @success
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/curbala/action.rb', line 6

def url
  @url
end

Instance Method Details

#hash_from_json_responseObject



71
72
73
# File 'lib/curbala/action.rb', line 71

def hash_from_json_response
  @response = (ActiveSupport::JSON.decode(@raw_response_string) rescue {})
end

#hash_from_xml_responseObject

response utilities:



66
67
68
69
# File 'lib/curbala/action.rb', line 66

def hash_from_xml_response
  @response = Hash.from_xml(@raw_response_string)
  @response ||= {}
end

#xml_payload(xml) ⇒ Object



55
56
57
58
# File 'lib/curbala/action.rb', line 55

def xml_payload(xml)
  xml_request
  @payload = xml
end

#xml_payload_from_args_hash(root) ⇒ Object

APIs have different formatting/hygiene needs…



60
61
62
63
# File 'lib/curbala/action.rb', line 60

def xml_payload_from_args_hash(root) # APIs have different formatting/hygiene needs...
  xml_request
  @payload = @args_hash.to_xml(:root => root, :indent => 0) # .gsub(/(>[ \t\r\n]+<\/)/,"><\/").gsub(/[ \t\r\n]+^/,'')
end

#xml_requestObject

request utilities:



50
51
52
53
# File 'lib/curbala/action.rb', line 50

def xml_request
  @curl.headers['Accept'] = 'application/xml'
  @curl.headers['Content-Type'] = 'application/xml'
end