Class: RestCore::Builder

Inherits:
Object
  • Object
show all
Includes:
RestCore, Wrapper
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

Attributes included from Wrapper

#default_engine, #middles, #wrapped

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Wrapper

included, #members, partial_deep_copy, #run, #to_app, #use

Methods included from RestCore

eagerload, id

Constructor Details

- (Builder) initialize(&block)



93
94
95
96
97
# File 'lib/rest-core/builder.rb', line 93

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

Class Method Details

+ (Object) client(*attrs, &block)



14
15
16
# File 'lib/rest-core/builder.rb', line 14

def self.client *attrs, &block
  new(&block).to_client(*attrs)
end

+ (Object) default_engine



10
11
12
# File 'lib/rest-core/builder.rb', line 10

def self.default_engine
  @default_engine ||= RestCore::HttpClient
end

Instance Method Details

- (Object) build_class_methods



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
# File 'lib/rest-core/builder.rb', line 44

def build_class_methods
  Module.new do
    attr_accessor :builder, :pool_size, :pool_idle_time,
                  :event_source_class, :promises, :mutex

    def inherited sub
      sub.builder            = builder
      sub.pool_size          = pool_size
      sub.pool_idle_time     = pool_idle_time
      sub.event_source_class = event_source_class
      sub.promises           = []
      sub.mutex              = Mutex.new
    end

    def thread_pool; RestCore::ThreadPool[self]; end

    def give_promise weak_promise, ps=promises, m=mutex
      m.synchronize do
        ps << weak_promise
        ps.keep_if(&:weakref_alive?)
      end
    end

    # Shutdown the thread pool for this client and wait for all requests
    def shutdown
      thread_pool.shutdown
      wait
    end

    # Wait for all the requests to be done for this client
    def wait ps=promises, m=mutex
      return self if ps.empty?
      current_promises = nil
      m.synchronize do
        current_promises = ps.dup
        ps.clear
      end
      current_promises.each do |p|
        next unless p.weakref_alive?
        begin
          p.wait
        rescue WeakRef::RefError # it's gc'ed after we think it's alive
        end
      end
      wait(ps, m)
    end
  end
end

- (Object) build_struct(fields)



36
37
38
39
40
41
42
# File 'lib/rest-core/builder.rb', line 36

def build_struct fields
  if fields.empty?
    Struct.new(nil)
  else
    Struct.new(*fields.uniq)
  end
end

- (Object) to_client(*attrs)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rest-core/builder.rb', line 18

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