Class: Collins::AssetClient

Inherits:
Object
  • Object
show all
Defined in:
lib/collins/asset_client.rb

Overview

Convenience class for making collins calls for only a single asset

Instance Method Summary collapse

Constructor Details

#initialize(asset, client, logger) ⇒ AssetClient

Returns a new instance of AssetClient.



6
7
8
9
10
11
12
13
14
15
# File 'lib/collins/asset_client.rb', line 6

def initialize asset, client, logger
  @asset = asset
  if asset.is_a?(Collins::Asset) then
    @tag = asset.tag
  else
    @tag = asset
  end
  @client = client
  @logger = logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Note:

this method should never be called directly

Fill in the missing asset parameter on the dynamic method if needed

If Client responds to the method, and the method requires an ‘asset_or_tag`, we insert the asset specified during initialization into the args array. If the method does not require an `asset_or_tag`, we simply proxy the method call as is. If Client does not respond to the method, we defer to `super`.

Examples:

collins_client.get('some_tag')            # => returns that asset
collins_client.with_asset('some_tag').get # => returns that same asset


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/collins/asset_client.rb', line 33

def method_missing meth, *args, &block
  if @client.respond_to?(meth) then
    method_parameters = @client.class.instance_method(meth).parameters
    asset_idx = method_parameters.find_index do |item|
      item[1] == :asset_or_tag
    end
    if asset_idx.nil? then
      @client.send(meth, *args, &block)
    else
      args_with_asset = args.insert(asset_idx, @tag)
      logger.debug("Doing #{meth}(#{args_with_asset.join(',')}) for #{@tag}")
      @client.send(meth, *args_with_asset, &block)
    end
  else
    super
  end
end

Instance Method Details

#respond_to?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/collins/asset_client.rb', line 51

def respond_to? meth, include_private = false
  @client.respond_to?(meth)
end

#to_sObject



17
18
19
# File 'lib/collins/asset_client.rb', line 17

def to_s
  "AssetClient(asset = #{@tag}, client = #{@client})"
end