Module: ActiveRestClient::ProxyBase::ClassMethods
- Defined in:
- lib/active_rest_client/proxy_base.rb
Instance Method Summary collapse
- #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
- #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
- #translate(result, options = {}) ⇒ Object
- #url(value = nil) ⇒ Object
Instance Method Details
#add_mapping(method_type, match, block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_rest_client/proxy_base.rb', line 25 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
39 40 41 42 |
# File 'lib/active_rest_client/proxy_base.rb', line 39 def body(value = nil) @body = value if value @body end |
#delete(match, &block) ⇒ Object
21 22 23 |
# File 'lib/active_rest_client/proxy_base.rb', line 21 def delete(match, &block) add_mapping(:delete, match, block) end |
#find_mapping_for_current_request ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_rest_client/proxy_base.rb', line 117 def find_mapping_for_current_request uri = URI.parse(@original_url) @mappings ||= [] @params = {} @mappings.each do |mapping| match = mapping.match if (match_data = uri.path.match(match)) && @request.http_method.to_sym == mapping.http_method matches = match_data.to_a matches.shift matches.each_with_index do |value, index| @params[mapping.param_keys[index]] = value end return mapping end end nil end |
#get(match, &block) ⇒ Object
9 10 11 |
# File 'lib/active_rest_client/proxy_base.rb', line 9 def get(match, &block) add_mapping(:get, match, block) end |
#get_params(value = nil) ⇒ Object
49 50 51 52 |
# File 'lib/active_rest_client/proxy_base.rb', line 49 def get_params(value = nil) @get_params = value if value @get_params end |
#handle(request, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_rest_client/proxy_base.rb', line 94 def handle(request, &block) @request = request @original_handler = block @original_body = request.body @body = @original_body.dup @original_get_params = request.get_params @get_params = @original_get_params.dup @original_post_params = request.post_params @post_params = (@original_post_params || {}).dup @original_url = request.url @url = @original_url.dup if mapping = find_mapping_for_current_request self.class_eval(&mapping.block) else passthrough end end |
#params(value = nil) ⇒ Object
59 60 61 62 |
# File 'lib/active_rest_client/proxy_base.rb', line 59 def params(value = nil) @params = value if value @params end |
#passthrough ⇒ Object
64 65 66 67 |
# File 'lib/active_rest_client/proxy_base.rb', line 64 def passthrough rebuild_request @original_handler.call(@request) end |
#post(match, &block) ⇒ Object
13 14 15 |
# File 'lib/active_rest_client/proxy_base.rb', line 13 def post(match, &block) add_mapping(:post, match, block) end |
#post_params(value = nil) ⇒ Object
54 55 56 57 |
# File 'lib/active_rest_client/proxy_base.rb', line 54 def post_params(value = nil) @post_params = value if value @post_params end |
#put(match, &block) ⇒ Object
17 18 19 |
# File 'lib/active_rest_client/proxy_base.rb', line 17 def put(match, &block) add_mapping(:put, match, block) end |
#rebuild_request ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_rest_client/proxy_base.rb', line 77 def rebuild_request if @url != @original_url @request.forced_url = @request.url = @url end if @body != @original_body @request.body = @body elsif @post_params != @original_post_params @request.body = nil @request.prepare_request_body(@post_params) end if @get_params != @original_get_params @request.get_params = @get_params @request.prepare_url @request.append_get_parameters end end |
#render(body, status = 200, content_type = "application/javascript", headers = {}) ⇒ Object
135 136 137 138 |
# File 'lib/active_rest_client/proxy_base.rb', line 135 def render(body, status=200, content_type="application/javascript", headers={}) headers["Content-type"] = content_type OpenStruct.new(body:body, status:status, headers:headers, proxied:true) end |
#translate(result, options = {}) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/active_rest_client/proxy_base.rb', line 69 def translate(result, = {}) result.headers["content-type"] = "application/hal+json" result = OpenStruct.new(status:result.status, headers:result.headers, body:result.body) obj = MultiJson.load(result.body) result.body = yield obj result end |
#url(value = nil) ⇒ Object
44 45 46 47 |
# File 'lib/active_rest_client/proxy_base.rb', line 44 def url(value = nil) @url = value if value @url end |