gnarly 0.0.5 → 0.0.6
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.
- data/lib/gnarly/application.rb +6 -5
- data/lib/gnarly/client/emhttp.rb +3 -3
- data/lib/gnarly/client/patron.rb +4 -3
- data/lib/gnarly/client/test.rb +19 -0
- data/lib/gnarly/client/typhoeus.rb +38 -0
- data/lib/gnarly/client.rb +4 -2
- data/lib/gnarly/environment.rb +8 -0
- data/lib/gnarly/static.rb +26 -0
- data/lib/gnarly/status.rb +3 -2
- data/lib/gnarly/url.rb +19 -31
- metadata +7 -4
data/lib/gnarly/application.rb
CHANGED
@@ -65,17 +65,18 @@ module Gnarly
|
|
65
65
|
if before = url.before
|
66
66
|
environment.instance_exec *url.parameters, &before
|
67
67
|
end
|
68
|
-
|
69
|
-
response = environment.
|
68
|
+
response = environment.instance_exec *url.parameters(request.method), &@responder
|
69
|
+
@response = environment.response ? environment.response : response
|
70
70
|
if after = url.after
|
71
71
|
environment.instance_exec *url.parameters, &after
|
72
72
|
end
|
73
|
-
|
73
|
+
if @response.is_a? Array and @response.size == 3
|
74
|
+
@response[2] = [@response[2]] unless @response[2].respond_to? :each
|
75
|
+
@response
|
76
|
+
else
|
74
77
|
name, line = @responder.source_location
|
75
78
|
msg = "Bad return type for block in #{name} line #{line}"
|
76
79
|
internal_server_error msg
|
77
|
-
else
|
78
|
-
response
|
79
80
|
end
|
80
81
|
else
|
81
82
|
method_not_allowed url.allow
|
data/lib/gnarly/client/emhttp.rb
CHANGED
@@ -8,7 +8,7 @@ module Gnarly
|
|
8
8
|
|
9
9
|
module EMHttp
|
10
10
|
|
11
|
-
def http(request, &block)
|
11
|
+
def http(request, options={}, &block)
|
12
12
|
if block
|
13
13
|
method, url, headers, body = request
|
14
14
|
opts = {}
|
@@ -18,11 +18,11 @@ module Gnarly
|
|
18
18
|
url, query = url
|
19
19
|
opts[:query] = query
|
20
20
|
end
|
21
|
-
http = EventMachine::HttpRequest.new(url).__send__(method, opts)
|
21
|
+
http = EventMachine::HttpRequest.new(url).__send__(method, opts)
|
22
|
+
http.stream &options[:stream] if options[:stream]
|
22
23
|
http.callback {
|
23
24
|
headers = Client.symbolize_headers http.response_header.to_hash
|
24
25
|
block.call [http.response_header.status, headers, http.response]
|
25
|
-
EventMachine.stop
|
26
26
|
}
|
27
27
|
else
|
28
28
|
raise :async_only
|
data/lib/gnarly/client/patron.rb
CHANGED
@@ -8,9 +8,10 @@ module Gnarly
|
|
8
8
|
|
9
9
|
module Patron
|
10
10
|
|
11
|
-
def http(request)
|
11
|
+
def http(request, opts={})
|
12
12
|
method, url, headers, body = request
|
13
13
|
session = ::Patron::Session.new
|
14
|
+
session.timeout = opts[:timeout] if opts[:timeout]
|
14
15
|
options = {}
|
15
16
|
options[:data] = body if body
|
16
17
|
r = session.request method, Client.create_url(url), headers, options
|
@@ -22,8 +23,8 @@ module Gnarly
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
|
-
def self.http(request)
|
26
|
-
Object.new.extend(self).http request
|
26
|
+
def self.http(request, opts={})
|
27
|
+
Object.new.extend(self).http request, opts
|
27
28
|
end
|
28
29
|
|
29
30
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
|
3
|
+
require 'gnarly/client.rb'
|
4
|
+
|
5
|
+
module Gnarly
|
6
|
+
|
7
|
+
module Client
|
8
|
+
|
9
|
+
module Typhoeus
|
10
|
+
|
11
|
+
def http(request, opts={})
|
12
|
+
method, url, headers, body = request
|
13
|
+
options = {:headers => headers}
|
14
|
+
case url
|
15
|
+
when Array
|
16
|
+
url, query = url
|
17
|
+
options[:params] = query
|
18
|
+
end
|
19
|
+
options[:body] = body if body
|
20
|
+
options[:timeout] = opts[:timeout] * 1000 if opts[:timeout]
|
21
|
+
r = ::Typhoeus::Request.send method, url, options
|
22
|
+
response = [r.code, Client.symbolize_headers(r.headers_hash), r.body]
|
23
|
+
if block_given?
|
24
|
+
yield response
|
25
|
+
else
|
26
|
+
response
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.http(request, opts={})
|
31
|
+
Object.new.extend(self).http request, opts
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/lib/gnarly/client.rb
CHANGED
@@ -8,8 +8,10 @@ module Gnarly
|
|
8
8
|
def self.symbolize_headers(headers)
|
9
9
|
symbolized = {}
|
10
10
|
headers.each_pair do |key, value|
|
11
|
-
|
12
|
-
|
11
|
+
if value
|
12
|
+
sym = key.downcase.gsub('-', '_').to_sym
|
13
|
+
symbolized[sym] = value
|
14
|
+
end
|
13
15
|
end
|
14
16
|
symbolized
|
15
17
|
end
|
data/lib/gnarly/environment.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
module Gnarly
|
3
|
+
|
4
|
+
class Static
|
5
|
+
|
6
|
+
def initialize(app, options={})
|
7
|
+
@app = app
|
8
|
+
@urls = options[:urls] || ["/favicon.ico"]
|
9
|
+
root = options[:root] || Dir.pwd
|
10
|
+
@file_server = Rack::File.new(root)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
path = env["PATH_INFO"]
|
15
|
+
can_serve = @urls.any? { |url| path.index(url) == 0 }
|
16
|
+
|
17
|
+
if can_serve
|
18
|
+
@file_server.call(env)
|
19
|
+
else
|
20
|
+
@app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/gnarly/status.rb
CHANGED
@@ -15,9 +15,10 @@ module Gnarly
|
|
15
15
|
@default_content_types.last if @default_content_types
|
16
16
|
end
|
17
17
|
|
18
|
-
def ok(body, mime=nil)
|
18
|
+
def ok(body, mime=nil, charset=nil)
|
19
19
|
mime = default_content_type unless mime
|
20
|
-
|
20
|
+
charset = body.encoding.name.downcase unless charset
|
21
|
+
headers = mime ? {"Content-type" => "#{mime}; charset=#{charset}"} : {}
|
21
22
|
[200, headers, [body]]
|
22
23
|
end
|
23
24
|
|
data/lib/gnarly/url.rb
CHANGED
@@ -20,23 +20,9 @@ module Gnarly
|
|
20
20
|
@match != nil
|
21
21
|
end
|
22
22
|
|
23
|
-
def parameters(
|
24
|
-
|
25
|
-
|
26
|
-
keys.collect do |key|
|
27
|
-
case @type_map[key.to_s]
|
28
|
-
when :integer
|
29
|
-
fetch_match(key, defaults).to_i
|
30
|
-
else
|
31
|
-
fetch_match key, defaults
|
32
|
-
end
|
33
|
-
end
|
34
|
-
else
|
35
|
-
keys.collect do |key|
|
36
|
-
fetch_match key, defaults
|
37
|
-
end
|
38
|
-
end
|
39
|
-
else
|
23
|
+
def parameters(method)
|
24
|
+
keys = @keys[method]
|
25
|
+
if keys.empty?
|
40
26
|
a = @match.to_a
|
41
27
|
a.shift
|
42
28
|
if @types
|
@@ -51,26 +37,28 @@ module Gnarly
|
|
51
37
|
else
|
52
38
|
a
|
53
39
|
end
|
40
|
+
else
|
41
|
+
if @type_map
|
42
|
+
keys.collect do |key|
|
43
|
+
case @type_map[key.to_s]
|
44
|
+
when :integer
|
45
|
+
@match[key].to_i
|
46
|
+
else
|
47
|
+
@match[key]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
else
|
51
|
+
keys.collect do |key|
|
52
|
+
@match[key]
|
53
|
+
end
|
54
|
+
end
|
54
55
|
end
|
55
56
|
end
|
56
|
-
|
57
|
-
def fetch_match(key, defaults=nil)
|
58
|
-
#TODO: Is this a hack?
|
59
|
-
begin
|
60
|
-
@match[key]
|
61
|
-
rescue IndexError
|
62
|
-
defaults ? defaults[key.to_s] : nil
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
57
|
+
|
66
58
|
def responder(method)
|
67
59
|
@responders[method]
|
68
60
|
end
|
69
61
|
|
70
|
-
def keys(method)
|
71
|
-
@keys[method]
|
72
|
-
end
|
73
|
-
|
74
62
|
def allow()
|
75
63
|
@allow.join " "
|
76
64
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gnarly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kim Dalsgaard
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-31 01:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -45,6 +45,8 @@ files:
|
|
45
45
|
- lib/gnarly/base.rb
|
46
46
|
- lib/gnarly/client/emhttp.rb
|
47
47
|
- lib/gnarly/client/patron.rb
|
48
|
+
- lib/gnarly/client/test.rb
|
49
|
+
- lib/gnarly/client/typhoeus.rb
|
48
50
|
- lib/gnarly/client.rb
|
49
51
|
- lib/gnarly/environment.rb
|
50
52
|
- lib/gnarly/haml.rb
|
@@ -52,6 +54,7 @@ files:
|
|
52
54
|
- lib/gnarly/parameters.rb
|
53
55
|
- lib/gnarly/provides.rb
|
54
56
|
- lib/gnarly/request.rb
|
57
|
+
- lib/gnarly/static.rb
|
55
58
|
- lib/gnarly/status.rb
|
56
59
|
- lib/gnarly/system.rb
|
57
60
|
- lib/gnarly/typhoeus.rb
|