class Icss::Meta::MessageSample

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::Meta::Message and Icss::Meta::Protocol with helper methods for sample calls.

Attributes

message[RW]
raw_response[RW]

Public Instance Methods

fetch_response!(hostname="", extra_query_params={}) click to toggle source

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.

# File lib/icss/message/message_sample.rb, line 71
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={}) click to toggle source

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

@param [String] hostname The hostname or hostname:port to include in the URL @param [Hash] extra_query_params A hash of extra params to in

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.

# File lib/icss/message/message_sample.rb, line 38
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
path() click to toggle source
# File lib/icss/message/message_sample.rb, line 52
def path
  ((@url && @url.path).present? ? @url.path : "/#{message.path}" )
end
query_hash(extra_query_params={}) click to toggle source
# File lib/icss/message/message_sample.rb, line 45
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_obj() click to toggle source

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

# File lib/icss/message/message_sample.rb, line 25
def response_obj
  return if response.blank?
  message.response.receive(response)
end
url=(new_url) click to toggle source

@param [String, Addressable::URI]

the 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.
# File lib/icss/message/message_sample.rb, line 60
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

Protected Instance Methods

fetch_raw_response(full_url) click to toggle source
# File lib/icss/message/message_sample.rb, line 92
def fetch_raw_response full_url
  RestClient.get(full_url.to_s) do |response, request, result|
    response
  end
end