Class: Icss::Meta::MessageSample
- Includes:
- ReceiverModel
- Defined in:
- lib/icss/message/message_sample.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::Meta::Message and Icss::Meta::Protocol with helper methods for sample calls.
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
-
#raw_response ⇒ Object
the raw http response from fetching.
Instance Method Summary collapse
-
#fetch_response!(hostname = "", extra_query_params = {}) ⇒ Object
retrieve the response from the given host, storing it in response.
-
#full_url(hostname, extra_query_params = {}) ⇒ Object
The URL implied by the given hostname and the sample request parameters.
- #path ⇒ Object
- #query_hash(extra_query_params = {}) ⇒ Object
-
#response_obj ⇒ Object
Whips up the class implied by the ICSS type of this message’s response, and populates it using the response hash.
- #url=(new_url) ⇒ Object
Methods included from ReceiverModel
Methods included from ReceiverModel::ActsAsTuple
Methods included from RecordModel
#attr_set?, included, #receive!, #to_zaml
Methods included from ReceiverModel::ActsAsLoadable
Methods included from ReceiverModel::ActsAsHash
#[], #[]=, #attributes, #delete, included, #keys, #to_hash
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
21 22 23 |
# File 'lib/icss/message/message_sample.rb', line 21 def @message end |
#raw_response ⇒ Object
the raw http response from fetching
18 19 20 |
# File 'lib/icss/message/message_sample.rb', line 18 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 = {}) ⇒ 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.
38 39 40 41 42 43 |
# 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 ⇒ Object
52 53 54 |
# File 'lib/icss/message/message_sample.rb', line 52 def path ((@url && @url.path).present? ? @url.path : "/#{.path}" ) end |
#query_hash(extra_query_params = {}) ⇒ Object
45 46 47 48 49 50 |
# 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 ⇒ Object
Whips up the class implied by the ICSS type of this message’s response, and populates it using the response hash.
25 26 27 28 |
# File 'lib/icss/message/message_sample.rb', line 25 def response_obj return if response.blank? .response.receive(response) end |
#url=(new_url) ⇒ Object
60 61 62 63 64 65 66 |
# 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 |