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
-
#attributes ⇒ Object
-
#build_env(env = {}) ⇒ Object
-
#default_env ⇒ Object
-
#delete(path, query = {}, opts = {}, &cb) ⇒ Object
-
#event_source(path, query = {}, opts = {}) ⇒ Object
-
#get(path, query = {}, opts = {}, &cb) ⇒ Object
-
#give_promise(response) ⇒ Object
-
#head(path, query = {}, opts = {}, &cb) ⇒ Object
-
#initialize(o = {}) ⇒ Object
-
#inspect ⇒ Object
-
#lighten(o = {}) ⇒ Object
-
#lighten!(o = {}) ⇒ Object
-
#options(path, query = {}, opts = {}, &cb) ⇒ Object
-
#patch(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object
-
#post(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object
-
#put(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object
-
#request(env, a = app) ⇒ Object
-
#request_full(env, a = app, &k) ⇒ Object
-
#url(path, query = {}, opts = {}) ⇒ Object
-
#wait ⇒ Object
Methods included from RestCore
eagerload, id
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
43
44
45
|
# File 'lib/rest-core/client.rb', line 43
def app
@app
end
|
#dry ⇒ Object
Returns the value of attribute dry.
43
44
45
|
# File 'lib/rest-core/client.rb', line 43
def dry
@dry
end
|
#error_callback ⇒ Object
Returns the value of attribute error_callback.
44
45
46
|
# File 'lib/rest-core/client.rb', line 44
def error_callback
@error_callback
end
|
#promises ⇒ Object
Returns the value of attribute promises.
43
44
45
|
# File 'lib/rest-core/client.rb', line 43
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
|
# File 'lib/rest-core/client.rb', line 11
def self.included mod
src = mod.members.map{ |name|
" def \#{name}\n if (r = super).nil?\n self.\#{name} = default_\#{name}\n else\n r\n end\n end\n\n def default_\#{name} a=app\n if self.class.respond_to?(\"default_\#{name}\")\n self.class.default_\#{name} # old class default style\n elsif a.respond_to?(:\#{name})\n a.\#{name}({}) # middleware instance value\n elsif a.respond_to?(:app)\n default_\#{name}(a.app) # walk into next app\n else\n nil\n end\n end\n private :default_\#{name}\n RUBY\n }\n accessor = Module.new\n accessor.module_eval(src.join(\"\\n\"), __FILE__, __LINE__)\n mod.const_set('Accessor', accessor)\n mod.send(:include, accessor)\nend\n"
|
Instance Method Details
#attributes ⇒ Object
55
56
57
|
# File 'lib/rest-core/client.rb', line 55
def attributes
Hash[each_pair.map{ |k, v| [k, send(k)] }]
end
|
#build_env(env = {}) ⇒ Object
192
193
194
195
|
# File 'lib/rest-core/client.rb', line 192
def build_env env={}
default_env.merge(
Middleware.string_keys(attributes).merge(Middleware.string_keys(env)))
end
|
#default_env ⇒ Object
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/rest-core/client.rb', line 197
def default_env
{REQUEST_METHOD => :get,
REQUEST_PATH => '/' ,
REQUEST_QUERY => {} ,
REQUEST_PAYLOAD => {} ,
=> {} ,
FAIL => [] ,
LOG => [] ,
CLIENT => self}
end
|
#delete(path, query = {}, opts = {}, &cb) ⇒ Object
106
107
108
109
110
111
|
# File 'lib/rest-core/client.rb', line 106
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
153
154
155
|
# File 'lib/rest-core/client.rb', line 153
def event_source path, query={}, opts={}
self.class.event_source_class.new(self, path, query, opts)
end
|
#get(path, query = {}, opts = {}, &cb) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/rest-core/client.rb', line 99
def get path, query={}, opts={}, &cb
request(
{REQUEST_METHOD => :get ,
REQUEST_PATH => path ,
REQUEST_QUERY => query }.merge(opts), &cb)
end
|
#give_promise(response) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/rest-core/client.rb', line 179
def give_promise response
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
response
end
|
#head(path, query = {}, opts = {}, &cb) ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/rest-core/client.rb', line 113
def head path, query={}, opts={}, &cb
request(
{REQUEST_METHOD => :head ,
REQUEST_PATH => path ,
REQUEST_QUERY => query ,
RESPONSE_KEY => }.merge(opts), &cb)
end
|
#initialize(o = {}) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/rest-core/client.rb', line 45
def initialize o={}
@app ||= self.class.builder.to_app
@dry ||= self.class.builder.to_app(Dry)
@promises = []
@mutex = nil
@error_callback = nil
o.each{ |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end
|
#inspect ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/rest-core/client.rb', line 59
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
83
84
85
|
# File 'lib/rest-core/client.rb', line 83
def lighten o={}
dup.lighten!(o)
end
|
#lighten!(o = {}) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rest-core/client.rb', line 70
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
121
122
123
124
125
126
127
|
# File 'lib/rest-core/client.rb', line 121
def options path, query={}, opts={}, &cb
request(
{REQUEST_METHOD => :options,
REQUEST_PATH => path ,
REQUEST_QUERY => query ,
RESPONSE_KEY => }.merge(opts), &cb)
end
|
#patch(path, payload = {}, query = {}, opts = {}, &cb) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/rest-core/client.rb', line 145
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
129
130
131
132
133
134
135
|
# File 'lib/rest-core/client.rb', line 129
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
137
138
139
140
141
142
143
|
# File 'lib/rest-core/client.rb', line 137
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
157
158
159
160
161
162
163
|
# File 'lib/rest-core/client.rb', line 157
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
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/rest-core/client.rb', line 165
def request_full env, a=app, &k
response = a.call(build_env({ASYNC => !!k}.merge(env))) do |res|
(k || RC.id).call(request_complete(res))
end
give_promise(response)
if block_given?
self
else
response
end
end
|
#url(path, query = {}, opts = {}) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/rest-core/client.rb', line 92
def url path, query={}, opts={}
dry.call(build_env({
REQUEST_PATH => path,
REQUEST_QUERY => query,
DRY => true}.merge(opts)), &Middleware.method(:request_uri))
end
|
#wait ⇒ Object
87
88
89
90
|
# File 'lib/rest-core/client.rb', line 87
def wait
self.class.wait(promises, mutex)
self
end
|