gnarly 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gnarly/application.rb +3 -2
- data/lib/gnarly/client/emhttp.rb +36 -0
- data/lib/gnarly/client/patron.rb +33 -0
- data/lib/gnarly/client.rb +42 -0
- data/lib/gnarly/environment.rb +6 -0
- data/lib/gnarly/parameters.rb +32 -0
- data/lib/gnarly/request.rb +4 -2
- data/lib/gnarly/url.rb +28 -18
- metadata +28 -6
data/lib/gnarly/application.rb
CHANGED
@@ -22,7 +22,8 @@ module Gnarly
|
|
22
22
|
url
|
23
23
|
end
|
24
24
|
|
25
|
-
def helper(helper)
|
25
|
+
def helper(helper=nil, &block)
|
26
|
+
helper = Module.new &block if block and not helper
|
26
27
|
(@helpers ||= []) << helper
|
27
28
|
end
|
28
29
|
|
@@ -65,7 +66,7 @@ module Gnarly
|
|
65
66
|
environment.instance_exec *url.parameters, &before
|
66
67
|
end
|
67
68
|
keys = url.keys request.method
|
68
|
-
response = environment.instance_exec *url.parameters(
|
69
|
+
response = environment.instance_exec *url.parameters(keys, request.params), &@responder
|
69
70
|
if after = url.after
|
70
71
|
environment.instance_exec *url.parameters, &after
|
71
72
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'em-http'
|
2
|
+
|
3
|
+
require 'gnarly/client.rb'
|
4
|
+
|
5
|
+
module Gnarly
|
6
|
+
|
7
|
+
module Client
|
8
|
+
|
9
|
+
module EMHttp
|
10
|
+
|
11
|
+
def http(request, &block)
|
12
|
+
if block
|
13
|
+
method, url, headers, body = request
|
14
|
+
opts = {}
|
15
|
+
opts[:head] = headers if headers
|
16
|
+
case url
|
17
|
+
when Array
|
18
|
+
url, query = url
|
19
|
+
opts[:query] = query
|
20
|
+
end
|
21
|
+
http = EventMachine::HttpRequest.new(url).__send__(method, opts)
|
22
|
+
http.callback {
|
23
|
+
headers = Client.symbolize_headers http.response_header.to_hash
|
24
|
+
block.call [http.response_header.status, headers, http.response]
|
25
|
+
EventMachine.stop
|
26
|
+
}
|
27
|
+
else
|
28
|
+
raise :async_only
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'patron'
|
2
|
+
|
3
|
+
require 'gnarly/client.rb'
|
4
|
+
|
5
|
+
module Gnarly
|
6
|
+
|
7
|
+
module Client
|
8
|
+
|
9
|
+
module Patron
|
10
|
+
|
11
|
+
def http(request)
|
12
|
+
method, url, headers, body = request
|
13
|
+
session = ::Patron::Session.new
|
14
|
+
options = {}
|
15
|
+
options[:data] = body if body
|
16
|
+
r = session.request method, Client.create_url(url), headers, options
|
17
|
+
response = [r.status, Client.symbolize_headers(r.headers), r.body]
|
18
|
+
if block_given?
|
19
|
+
yield response
|
20
|
+
else
|
21
|
+
response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.http(request)
|
26
|
+
Object.new.extend(self).http request
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'addressable/uri'
|
3
|
+
|
4
|
+
module Gnarly
|
5
|
+
|
6
|
+
module Client
|
7
|
+
|
8
|
+
def self.symbolize_headers(headers)
|
9
|
+
symbolized = {}
|
10
|
+
headers.each_pair do |key, value|
|
11
|
+
sym = key.downcase.gsub('-', '_').to_sym
|
12
|
+
symbolized[sym] = value
|
13
|
+
end
|
14
|
+
symbolized
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.create_url(url)
|
18
|
+
case url
|
19
|
+
when Array
|
20
|
+
path, params = url
|
21
|
+
sio = StringIO.new
|
22
|
+
sio << path
|
23
|
+
if params.size > 0
|
24
|
+
key, value = params.shift
|
25
|
+
sio << "?" << key << "=" << value
|
26
|
+
params.each_pair do |key, value|
|
27
|
+
sio << "&" << key << "=" << value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
sio.string
|
31
|
+
else
|
32
|
+
url
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.url_encode(url)
|
37
|
+
Addressable::URI::encode url
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/gnarly/environment.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'gnarly/provides.rb'
|
2
2
|
require 'gnarly/understands.rb'
|
3
|
+
require 'gnarly/parameters.rb'
|
3
4
|
|
4
5
|
module Gnarly
|
5
6
|
|
@@ -16,6 +17,11 @@ module Gnarly
|
|
16
17
|
@application
|
17
18
|
end
|
18
19
|
|
20
|
+
def helper(helper=nil, &block)
|
21
|
+
helper = Module.new &block if block and not helper
|
22
|
+
extend helper
|
23
|
+
end
|
24
|
+
|
19
25
|
end
|
20
26
|
|
21
27
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'gnarly/base.rb'
|
2
|
+
require 'gnarly/status.rb'
|
3
|
+
|
4
|
+
module Gnarly
|
5
|
+
|
6
|
+
module Provides
|
7
|
+
include Base
|
8
|
+
include Status
|
9
|
+
|
10
|
+
def parameters(*names)
|
11
|
+
params = names.collect do |n|
|
12
|
+
name, type = n
|
13
|
+
p = request.params[name.to_s]
|
14
|
+
case type
|
15
|
+
when :integer, :int
|
16
|
+
if /^-?[0-9]+$/ =~ p
|
17
|
+
p.to_i
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
when :symbol
|
22
|
+
p.to_sym
|
23
|
+
else
|
24
|
+
p
|
25
|
+
end
|
26
|
+
end
|
27
|
+
yield *params
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/gnarly/request.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'query_string_parser'
|
2
2
|
|
3
3
|
module Gnarly
|
4
4
|
|
5
5
|
class Request
|
6
|
+
include QueryStringParser
|
7
|
+
|
6
8
|
attr_reader :state
|
7
9
|
|
8
10
|
def initialize(state)
|
@@ -41,7 +43,7 @@ module Gnarly
|
|
41
43
|
end
|
42
44
|
|
43
45
|
def params()
|
44
|
-
@params ||=
|
46
|
+
@params ||= qs_parse(@state["QUERY_STRING"])
|
45
47
|
end
|
46
48
|
|
47
49
|
private
|
data/lib/gnarly/url.rb
CHANGED
@@ -20,8 +20,23 @@ module Gnarly
|
|
20
20
|
@match != nil
|
21
21
|
end
|
22
22
|
|
23
|
-
def parameters(
|
24
|
-
if keys
|
23
|
+
def parameters(keys=nil, defaults=nil)
|
24
|
+
if keys
|
25
|
+
if @type_map
|
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
|
25
40
|
a = @match.to_a
|
26
41
|
a.shift
|
27
42
|
if @types
|
@@ -36,21 +51,15 @@ module Gnarly
|
|
36
51
|
else
|
37
52
|
a
|
38
53
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
else
|
50
|
-
keys.collect do |key|
|
51
|
-
@match[key]
|
52
|
-
end
|
53
|
-
end
|
54
|
+
end
|
55
|
+
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
|
54
63
|
end
|
55
64
|
end
|
56
65
|
|
@@ -66,7 +75,8 @@ module Gnarly
|
|
66
75
|
@allow.join " "
|
67
76
|
end
|
68
77
|
|
69
|
-
def helper(helper)
|
78
|
+
def helper(helper=nil, &block)
|
79
|
+
helper = Module.new &block if block and not helper
|
70
80
|
(@helpers ||= []) << helper
|
71
81
|
end
|
72
82
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gnarly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Kim Dalsgaard
|
@@ -14,10 +15,23 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-07-19 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: query_string_parser
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
21
35
|
description: Super simple DSL style web framework on top of Rack. Makes real HTTP'ish REST services easy.
|
22
36
|
email: kim@kimdalsgaard.com
|
23
37
|
executables: []
|
@@ -29,9 +43,13 @@ extra_rdoc_files: []
|
|
29
43
|
files:
|
30
44
|
- lib/gnarly/application.rb
|
31
45
|
- lib/gnarly/base.rb
|
46
|
+
- lib/gnarly/client/emhttp.rb
|
47
|
+
- lib/gnarly/client/patron.rb
|
48
|
+
- lib/gnarly/client.rb
|
32
49
|
- lib/gnarly/environment.rb
|
33
50
|
- lib/gnarly/haml.rb
|
34
51
|
- lib/gnarly/mimeparse.rb
|
52
|
+
- lib/gnarly/parameters.rb
|
35
53
|
- lib/gnarly/provides.rb
|
36
54
|
- lib/gnarly/request.rb
|
37
55
|
- lib/gnarly/status.rb
|
@@ -51,23 +69,27 @@ rdoc_options: []
|
|
51
69
|
require_paths:
|
52
70
|
- lib
|
53
71
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
54
73
|
requirements:
|
55
74
|
- - ">="
|
56
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
57
77
|
segments:
|
58
78
|
- 0
|
59
79
|
version: "0"
|
60
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
61
82
|
requirements:
|
62
83
|
- - ">="
|
63
84
|
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
64
86
|
segments:
|
65
87
|
- 0
|
66
88
|
version: "0"
|
67
89
|
requirements: []
|
68
90
|
|
69
91
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.3.
|
92
|
+
rubygems_version: 1.3.7
|
71
93
|
signing_key:
|
72
94
|
specification_version: 3
|
73
95
|
summary: Super simple web framework on top of Rack
|