Module: Flexirest::ProxyBase::ClassMethods
- Defined in:
- lib/flexirest/proxy_base.rb
Instance Method Summary collapse
-
#_proxy_state ⇒ Object
Per-request state used while handling a proxied request is stored in thread-local storage (keyed by the proxy class) rather than on the class itself.
- #add_mapping(method_type, match, block) ⇒ Object
- #body(value = nil) ⇒ Object
- #delete(match, &block) ⇒ Object
- #find_mapping_for_current_request ⇒ Object
- #get(match, &block) ⇒ Object
- #get_params(value = nil) ⇒ Object
- #handle(request, &block) ⇒ Object
- #params(value = nil) ⇒ Object
- #passthrough ⇒ Object
- #patch(match, &block) ⇒ Object
- #post(match, &block) ⇒ Object
- #post_params(value = nil) ⇒ Object
- #put(match, &block) ⇒ Object
- #rebuild_request ⇒ Object
- #render(body, status = 200, content_type = "application/javascript", headers = {}) ⇒ Object
- #result_is_json_or_unspecified?(result) ⇒ Boolean
- #translate(result, options = {}) ⇒ Object
- #url(value = nil) ⇒ Object
Instance Method Details
#_proxy_state ⇒ Object
Per-request state used while handling a proxied request is stored in thread-local storage (keyed by the proxy class) rather than on the class itself. The DSL is implemented as class methods run via class_eval, so historically "the current request" was stored in class instance variables/class variables, which are shared across every thread calling through the same proxy class. That caused request data to be dropped or corrupted under concurrency. Keeping it per-thread isolates concurrent requests while preserving the existing DSL.
14 15 16 17 |
# File 'lib/flexirest/proxy_base.rb', line 14 def _proxy_state states = (Thread.current[:flexirest_proxy_state] ||= {}) states[self] ||= {} end |
#add_mapping(method_type, match, block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/flexirest/proxy_base.rb', line 38 def add_mapping(method_type, match, block) @mappings ||= [] if match.is_a?(String) && (param_keys = match.scan(/:\w+/)) && param_keys.any? param_keys.each do |key| match.gsub!(key, "([^/]+)") end param_keys = param_keys.map {|k| k.gsub(":", "").to_sym} match = Regexp.new(match) end @mappings << OpenStruct.new(http_method:method_type, match:match, block:block, param_keys:param_keys) end |
#body(value = nil) ⇒ Object
52 53 54 55 |
# File 'lib/flexirest/proxy_base.rb', line 52 def body(value = nil) _proxy_state[:body] = value if value _proxy_state[:body] end |
#delete(match, &block) ⇒ Object
34 35 36 |
# File 'lib/flexirest/proxy_base.rb', line 34 def delete(match, &block) add_mapping(:delete, match, block) end |
#find_mapping_for_current_request ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/flexirest/proxy_base.rb', line 161 def find_mapping_for_current_request state = _proxy_state uri = URI.parse(state[:original_url]) @mappings ||= [] state[:params] = {} @mappings.each do |mapping| match = mapping.match if (match_data = uri.path.match(match)) && state[:request].http_method.to_sym == mapping.http_method matches = match_data.to_a matches.shift matches.each_with_index do |value, index| state[:params][mapping.param_keys[index]] = value end return mapping end end nil end |
#get(match, &block) ⇒ Object
18 19 20 |
# File 'lib/flexirest/proxy_base.rb', line 18 def get(match, &block) add_mapping(:get, match, block) end |
#get_params(value = nil) ⇒ Object
62 63 64 65 |
# File 'lib/flexirest/proxy_base.rb', line 62 def get_params(value = nil) _proxy_state[:get_params] = value if value _proxy_state[:get_params] end |
#handle(request, &block) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/flexirest/proxy_base.rb', line 124 def handle(request, &block) # Preserve any state already in flight on this thread (e.g. a proxied # request that itself triggers another request through the same proxy # class) and restore it when we're done, so nested calls can't clobber # each other. states = (Thread.current[:flexirest_proxy_state] ||= {}) previous_state = states[self] state = states[self] = {} state[:request] = request state[:original_handler] = block state[:original_body] = request.body state[:body] = state[:original_body].dup state[:original_get_params] = request.get_params state[:get_params] = state[:original_get_params].dup state[:original_post_params] = request.post_params state[:post_params] = (state[:original_post_params] || {}).dup state[:original_url] = request.url state[:url] = state[:original_url].dup if mapping = find_mapping_for_current_request self.class_eval(&mapping.block) else passthrough end ensure if previous_state states[self] = previous_state else states.delete(self) end end |
#params(value = nil) ⇒ Object
72 73 74 75 |
# File 'lib/flexirest/proxy_base.rb', line 72 def params(value = nil) _proxy_state[:params] = value if value _proxy_state[:params] end |
#passthrough ⇒ Object
77 78 79 80 81 |
# File 'lib/flexirest/proxy_base.rb', line 77 def passthrough rebuild_request state = _proxy_state state[:original_handler].call(state[:request]) end |
#patch(match, &block) ⇒ Object
30 31 32 |
# File 'lib/flexirest/proxy_base.rb', line 30 def patch(match, &block) add_mapping(:patch, match, block) end |
#post(match, &block) ⇒ Object
22 23 24 |
# File 'lib/flexirest/proxy_base.rb', line 22 def post(match, &block) add_mapping(:post, match, block) end |
#post_params(value = nil) ⇒ Object
67 68 69 70 |
# File 'lib/flexirest/proxy_base.rb', line 67 def post_params(value = nil) _proxy_state[:post_params] = value if value _proxy_state[:post_params] end |
#put(match, &block) ⇒ Object
26 27 28 |
# File 'lib/flexirest/proxy_base.rb', line 26 def put(match, &block) add_mapping(:put, match, block) end |
#rebuild_request ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/flexirest/proxy_base.rb', line 105 def rebuild_request state = _proxy_state request = state[:request] if state[:url] != state[:original_url] request.forced_url = request.url = state[:url] end if state[:body] != state[:original_body] request.body = state[:body] elsif state[:post_params] != state[:original_post_params] request.body = nil request.prepare_request_body(state[:post_params]) end if state[:get_params] != state[:original_get_params] request.get_params = state[:get_params] request.prepare_url request.append_get_parameters end end |
#render(body, status = 200, content_type = "application/javascript", headers = {}) ⇒ Object
180 181 182 183 |
# File 'lib/flexirest/proxy_base.rb', line 180 def render(body, status=200, content_type="application/javascript", headers={}) headers["Content-type"] = content_type FaradayResponseProxy.new(OpenStruct.new(body:body, status:status, response_headers:headers, proxied:true)) end |
#result_is_json_or_unspecified?(result) ⇒ Boolean
83 84 85 86 87 |
# File 'lib/flexirest/proxy_base.rb', line 83 def result_is_json_or_unspecified?(result) result.headers['Content-Type'].include?('json') rescue true end |
#translate(result, options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/flexirest/proxy_base.rb', line 89 def translate(result, = {}) incoming_content_type = result.headers['Content-Type'] if result_is_json_or_unspecified?(result) result.headers["content-type"] = "application/hal+json" end result = FaradayResponseProxy.new(OpenStruct.new(status:result.status, response_headers:result.headers, body:result.body)) if result.body.present? if incoming_content_type && incoming_content_type["xml"] result.body = yield Crack::XML.parse(result.body) else result.body = yield MultiJson.load(result.body) end end result end |
#url(value = nil) ⇒ Object
57 58 59 60 |
# File 'lib/flexirest/proxy_base.rb', line 57 def url(value = nil) _proxy_state[:url] = value if value _proxy_state[:url] end |