Class: Reuters::Builder

Inherits:
Hash
  • Object
show all
Defined in:
lib/reuters/builder.rb

Overview

Note:

This class is documented with worked examples on the main README file.

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.

Instance Method Summary collapse

Constructor Details

#initialize(body = {}, &block) ⇒ Builder

Returns a new instance of Builder.



15
16
17
18
19
# File 'lib/reuters/builder.rb', line 15

def initialize(body = {}, &block)
  self[:attributes!] = {}
  attributes body
  block.call(self) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, body = nil, camelcase = true, &block) ⇒ Object

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 Reuters::Builder is returned.



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

Instance Method Details

#attribute_key?(key, attr_key) ⇒ Boolean

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.

Returns:

  • (Boolean)

    True if the key exists, false otherwise



86
87
88
89
# 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

#attribute_keys(key) ⇒ Array

Returns all keys inside the attributes hash.

Returns:

  • (Array)

    All attribute keys.



94
95
96
# File 'lib/reuters/builder.rb', line 94

def attribute_keys(key)
  (self[:attributes!][camelize(key)] || {}).keys
end

#attributes(attribs, camelcase = true) ⇒ Hash

Assign element attributes to a specific key inside the hash. All attributes are assigned to the ‘hidden’ :attributes! key.

Examples:

Adding an attribute

req = Reuters::Builder.new
req.attributes(exchange_code: { id: 123 })

Parameters:

  • attribs (Hash)

    to add to the attributes! key.

  • camelcase (Boolean) (defaults to: true)

    the keys inside the hash?

Returns:

  • (Hash)

    the resulting hash that was added.



33
34
35
36
37
38
39
# 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

#key?(key) ⇒ Boolean

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.

Returns:

  • (Boolean)

    True if the key exists, false otherwise.



76
77
78
# File 'lib/reuters/builder.rb', line 76

def key?(key)
  super || super(camelize(key))
end

#keysArray

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.

Returns:

  • (Array)

    All keys except for :attributes! which will always exist.



66
67
68
# File 'lib/reuters/builder.rb', line 66

def keys
  super - [:attributes!]
end