Class: Icss::SampleMessageCall

Inherits:
Object
  • Object
show all
Includes:
Receiver
Defined in:
lib/icss/sample_message_call.rb

Overview

Holds a sample call for a message and its expected response

You may define the request parameters using an array of parameters or with the corresponding URL it would render to.

This file also decorates Icss::Message and Icss::Protocol with helper methods for sample calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messageObject

Returns the value of attribute message.



19
20
21
# File 'lib/icss/sample_message_call.rb', line 19

def message
  @message
end

#raw_responseObject

the raw http response from fetching



16
17
18
# File 'lib/icss/sample_message_call.rb', line 16

def raw_response
  @raw_response
end

Instance Method Details

#fetch_response!(hostname = "", extra_query_params = {}) ⇒ Object

retrieve the response from the given host, storing it in response. this catches all server errors and constructs a dummy response hash if the call fails.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/icss/sample_message_call.rb', line 70

def fetch_response! hostname="", extra_query_params={}
  self.raw_response = fetch_raw_response( full_url(hostname, extra_query_params) )
  begin
    resp_hsh = JSON.load(raw_response.body)
  rescue StandardError => e
    warn ["  error parsing response: #{e}"].join("\n")
    self.response = nil
    self.error    = "JsonParseError"
    return
  end
  if raw_response.code == 200
    self.response = resp_hsh
    self.error    = nil
  else
    self.response = nil
    self.error    = resp_hsh["error"]
  end
end

#full_url(hostname, extra_query_params = {}) ⇒ Object

The URL implied by the given hostname and the sample request parameters.

The URI expects string values in the hash used to build the query – if calling #to_s on a field won’t do what you want, clobber the value beforehand.

Parameters:

  • hostname (String)

    The hostname or hostname:port to include in the URL

  • extra_query_params (Hash) (defaults to: {})

    A hash of extra params to in



29
30
31
32
33
34
# File 'lib/icss/sample_message_call.rb', line 29

def full_url hostname, extra_query_params={}
  host, port = hostname.split(':', 2)
  u = Addressable::URI.new(:host => host, :port => port, :path => self.path, :scheme => 'http')
  u.query_values = query_hash(extra_query_params)
  u
end

#pathObject



43
44
45
# File 'lib/icss/sample_message_call.rb', line 43

def path
  ((@url && @url.path).present? ? @url.path : "/#{message.path}" )
end

#query_hash(extra_query_params = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/icss/sample_message_call.rb', line 36

def query_hash extra_query_params={}
  hsh = (@url.present? ? @url.query_values : request.first.to_hash) rescue {}
  hsh = hsh.merge extra_query_params
  hsh.each{|k,v| hsh[k] = v.to_s }
  hsh
end

#response_objObject

Whips up the class implied by the ICSS type of this message’s response, and populates it using the response hash.



61
62
63
64
65
# File 'lib/icss/sample_message_call.rb', line 61

def response_obj
  return if response.blank?
  klass = message.response.ruby_klass
  klass.receive(response.compact_blank!)
end

#url=(new_url) ⇒ Object

Parameters:

  • the (String, Addressable::URI)

    URL can be fully-qualified (htttp://api.infochimps.com/this/that?the=other) or relative (this/that?the=other) and the path must match that of the message.



51
52
53
54
55
56
57
# File 'lib/icss/sample_message_call.rb', line 51

def url= new_url
  if new_url.is_a?(String)
    unless new_url.include?('?') then warn "sample request url should have a '?' introducing its query parameters: {#{new_url}}" ; end
    new_url = Addressable::URI.parse(new_url)
  end
  @url = new_url
end