octarine 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,7 +43,8 @@ module Octarine # :nodoc:
43
43
  #
44
44
  # Adds block as a handler for when no path is matched.
45
45
 
46
- [:add, :get, :post, :delete, :put, :default].each do |method|
46
+ methods = [:add, :get, :post, :delete, :put, :options, :default]
47
+ methods.each do |method|
47
48
  define_method(method) do |*args, &block|
48
49
  if Hash === args.last
49
50
  restrictions = Array(args.last[:restrict])
@@ -144,11 +145,12 @@ module Octarine # :nodoc:
144
145
  end
145
146
  end
146
147
 
147
- def register_handler(method, *args, &block)
148
+ def register_handler(method, path="/", opts={}, &block)
148
149
  return register_default(&block) if method == :default
149
- restrictions = Hash === args[-1] ? Array(args[-1].delete(:restrict)) : []
150
+ restrictions = Array(opts.delete(:restrict))
150
151
  restrictions.map! {|name| @restrictions[name]}
151
- route = router.send(method, *args)
152
+ opts[:request_method] = method unless method == :add
153
+ route = router.add(path, opts)
152
154
  route.to do |env|
153
155
  env.merge!("router.route" => route.original_path)
154
156
  request = request_class.new(env)
@@ -75,7 +75,9 @@ module Octarine # :nodoc:
75
75
  #
76
76
  def to(client, to_path=path, to_input=input)
77
77
  client = SimpleHTTP.new(client.to_str) if client.respond_to?(:to_str)
78
- res = if %W{POST PUT}.include?(method)
78
+ res = if method == "OPTIONS" && client.respond_to?(:run_request)
79
+ client.run_request(:options, to_path, nil, header_for_rerequest)
80
+ elsif %W{POST PUT}.include?(method)
79
81
  client.__send__(method.downcase, to_path, to_input, header_for_rerequest)
80
82
  else
81
83
  client.__send__(method.downcase, to_path, header_for_rerequest)
@@ -24,14 +24,14 @@ module Octarine # :nodoc:
24
24
  end
25
25
 
26
26
  # :call-seq: response.update {|body| block } -> response
27
- # response.update(path) {|value| block } -> response
27
+ # response.update(path[, opts]) {|value| block } -> response
28
28
  #
29
29
  # Called without an argument, the block will be supplied the response body,
30
30
  # and the response body will be set to the result of the block. The response
31
31
  # itself is returned.
32
32
  #
33
- # When called with an argument the body should be a hash, the body will be
34
- # traversed accoring to the path supplied, the value of the body will be
33
+ # When called with a path argument the body should be a hash, the body will
34
+ # be traversed accoring to the path supplied, the value of the body will be
35
35
  # yielded to the block, and then replaced with the result of the block.
36
36
  # Example:
37
37
  # response.body
@@ -42,11 +42,48 @@ module Octarine # :nodoc:
42
42
  # response.body
43
43
  # #=> {"data" => [{"user" => {"id" => "1234", ...}, "message" => "..."}]}
44
44
  #
45
- def update(path=nil, &block)
45
+ # Additional options can be passed as a hash, the options available are:
46
+ # [remove] The full path to the element that should be removed if the
47
+ # block returns nil. Must be a parent of the element targeted
48
+ # by the main path argument
49
+ # [remove_if] If supplied along with the remove option the result of the
50
+ # block will be tested againt this value (using ===) rather
51
+ # than nil
52
+ # [link] Should be supplied with a value of a hash, in which the key
53
+ # is a path to an element to be updated, and the value is an
54
+ # array of a an element and a method from which to derive a
55
+ # value
56
+ # Example:
57
+ # response.body
58
+ # #=> {"data" => [1, 2, 3], "total" => 3}
59
+ #
60
+ # user_names = {1 => "Arthur", 2 => "Ford"}
61
+ #
62
+ # total_to_length = {"total" => ["data", :length]}
63
+ # response.update("data.", remove: "data.", link: total_to_length) do |id|
64
+ # user_names[id]
65
+ # end
66
+ #
67
+ # response.body
68
+ # # {"data" => ["Arthur", "Ford"], "total" => 2}
69
+ #
70
+ def update(path=nil, options={}, &block)
46
71
  @body = if body.respond_to?(:to_ary) && path.nil?
47
72
  block.call(body)
48
73
  else
49
- apply(body, path, &block)
74
+ path = nil if path == "."
75
+ remove_path = options[:remove]
76
+ remove_if = remove_path ? options[:remove_if] : -> x {false}
77
+ apply(body, path, remove_path, remove_if, &block)
78
+ end
79
+ (options[:link] || []).each do |dest, (source_path, source_method)|
80
+ update(dest) do |val|
81
+ source = body
82
+ source_path.split(".").each do |part|
83
+ source = source[part]
84
+ end
85
+ source.send(source_method)
86
+ end
50
87
  end
