suggester 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/suggester/server.rb +48 -8
  3. data/suggester.gemspec +2 -2
  4. metadata +30 -22
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.3
@@ -16,6 +16,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'handlers.rb'))
16
16
  module Suggester
17
17
  # Core server class
18
18
  class Server < Sinatra::Base
19
+ ACCEPTED_FORMATS = ['yml', 'json', 'html']
19
20
 
20
21
  # Create an instance of the server. At this time, we spawn a separate thread
21
22
  # that will reload handlers as needed to prevent locking the server thread.
@@ -49,37 +50,45 @@ module Suggester
49
50
  output << data.map{|r| r.inspect}.join('<br/>')
50
51
  output << "</body></html>"
51
52
  else
52
- "BAD FORMAT"
53
+ invalid_format
53
54
  end
54
55
  end
55
56
 
56
57
  # find exact matches for the query string
57
- get "/:handler/match/:query.:format" do
58
- format = params.delete("format")
58
+ get "/:handler/match/*" do
59
59
  handler = params.delete("handler")
60
+ format, params[:query] = parse_format_and_query_from_splat(params)
60
61
  matches = self.class.handler(handler).match(params)
61
62
  case(format)
62
63
  when 'yml'
63
64
  matches.to_yaml
64
65
  when 'json'
65
66
  matches.to_json
67
+ when 'html'
68
+ output = "<html><body>"
69
+ output << matches.map{|r| r.inspect}.join('<br/>')
70
+ output << "</body></html>"
66
71
  else
67
- matches.inspect
72
+ invalid_format
68
73
  end
69
74
  end
70
75
 
71
76
  # find matches that begin with the query string
72
- get "/:handler/find/:query.:format" do
73
- format = params.delete("format")
77
+ get "/:handler/find/*" do
74
78
  handler = params.delete("handler")
79
+ format, params[:query] = parse_format_and_query_from_splat(params)
75
80
  matches = self.class.handler(handler).find(params)
76
81
  case(format)
77
82
  when 'yml'
78
83
  matches.to_yaml
79
84
  when 'json'
80
85
  matches.to_json
86
+ when 'html'
87
+ output = "<html><body>"
88
+ output << matches.map{|r| r.inspect}.join('<br/>')
89
+ output << "</body></html>"
81
90
  else
82
- matches.inspect
91
+ invalid_format
83
92
  end
84
93
  end
85
94
 
@@ -100,8 +109,12 @@ module Suggester
100
109
  response.to_yaml
101
110
  when 'json'
102
111
  response.to_json
112
+ when 'html'
113
+ output = "<html><body>"
114
+ output << response.map{|r| r.inspect}.join('<br/>')
115
+ output << "</body></html>"
103
116
  else
104
- response.inspect
117
+ invalid_format
105
118
  end
106
119
  end
107
120
 
@@ -124,6 +137,33 @@ module Suggester
124
137
 
125
138
  private
126
139
 
140
+ def parse_format_and_query_from_splat(params)
141
+ # get the splat param(s) - Array of arrays.
142
+ splat = params.delete("splat")
143
+ return nil, nil unless splat.kind_of?(Array)
144
+
145
+ # were only expecting one splat, get the first one and split it on period
146
+ splat = splat.first.split(".")
147
+
148
+ # format is the last token
149
+ format = splat.pop
150
+
151
+ # If format isn't acceptable, push it back onto splat and set format to nil
152
+ unless ACCEPTED_FORMATS.include?(format)
153
+ splat.push(format)
154
+ format = nil
155
+ end
156
+ # join the array of of splat as query
157
+ query = splat.join(".")
158
+
159
+ # return format and query
160
+ return format, query
161
+ end
162
+
163
+ def invalid_format(message = "Invalid Format")
164
+ [ 404, {'Content-Type' => 'text/plain'}, message ]
165
+ end
166
+
127
167
  def spawn_refresh_thread! #:nodoc:
128
168
  Thread.new do
129
169
  loop do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{suggester}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeff Ching"]
12
- s.date = %q{2011-04-04}
12
+ s.date = %q{2011-06-17}
13
13
  s.default_executable = %q{suggester_server}
