Module: RestCore::Client

Includes:
RestCore
Defined in:
lib/rest-core/client.rb

Constant Summary collapse

Unserializable =
[Proc, Method, IO]

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, Simple, TIMER, Universal, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestCore

eagerload, id

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#dryObject (readonly)

Returns the value of attribute dry.



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

def dry
  @dry
end

#promisesObject (readonly)

Returns the value of attribute promises.



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

def promises
  @promises
end

Class Method Details

.included(mod) ⇒ Object



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

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} app=app
        if self.class.respond_to?("default_#{name}")
          self.class.default_#{name}      # old class default style
        elsif app.respond_to?(:#{name})
          app.#{name}({})                 # middleware instance value
        elsif app.respond_to?(:wrapped)
          default_#{name}(app.wrapped) || # wrapper value
          default_#{name}(app.app)        # walk into it
        elsif app.respond_to?(:app)
          default_#{name}(app.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



56
57
58
# File 'lib/rest-core/client.rb', line 56

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

#build_env(env = {}) ⇒ Object



186
187
188
189
# File 'lib/rest-core/client.rb', line 186

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

#default_envObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/rest-core/client.rb', line 191

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



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

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



154
155
156
# File 'lib/rest-core/client.rb', line 154

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

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



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

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

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



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

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
# File 'lib/rest-core/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
  o.each{ |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end

#inspectObject



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

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



84
85
86
# File 'lib/rest-core/client.rb', line 84

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

#lighten!(o = {}) ⇒ Object



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

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



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

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



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

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



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

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



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

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, app = app) ⇒ Object



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

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

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rest-core/client.rb', line 166

def request_full env, app=app, &k
  response = app.call(build_env({ASYNC => !!k}.merge(env)),
                      &(k || RC.id))

  # 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].kind_of?(Promise)
    weak_promise = WeakRef.new(response[PROMISE])
    self.class.give_promise(weak_promise)
    self.class.give_promise(weak_promise, promises, mutex)
  end

  if block_given?
    self
  else
    response
  end
end

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



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

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



88
89
90
91
# File 'lib/rest-core/client.rb', line 88

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