class Reuters::Builder
This class enables the rapid and relatively painless construction of the body of Reuters
API calls.
It extends the Hash class, which means that it is compatible with the Savon request call and includes all the basic functionality of a Hash.
@note This class is documented with worked examples
on the main README file.
Public Class Methods
# File lib/reuters/builder.rb, line 15 def initialize(body = {}, &block) self[:attributes!] = {} attributes body block.call(self) if block end
Public Instance Methods
Attempts to find a key that exists within the attributes hash. If the key cannot be found in its current format, the method attempts to camelcase the key and search again.
@return [Boolean] True if the key exists, false otherwise
# File lib/reuters/builder.rb, line 86 def attribute_key?(key, attr_key) attrs = attribute_keys(key) attrs.include?(camelize(attr_key)) || attrs.include?(attr_key) end
Returns all keys inside the attributes hash.
@return [Array] All attribute keys.
# File lib/reuters/builder.rb, line 94 def attribute_keys(key) (self[:attributes!][camelize(key)] || {}).keys end
Assign element attributes to a specific key inside the hash. All attributes are assigned to the ‘hidden’ :attributes! key.
@example Adding an attribute
req = Reuters::Builder.new req.attributes(exchange_code: { id: 123 })
@param [Hash] attribs to add to the attributes! key. @param [Boolean] camelcase the keys inside the hash?
@return [Hash] the resulting hash that was added.
# File lib/reuters/builder.rb, line 33 def attributes(attribs, camelcase = true) camelize_keys(attribs).each do |key, value| hash = camelcase ? camelize_keys(value) : value self[:attributes!][key] ||= {} self[:attributes!][key].merge! hash end end
Attempts to find a key that exists in the hash. If the key cannot be found in its current format, the method attempts to camelcase the key and search again.
@return [Boolean] True if the key exists, false otherwise.
# File lib/reuters/builder.rb, line 76 def key?(key) super || super(camelize(key)) end
Return all keys inside this Hash object except for the :attributes! key. This key is hidden so that it cannot be mistaken for an XML Element.
@return [Array] All keys except for :attributes!
which will always exist.
# File lib/reuters/builder.rb, line 66 def keys super - [:attributes!] end
Uses the name of the missing method and adds it as a new key inside this hash. If the method is called as an assignment, the value will be set to the element, otherwise a nested {Builder} is returned.
# File lib/reuters/builder.rb, line 46 def method_missing(name, body = nil, camelcase = true, &block) key = camelize name if name.match(/\=$/) assign_val(key, body) else assign_body(key, body, camelcase) end self[key] ||= Reuters::Builder.new(&block) if block self[key] ||= Reuters::Builder.new end
Private Instance Methods
# File lib/reuters/builder.rb, line 104 def assign_body(key, body, camelcase = true) case body when Hash attributes({ key => body }, camelcase) when Array self[key] ||= Array.new(body.count) { '' } attributes(key => order_attributes(body)) end end
# File lib/reuters/builder.rb, line 100 def assign_val(key, body) self[key] = body end
# File lib/reuters/builder.rb, line 124 def camelize(key) key.to_s.camelize.gsub(/[^a-z0-9]/i, '') end
# File lib/reuters/builder.rb, line 128 def camelize_keys(hash) new_hash = {} hash.each { |key, val| new_hash[camelize key] = val } new_hash end
# File lib/reuters/builder.rb, line 114 def order_attributes(attribs) keys = attribs.map(&:keys).flatten.uniq ordered = {} keys.each do |k| ordered[k] = [] attribs.each { |a| ordered[k] << (a[k].to_s || '') } end ordered end