14
14
  s.description = %q{Extensible, cache-based auto-suggest server for ruby. Includes refresh and replication support out of the box.}
15
15
  s.email = %q{ching.jeff@gmail.com}
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: suggester
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 29
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ - 3
10
+ version: 0.1.3
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jeff Ching
@@ -14,91 +15,97 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-04-04 00:00:00 -07:00
18
+ date: 2011-06-17 00:00:00 -07:00
18
19
  default_executable: suggester_server
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
22
+ prerelease: false
21
23
  name: sinatra
22
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
25
  none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
33
+ requirement: *id001
30
34
  type: :runtime
31
- prerelease: false
32
- version_requirements: *id001
33
35
  - !ruby/object:Gem::Dependency
36
+ prerelease: false
34
37
  name: json
35
- requirement: &id002 !ruby/object:Gem::Requirement
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
36
39
  none: false
37
40
  requirements:
38
41
  - - ">="
39
42
  - !ruby/object:Gem::Version
43
+ hash: 3
40
44
  segments:
41
45
  - 0
42
46
  version: "0"
47
+ requirement: *id002
43
48
  type: :runtime
44
- prerelease: false
45
- version_requirements: *id002
46
49
  - !ruby/object:Gem::Dependency
50
+ prerelease: false
47
51
  name: vegas
48
- requirement: &id003 !ruby/object:Gem::Requirement
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
49
53
  none: false
50
54
  requirements:
51
55
  - - ">="
52
56
  - !ruby/object:Gem::Version
57
+ hash: 3
53
58
  segments:
54
59
  - 0
55
60
  version: "0"
61
+ requirement: *id003
56
62
  type: :runtime
57
- prerelease: false
58
- version_requirements: *id003
59
63
  - !ruby/object:Gem::Dependency
64
+ prerelease: false
60
65
  name: bundler
61
- requirement: &id004 !ruby/object:Gem::Requirement
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
62
67
  none: false
63
68
  requirements:
64
69
  - - ~>
65
70
  - !ruby/object:Gem::Version
71
+ hash: 23
66
72
  segments:
67
73
  - 1
68
74
  - 0
69
75
  - 0
70
76
  version: 1.0.0
77
+ requirement: *id004
71
78
  type: :development
72
- prerelease: false
73
- version_requirements: *id004
74
79
  - !ruby/object:Gem::Dependency
80
+ prerelease: false
75
81
  name: jeweler
76
- requirement: &id005 !ruby/object:Gem::Requirement
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
77
83
  none: false
78
84
  requirements:
79
85
  - - ~>
80
86
  - !ruby/object:Gem::Version
87
+ hash: 1
81
88
  segments:
82
89
  - 1
83
90
  - 5
84
91
  - 1
85
92
  version: 1.5.1
93
+ requirement: *id005
86
94
  type: :development
87
- prerelease: false
88
- version_requirements: *id005
89
95
  - !ruby/object:Gem::Dependency
96
+ prerelease: false
90
97
  name: rcov
91
- requirement: &id006 !ruby/object:Gem::Requirement
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
92
99
  none: false
93
100
  requirements:
94
101
  - - ">="
95
102
  - !ruby/object:Gem::Version
103
+ hash: 3
96
104
  segments:
97
105
  - 0
98
106
  version: "0"
107
+ requirement: *id006
99
108
  type: :development
100
- prerelease: false
101
- version_requirements: *id006
102
109
  description: Extensible, cache-based auto-suggest server for ruby. Includes refresh and replication support out of the box.
103
110
  email: ching.jeff@gmail.com
104
111
  executables:
@@ -151,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
158
  requirements:
152
159
  - - ">="
153
160
  - !ruby/object:Gem::Version
154
- hash: -1297736483607632370
161
+ hash: 3
155
162
  segments:
156
163
  - 0
157
164
  version: "0"
@@ -160,6 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
167
  requirements:
161
168
  - - ">="
162
169
  - !ruby/object:Gem::Version
170
+ hash: 3
163
171
  segments:
164
172
  - 0
165
173
  version: "0"