Class: OandaAPI::ResourceBase

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

Overview

Base class for all Resources.

Constant Summary collapse

NOT_PLURALIZED =

List of API resources that don't follow normal REST naming standards, and are not pluralized despite being collections.

[:calendar]

Instance Attribute Summary collapse

Class Method 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.



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

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.



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/oanda_api/resource_base.rb', line 13

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(*args)
    JSON.generate @_attributes.merge(custom_attributes), *args
  end

  # Returns the class of a Resource if klass_symbol is the name
  #   of a resource, otherwise nil.
  #
  #   @example Example:
  #     ResourceBase.class_from_symbol(:transaction_history) => OandaAPI::Resource::TransactionHistory
  #     ResourceBase.class_from_symbol(:calendar_event) => OandaAPI::Resource::Labs::CalendarEvent
  #
  # @param [Symbol] resource_symbol symbolized resource name
  #
  # @return [Nil] if resource_symbol is not a Resource.
  #
  # @return [Class] if resource_symbol is identifies a Resource.
  def self.class_from_symbol(resource_symbol)
    descendant resource_symbol
  end

  # Tests whether a class is a Labs Resource.
  #
  # @param [Class] klass the class to be tested.
  #
  # @return [Boolean] True if the class is a Labs Resource.
  def self.labs_resource?(klass)
    (@lab_resources ||=[]).include? klass
  end

  # List of API resources that don't follow normal REST naming standards,
  #  and are not pluralized despite being collections.
  NOT_PLURALIZED = [:calendar]

  # Returns a pluralized version of the resource class name
  # @param [Symbol] klass_symbol
  # @return [String] the pluralized resource class name
  def self.pluralize(klass_symbol)
    NOT_PLURALIZED.include?(klass_symbol.to_sym) ? klass_symbol.to_s : OandaAPI::Utils.pluralize(klass_symbol)
  end

  private

  # @private
  # Called whenever ResourceBase class is inherited by a descendant class.
  #  Used to track descendant classes.
  #  Used to identify classes within the "::Labs::" namespace.
  def self.inherited(klass)
    ResourceBase.inherited(klass) unless self == OandaAPI::ResourceBase
    klass.to_s.match(/^(?<labs>(.*::)?Labs::)?(.*?)?(?<resource_name>(\w)+)$/) do |matched|
      resource_symbol = OandaAPI::Utils.underscore(matched[:resource_name]).to_sym
      (@descendants ||= {})[resource_symbol] = klass
      (@lab_resources ||= []) << klass if matched[:labs]
    end
  end

  # @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

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

  # @private
  # Returns a class name if klass_symbol is the name of a class that
  # descends from {OandaAPI::ResourceBase}, otherwise nil.
  # @param [Symbol] klass_symbol class name of a resource class
  # @return [Class]
  # @return [Nil] if klass_symbol is not a class that descends from
  #  {OandaAPI::ResourceBase}.
  def self.descendant(klass_symbol)
    (@descendants || {})[klass_symbol]
  end
end

Class Method Details

.class_from_symbol(resource_symbol) ⇒ Nil, Class

Returns the class of a Resource if klass_symbol is the name of a resource, otherwise nil.

@example Example: ResourceBase.class_from_symbol(:transaction_history) => OandaAPI::Resource::TransactionHistory ResourceBase.class_from_symbol(:calendar_event) => OandaAPI::Resource::Labs::CalendarEvent

Parameters:

  • resource_symbol (Symbol)

    symbolized resource name

Returns:

  • (Nil)

    if resource_symbol is not a Resource.

  • (Class)

    if resource_symbol is identifies a Resource.



42
43
44
# File 'lib/oanda_api/resource_base.rb', line 42

def self.class_from_symbol(resource_symbol)
  descendant resource_symbol
end

.labs_resource?(klass) ⇒ Boolean

Tests whether a class is a Labs Resource.

Parameters:

  • klass (Class)

    the class to be tested.

Returns:

  • (Boolean)

    True if the class is a Labs Resource.



51
52
53
# File 'lib/oanda_api/resource_base.rb', line 51

def self.labs_resource?(klass)
  (@lab_resources ||=[]).include? klass
end

.pluralize(klass_symbol) ⇒ String

Returns a pluralized version of the resource class name

Parameters:

  • klass_symbol (Symbol)

Returns:

  • (String)

    the pluralized resource class name



62
63
64
# File 'lib/oanda_api/resource_base.rb', line 62

def self.pluralize(klass_symbol)
  NOT_PLURALIZED.include?(klass_symbol.to_sym) ? klass_symbol.to_s : OandaAPI::Utils.pluralize(klass_symbol)
end

Instance Method Details

#to_json(*args) ⇒ String

Serializes an instance as JSON.

Returns:

  • (String)

    a stringified JSON representation of an instance



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

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