Class: OandaAPI::ResourceBase

Inherits:
Object
  • Object
show all
Defined in:
lib/oanda_api/resource_base.rb

Overview

Base class for all Resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ResourceBase

Returns a new instance of ResourceBase.

Parameters:

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

    collection of resource attributes. See the Oanda Developer Guide for documentation about resource attributes.


18
19
20
21
# File 'lib/oanda_api/resource_base.rb', line 18

def initialize(attributes = {})
  initialize_attributes Utils.rubyize_keys(attributes)
  @location = attributes.location if attributes.respond_to? :location
end

Instance Attribute Details

#locationString

Returns the location header if one is returned in an API response.

Examples:

Using the location attribute

client = OandaAPI::Client::TokenClient.new :practice, token
all_transactions = client.(123).alltransactions.get
all_transactions.location # => https://fxtrade.oanda.com/transactionhistory/d3aed6823c.json.zip

Returns:

  • (String)

    the location header if one is returned in an API response.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oanda_api/resource_base.rb', line 12

class ResourceBase
  attr_accessor :location

  # @param [Hash] attributes collection of resource attributes. See the
  #   {http://developer.oanda.com/rest-live/development-guide/ Oanda Developer Guide}
  #   for documentation about resource attributes.
  def initialize(attributes = {})
    initialize_attributes Utils.rubyize_keys(attributes)
    @location = attributes.location if attributes.respond_to? :location
  end

  # Serializes an instance as JSON
  # @return [String] a stringified JSON representation of an instance
  def to_json
    JSON.generate @_attributes.merge(custom_attributes)
  end

  private

  # @private
  # Initializes attributes.
  #
  # @param [Hash] attributes collection of resource attributes.
  # @return [void]
  def initialize_attributes(attributes)
    @_attributes = attributes
    attributes.each do |key, value|
      send("#{key}=", value) if respond_to? key
    end
  end

  # Provides additional attributes used in serialization.
  # @return [Hash] returns a hash of customized attributes for serialization
  def custom_attributes
    {}.tap { |hash| hash[:location] = location if location }
  end
end

Instance Method Details

#to_jsonString

Serializes an instance as JSON

Returns:

  • (String)

    a stringified JSON representation of an instance


25
26
27
# File 'lib/oanda_api/resource_base.rb', line 25

def to_json
  JSON.generate @_attributes.merge(custom_attributes)
end