Class: Barrister::Rails::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/barrister-rails.rb

Defined Under Namespace

Classes: BaseEtherealModel, InterfaceProxy

Constant Summary collapse

DEFAULT_BARRISTER_TYPES =
['bool', 'int', 'string', 'float']

Instance Method Summary collapse

Constructor Details

#initialize(transport_or_uri_or_handler, json_path = nil) ⇒ Client

Returns a new instance of Client.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/barrister-rails.rb', line 99

def initialize(transport_or_uri_or_handler, json_path=nil)
  if transport_or_uri_or_handler.is_a?(String)
    transport = Barrister::HttpTransport.new(transport_or_uri_or_handler)
  elsif transport_or_uri_or_handler.class.to_s.split('::').first == 'Barrister'
    transport = transport_or_uri_or_handler
  else
    raise 'json_path must be provided if registering a handler directly' unless json_path
    container = Barrister::IntraProcessContainer.new json_path, transport_or_uri_or_handler
    transport = Barrister::IntraProcessTransport.new container
  end

  @client = Barrister::Client.new(transport)
  @custom_types = Hash.new

  interfaces = @client
    .instance_variable_get('@contract')
    .interfaces

  pairs = interfaces
    .map { |iface| iface.functions }
    .flatten
    .map { |fx| [fx.name.to_sym, { type: fx.returns['type'], is_array: fx.returns['is_array'] } ] }

   = Hash[pairs]

  @interface_proxies = interfaces.map { |iface| InterfaceProxy.new iface.name, @client,  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



127
128
129
130
# File 'lib/barrister-rails.rb', line 127

def method_missing(name, *args)
  name_as_string = name.to_s
  @interface_proxies.find { |iface_proxy| iface_proxy.name == name_as_string }
end