Class: RestCore::Builder

Inherits:
Object
  • Object
show all
Includes:
RestCore
Defined in:
lib/rest-core/builder.rb

Constant Summary

Constant Summary

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from RestCore

eagerload, id

Constructor Details

- (Builder) initialize(&block)



19
20
21
22
23
# File 'lib/rest-core/builder.rb', line 19

def initialize &block
  @engine    = nil
  @middles ||= []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

- (Object) default_engine



27
28
29
# File 'lib/rest-core/builder.rb', line 27

def default_engine
  @default_engine ||= self.class.default_engine
end

- (Object) middles (readonly)

Returns the value of attribute middles



25
26
27
# File 'lib/rest-core/builder.rb', line 25

def middles
  @middles
end

Instance Method Details

- (Object) members



39
40
41
42
43
44
45
46
47
48
# File 'lib/rest-core/builder.rb', line 39

def members
  middles.map{ |(middle, args, block)|
    if middle.public_method_defined?(:wrapped)
      # TODO: this is hacky... try to avoid calling new!
      middle.members + middle.new(Dry.new, *args, &block).members
    else
      middle.members
    end if middle.respond_to?(:members)
  }.flatten.compact
end

- (Object) run(engine)



35
36
37
# File 'lib/rest-core/builder.rb', line 35

def run engine
  @engine = engine
end

- (Object) to_app(engine = @engine || default_engine)



50
51
52
53
54
55
56
57
58
59
# File 'lib/rest-core/builder.rb', line 50

def to_app engine=@engine || default_engine
  # === foldr m.new app middles
  middles.reverse.inject(engine.new){ |app, (middle, args, block)|
    begin
      middle.new(app, *partial_deep_copy(args), &block)
    rescue ArgumentError => e
      raise ArgumentError.new("#{middle}: #{e}")
    end
  }
end

- (Object) to_client(*attrs)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rest-core/builder.rb', line 61

def to_client *attrs
  fields = members + attrs
  struct = build_struct(fields)
  client = Class.new(struct)
  client.const_set('Struct', struct)
  class_methods = build_class_methods
  client.const_set('ClassMethods', class_methods)
  client.singleton_class.send(:include, class_methods)
  client.send(:include, Client)
  client.builder            = self
  client.pool_size          =  0 # default to no pool
  client.pool_idle_time     = 60 # default to 60 seconds
  client.event_source_class = EventSource
  client.promises           = []
  client.mutex              = Mutex.new
  client
end

- (Object) use(middle, *args, &block)



31
32
33
# File 'lib/rest-core/builder.rb', line 31

def use middle, *args, &block
  middles << [middle, args, block]
end