Module: RestBuilder::Client

Defined in:
lib/rest-builder/client.rb

Constant Summary collapse

Unserializable =
[Proc, Method, IO]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



45
46
47
# File 'lib/rest-builder/client.rb', line 45

def app
  @app
end

#dryObject (readonly)

Returns the value of attribute dry.



45
46
47
# File 'lib/rest-builder/client.rb', line 45

def dry
  @dry
end

#error_callbackObject

Returns the value of attribute error_callback.



46
47
48
# File 'lib/rest-builder/client.rb', line 46

def error_callback
  @error_callback
end

#promisesObject (readonly)

Returns the value of attribute promises.



45
46
47
# File 'lib/rest-builder/client.rb', line 45

def promises
  @promises
end

Class Method Details

.included(mod) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rest-builder/client.rb', line 13

def self.included mod
  # honor default attributes
  src = mod.members.map{ |name|
    <<-RUBY
      def #{name}
        if (r = super).nil?
          self.#{name} = default_#{name}
        else
          r
        end
      end

      def default_#{name} a=app
        if self.class.respond_to?("default_#{name}")
          self.class.default_#{name}      # old class default style
        elsif a.respond_to?(:#{name})
          a.#{name}({})                 # middleware instance value
        elsif a.respond_to?(:app)
          default_#{name}(a.app)        # walk into next app
        else
          nil
        end
      end
      private :default_#{name}
    RUBY
  }
  accessor = Module.new
  accessor.module_eval(src.join("\n"), __FILE__, __LINE__)
  mod.const_set('Accessor', accessor)
  mod.send(:include, accessor)
end

Instance Method Details

#attributesObject



57
58
59
# File 'lib/rest-builder/client.rb', line 57

def attributes
  Hash[each_pair.map{ |k, v| [k, send(k)] }]
end

#build_env(env = {}) ⇒ Object



194
195
196
197
# File 'lib/rest-builder/client.rb', line 194

def build_env env={}
  default_env.merge(
    Middleware.string_keys(attributes).merge(Middleware.string_keys(env)))
end

#default_envObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/rest-builder/client.rb', line 199

def default_env
  {REQUEST_METHOD  => :get,
   REQUEST_PATH    => '/' ,
   REQUEST_QUERY   => {}  ,
   REQUEST_PAYLOAD => {}  ,
   REQUEST_HEADERS => {}  ,
   FAIL            => []  ,
   LOG             => []  ,
   CLIENT          => self}
end

#delete(path, query = {}, opts = {}, &cb) ⇒ Object



108
109
110
111
112
113
# File 'lib/rest-builder/client.rb', line 108

def delete path, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :delete,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  }.merge(opts), &cb)
end

#event_source(path, query = {}, opts = {}) ⇒ Object



155
156
157
# File 'lib/rest-builder/client.rb', line 155

def event_source path, query={}, opts={}
  self.class.event_source_class.new(self, path, query, opts)
end

#get(path, query = {}, opts = {}, &cb) ⇒ Object



101
102
103
104
105
106
# File 'lib/rest-builder/client.rb', line 101

def get    path, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :get   ,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  }.merge(opts), &cb)
end

#give_promise(response) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rest-builder/client.rb', line 181

def give_promise response
  # under ASYNC callback, response might not be a response hash
  # in that case (maybe in a user created engine), Client#wait
  # won't work because we have no way to track the promise.
  if response.kind_of?(Hash) && response[PROMISE]
    weak_promise = WeakRef.new(response[PROMISE])
    self.class.give_promise(weak_promise)
    self.class.give_promise(weak_promise, promises, mutex)
  end

  response
end

#head(path, query = {}, opts = {}, &cb) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/rest-builder/client.rb', line 115

def head path, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :head  ,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  ,
     RESPONSE_KEY    => RESPONSE_HEADERS}.merge(opts), &cb)
end

#initialize(o = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rest-builder/client.rb', line 47

def initialize o={}
  @app ||= self.class.builder.to_app # lighten! would reinitialize anyway
  @dry ||= self.class.builder.to_app(Dry)
  @promises = []  # don't record any promises in lighten!
  @mutex    = nil # for locking promises, lazily initialized
                  # for serialization
  @error_callback = nil
  o.each{ |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end

#inspectObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/rest-builder/client.rb', line 61

def inspect
  fields = if size > 0
             attributes.map{ |k, v|
               "#{k}=#{v.inspect.sub(/(?<=.{12}).{4,}/, '...')}"
             }.join(', ')
           else
             ''
           end
  "#<struct #{self.class.name}#{fields}>"
end

#lighten(o = {}) ⇒ Object



85
86
87
# File 'lib/rest-builder/client.rb', line 85

def lighten o={}
  dup.lighten!(o)
end

#lighten!(o = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rest-builder/client.rb', line 72

def lighten! o={}
  attributes.each{ |k, v| vv = case v;
                                 when  Hash; lighten_hash(v)
                                 when Array; lighten_array(v)
                                 when *Unserializable; nil
                                 else v
                               end
                          send("#{k}=", vv)}
  initialize(o)
  @app, @dry = lighten_app(app), lighten_app(dry)
  self
end

#options(path, query = {}, opts = {}, &cb) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/rest-builder/client.rb', line 123

def options path, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :options,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  ,
     RESPONSE_KEY    => RESPONSE_HEADERS}.merge(opts), &cb)
end

#patch(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/rest-builder/client.rb', line 147

def patch  path, payload={}, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :patch ,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  ,
     REQUEST_PAYLOAD => payload}.merge(opts), &cb)
end

#post(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/rest-builder/client.rb', line 131

def post   path, payload={}, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :post  ,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  ,
     REQUEST_PAYLOAD => payload}.merge(opts), &cb)
end

#put(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/rest-builder/client.rb', line 139

def put    path, payload={}, query={}, opts={}, &cb
  request(
    {REQUEST_METHOD  => :put   ,
     REQUEST_PATH    => path   ,
     REQUEST_QUERY   => query  ,
     REQUEST_PAYLOAD => payload}.merge(opts), &cb)
end

#request(env, a = app) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/rest-builder/client.rb', line 159

def request env, a=app
  if block_given?
    request_full(env, a){ |response| yield(response[response_key(env)]) }
  else
    request_full(env, a)[response_key(env)]
  end
end

#request_full(env, a = app, &k) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rest-builder/client.rb', line 167

def request_full env, a=app, &k
  response = a.call(build_env({ASYNC => !!k}.merge(env))) do |res|
    (k || :itself.to_proc).call(request_complete(res))
  end

  give_promise(response)

  if block_given?
    self
  else
    response
  end
end

#url(path, query = {}, opts = {}) ⇒ Object



94
95
96
97
98
99
# File 'lib/rest-builder/client.rb', line 94

def url path, query={}, opts={}
  dry.call(build_env({
    REQUEST_PATH  => path,
    REQUEST_QUERY => query,
    DRY           => true}.merge(opts)), &Middleware.method(:request_uri))
end

#waitObject



89
90
91
92
# File 'lib/rest-builder/client.rb', line 89

def wait
  self.class.wait(promises, mutex)
  self
end