Class: IGMarkets::DealingPlatform::MarketMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/dealing_platform/market_methods.rb

Overview

Provides methods for working with markets. Returned by #markets.

Instance Method Summary collapse

Constructor Details

#initialize(dealing_platform) ⇒ MarketMethods

Initializes this helper class with the specified dealing platform.

Parameters:



8
9
10
# File 'lib/ig_markets/dealing_platform/market_methods.rb', line 8

def initialize(dealing_platform)
  @dealing_platform = WeakRef.new dealing_platform
end

Instance Method Details

#[](epic) ⇒ Market

Returns market details for the market with the specified EPIC, or ‘nil` if there is no market with that EPIC. Internally a call to #find is made.

Parameters:

  • epic (String)

    The EPIC of the market to return details for.

Returns:



65
66
67
# File 'lib/ig_markets/dealing_platform/market_methods.rb', line 65

def [](epic)
  find(epic).first
end

#find(*epics) ⇒ Array<Market>

Returns details for the markets with the passed EPICs.

Parameters:

  • epics (Array<String>)

    The EPICs of the markets to return details for.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ig_markets/dealing_platform/market_methods.rb', line 31

def find(*epics)
  epics = epics.flatten

  return [] if epics.empty?

  epics.each do |epic|
    raise ArgumentError, "invalid EPIC: #{epic}" unless Regex::EPIC.match?(epic.to_s)
  end

  # The API imposes a maximum of 50 EPICs per request
  markets = epics.each_slice(50).map do |epics_slice|
    @dealing_platform.session.get("markets?epics=#{epics_slice.join(',')}", API_V2).fetch :market_details
  end

  @dealing_platform.instantiate_models Market, markets.flatten
end

#hierarchy(node_id = nil) ⇒ MarketHierarchyResult

Returns details on the market hierarchy directly under the specified node.

Parameters:

  • node_id (String) (defaults to: nil)

    The ID of the node to return the market hierarchy for. If ‘nil` then details on the root node of the hierarchy will be returned.

Returns:



18
19
20
21
22
23
24
# File 'lib/ig_markets/dealing_platform/market_methods.rb', line 18

def hierarchy(node_id = nil)
  url = ['marketnavigation', node_id].compact.join '/'

  result = @dealing_platform.session.get url

  @dealing_platform.instantiate_models MarketHierarchyResult, result
end

#search(search_term) ⇒ Array<MarketOverview>

Searches markets using a search term and returns an array of results.

Parameters:

  • search_term (String)

    The search term to use.

Returns:



53
54
55
56
57
# File 'lib/ig_markets/dealing_platform/market_methods.rb', line 53

def search(search_term)
  result = @dealing_platform.session.get("markets?searchTerm=#{search_term}").fetch :markets

  @dealing_platform.instantiate_models MarketOverview, result
end