51
88
  self
52
89
  end
@@ -89,19 +126,39 @@ module Octarine # :nodoc:
89
126
 
90
127
  private
91
128
 
92
- def apply(object, path=nil, &block)
129
+ def apply(object, path=nil, remove_path=nil, remove_flag=nil, &block)
93
130
  if object.respond_to?(:to_ary)
94
- path = nil if path == "."
95
- return object.to_ary.map {|obj| apply(obj, path, &block)}
131
+ return object.to_ary.each_with_object([]) do |obj, collection|
132
+ result = catch :remove do
133
+ apply(obj, path, remove_path, remove_flag, &block)
134
+ end
135
+ if result != :__remove__
136
+ collection << result
137
+ elsif remove_path.nil?
138
+ throw :remove, :__remove__
139
+ end
140
+ end
96
141
  end
97
142
 
143
+ remove_key, remove_rest = remove_path.split(".", 2) if remove_path
144
+
98
145
  key, rest = path.split(".", 2) if path
99
146
  if rest
100
- object[key] = apply(object[key], rest, &block)
147
+ object[key] = apply(object[key], rest, remove_rest, remove_flag, &block)
101
148
  elsif key
102
- object[key] = block.call(object[key])
149
+ result = block.call(object[key])
150
+ should_remove = remove_flag === result
151
+ if should_remove && key == remove_path
152
+ object.delete(key)
153
+ elsif should_remove
154
+ throw :remove, :__remove__
155
+ else
156
+ object[key] = result
157
+ end
103
158
  else
104
- return block.call(object)
159
+ result = block.call(object)
160
+ throw :remove, :__remove__ if remove_flag === result
161
+ return result
105
162
  end
106
163
  object
107
164
  end
@@ -28,7 +28,7 @@ module Octarine # :nodoc:
28
28
  # #headers, and #body
29
29
  #
30
30
  def head(path, headers={})
31
- request(Net::HTTP::Head.new(path, headers))
31
+ run_request(:head, path, nil, headers)
32
32
  end
33
33
 
34
34
  # :call-seq: simple_http.get(path, headers={}) -> response
@@ -37,7 +37,7 @@ module Octarine # :nodoc:
37
37
  # #headers, and #body
38
38
  #
39
39
  def get(path, headers={})
40
- request(Net::HTTP::Get.new(path, headers))
40
+ run_request(:get, path, nil, headers)
41
41
  end
42
42
 
43
43
  # :call-seq: simple_http.post(path, body=nil, headers={}) -> response
@@ -46,9 +46,7 @@ module Octarine # :nodoc:
46
46
  # #headers, and #body
47
47
  #
48
48
  def post(path, body=nil, headers={})
49
- req = Net::HTTP::Post.new(path, headers)
50
- req.body = body if body
51
- request(req)
49
+ run_request(:post, path, body, headers)
52
50
  end
53
51
 
54
52
  # :call-seq: simple_http.put(path, body=nil, headers={}) -> response
@@ -57,9 +55,7 @@ module Octarine # :nodoc:
57
55
  # #headers, and #body
58
56
  #
59
57
  def put(path, body=nil, headers={})
60
- req = Net::HTTP::Put.new(path, headers)
61
- req.body = body if body
62
- request(req)
58
+ run_request(:put, path, body, headers)
63
59
  end
64
60
 
65
61
  # :call-seq: simple_http.delete(path, headers={}) -> response
@@ -68,7 +64,28 @@ module Octarine # :nodoc:
68
64
  # #headers, and #body
69
65
  #
70
66
  def delete(path, headers={})
71
- request(Net::HTTP::Delete.new(path, headers))
67
+ run_request(:delete, path, nil, headers)
68
+ end
69
+
70
+ # :call-seq: simple_http.options(path, headers={}) -> response
71
+ #
72
+ # Perform an OPTIONS request, returns a response that responds to #status,
73
+ # #headers, and #body
74
+ #
75
+ def options(path, headers)
76
+ run_request(:options, path, nil, headers)
77
+ end
78
+
79
+ # :call-seq: simple_http.run_request(method, path, body, headers) -> res
80
+ #
81
+ # Perform request, returns a response that responds to #status, #headers,
82
+ # and #body
83
+ #
84
+ def run_request(method, path, body, headers)
85
+ klass = Net::HTTP.const_get(method.to_s.capitalize)
86
+ req.klass.new(path, headers)
87
+ req.body = body if body
88
+ request(req)
72
89
  end
73
90
 
74
91
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octarine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-07 00:00:00.000000000 Z
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: http_router
16
- requirement: &2152395640 !ruby/object:Gem::Requirement
16
+ requirement: &2154507480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152395640
24
+ version_requirements: *2154507480
25
25
  description: Sinatra-like DSL for writing a HTTP routing proxy.
26
26
  email: mat@sourcetagsandcodes.com
27
27
  executables: []