picky 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -222,8 +222,23 @@ class Application
222
222
  # Finalizes the routes.
223
223
  #
224
224
  def finalize # :nodoc:
225
+ check
225
226
  routing.freeze
226
227
  end
228
+ # Checks app for missing things.
229
+ #
230
+ # Warns if something is missing.
231
+ #
232
+ # TODO Good specs.
233
+ #
234
+ def check # :nodoc:
235
+ warnings = []
236
+ warnings << check_external_interface
237
+ puts "\n#{warnings.join(?\n)}\n\n" unless warnings.all? &:nil?
238
+ end
239
+ def check_external_interface
240
+ "WARNING: No routes defined for application configuration in #{self.class}." if routing.empty?
241
+ end
227
242
 
228
243
  # TODO Add more info if possible.
229
244
  #
data/lib/picky/cli.rb CHANGED
@@ -4,10 +4,6 @@ module Picky
4
4
  #
5
5
  class CLI # :nodoc:all
6
6
 
7
- def self.mapping
8
- @@mapping
9
- end
10
-
11
7
  # Execute a command.
12
8
  #
13
9
  # Note: By default, help is displayed. I.e. when no command is given.
@@ -26,6 +22,27 @@ module Picky
26
22
  params.map { |param| "<#{param}>" }.join(' ') if params
27
23
  end
28
24
  end
25
+ class Statistics < Base
26
+ def execute name, args, params
27
+ relative_log_file = args.shift
28
+ port = args.shift
29
+
30
+ usage(name, params) || exit(1) unless relative_log_file
31
+
32
+ ENV['PICKY_LOG_FILE'] = File.expand_path relative_log_file
33
+ ENV['PICKY_STATISTICS_PORT'] = port
34
+
35
+ begin
36
+ require 'picky-statistics'
37
+ rescue LoadError => e
38
+ require 'picky/extensions/object'
39
+ puts_gem_missing 'picky-statistics', 'the Picky statistics'
40
+ exit 1
41
+ end
42
+
43
+ require 'picky-statistics/application/app'
44
+ end
45
+ end
29
46
  class Generate < Base
30
47
  def execute name, args, params
31
48
  system "picky-generate #{args.join(' ')}"
@@ -50,8 +67,12 @@ module Picky
50
67
  #
51
68
  @@mapping = {
52
69
  :generate => [Generate, 'thing_to_generate: e.g. "unicorn_server"', :parameters],
53
- :help => [Help]
70
+ :help => [Help],
71
+ :stats => [Statistics, 'logfile_to_use: e.g. log/search.log', 'port (optional)']
54
72
  }
73
+ def self.mapping
74
+ @@mapping
75
+ end
55
76
 
56
77
  end
57
78
 
@@ -13,4 +13,10 @@ class Object # :nodoc:all
13
13
  puts text
14
14
  end
15
15
 
16
+ # Puts a text that informs the user of a missing gem.
17
+ #
18
+ def puts_gem_missing gem_name, message
19
+ puts "#{gem_name} gem missing!\nTo use #{message}, you need to:\n 1. Add the following line to Gemfile:\n gem '#{gem_name}'\n 2. Then, run:\n bundle update\n"
20
+ end
21
+
16
22
  end
data/lib/picky/routing.rb CHANGED
@@ -142,6 +142,12 @@ class Routing # :nodoc:all
142
142
  String === url ? %r{#{url}} : url
143
143
  end
144
144
 
145
+ # Returns true if there are no routes defined.
146
+ #
147
+ def empty?
148
+ routes.length.zero?
149
+ end
150
+
145
151
  # TODO Beautify.
146
152
  #
147
153
  def to_s
@@ -35,10 +35,58 @@ module Sources
35
35
  #
36
36
  class Couch < Base
37
37
 
38
+ # If your Couch DB uses UUID keys, use
39
+ # Sources::Couch.new(:title, keys: Sources::Couch::UUIDKeys.new)
40
+ # Do not forget to reconvert the UUID Key from an integer in the client:
41
+ # uuid = UUIDTools::UUID.parse_int(id)
42
+ # uuid.to_s
43
+ #
44
+ class UUIDKeys
45
+ def initialize
46
+ # Tries to require the uuidtools gem.
47
+ #
48
+ begin
49
+ require 'uuidtools'
50
+ rescue LoadError
51
+ puts_gem_missing 'uuidtools', 'UUID keys in a CouchDB source'
52
+ exit 1
53
+ end
54
+ end
55
+ def to_i id
56
+ uuid = UUIDTools::UUID.parse id
57
+ uuid.to_i
58
+ end
59
+ end
60
+
61
+ # If your Couch DB uses Hex keys, use
62
+ # Sources::Couch.new(:title, keys: Sources::Couch::HexKeys.new)
63
+ # Do not forget to reconvert the Hex Key from an integer in the client:
64
+ # id.to_s(16)
65
+ #
66
+ class HexKeys
67
+ def to_i id
68
+ id.hex
69
+ end
70
+ end
71
+
72
+ # If your Couch DB uses Integer keys, use
73
+ # Sources::Couch.new(:title, keys: Sources::Couch::IntegerKeys.new)
74
+ #
75
+ class IntegerKeys
76
+ def to_i id
77
+ id
78
+ end
79
+ end
80
+
81
+ #
82
+ #
38
83
  def initialize *category_names, options
39
84
  check_gem
85
+
40
86
  Hash === options && options[:url] || raise_no_db_given(category_names)
41
87
  @db = RestClient::Resource.new options.delete(:url), options
88
+
89
+ @to_i_strategy = options.delete(:keys) || HexKeys.new
42
90
  end
43
91
 
44
92
  # Tries to require the rest_client gem.
@@ -46,7 +94,7 @@ module Sources
46
94
  def check_gem # :nodoc:
47
95
  require 'rest_client'
48
96
  rescue LoadError
49
- puts "Rest-client gem missing!\nTo use the CouchDB source, you need to:\n 1. Add the following line to Gemfile:\n gem 'rest-client'\n 2. Then, run:\n bundle update\n"
97
+ puts_gem_missing 'rest-client', 'the CouchDB source'
50
98
  exit 1
51
99
  end
52
100
 
@@ -54,10 +102,11 @@ module Sources
54
102
  #
55
103
  # See important note, above.
56
104
  #
105
+ @@id_key = '_id'
57
106
  def harvest type, category
58
107
  category_name = category.from.to_s
59
108
  get_data do |doc|
60
- yield doc['_id'].hex, doc[category_name] || next
109
+ yield @to_i_strategy.to_i(doc[@@id_key]), doc[category_name] || next
61
110
  end
62
111
  end
63
112
 
@@ -20,7 +20,7 @@ module Sources
20
20
  def check_gem # :nodoc:
21
21
  require 'www/delicious'
22
22
  rescue LoadError
23
- puts "Delicious gem missing!\nTo use the delicious source, you need to:\n 1. Add the following line to Gemfile:\n gem 'www-delicious'\n 2. Then, run:\n bundle update\n"
23
+ puts_gem_missing 'www-delicious', 'the delicious source'
24
24
  exit 1
25
25
  end
26
26
 
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+
5
+ # We need to load the CLI file explicitly as the CLI is not loaded with the Loader (not needed in the server, only for script runs).
6
+ #
7
+ require File.expand_path '../../../lib/picky/cli', __FILE__
8
+
9
+ # TODO Finish this prototype Spec, redesign.
10
+ #
11
+ describe Picky::CLI do
12
+
13
+ describe Picky::CLI::Base do
14
+ before(:each) do
15
+ @executor = Picky::CLI::Base.new
16
+ end
17
+ describe 'usage' do
18
+ it 'calls puts with an usage' do
19
+ @executor.should_receive(:puts).once.with "Usage\n picky some_name <param1> <param 2 (optional)>"
20
+
21
+ @executor.usage :some_name, [:param1, 'param 2 (optional)']
22
+ end
23
+ end
24
+ describe 'params_to_s' do
25
+ it 'returns the right string' do
26
+ @executor.params_to_s([:param1, 'param 2 (optional)']).should == '<param1> <param 2 (optional)>'
27
+ end
28
+ end
29
+ end
30
+
31
+ end
@@ -23,4 +23,12 @@ describe Object do
23
23
  end
24
24
  end
25
25
 
26
+ describe 'puts_gem_missing' do
27
+ it 'should puts right' do
28
+ @object.should_receive(:puts).once.with "gnorf gem missing!\nTo use gnarble gnarf, you need to:\n 1. Add the following line to Gemfile:\n gem 'gnorf'\n 2. Then, run:\n bundle update\n"
29
+
30
+ @object.puts_gem_missing 'gnorf', 'gnarble gnarf'
31
+ end
32
+ end
33
+
26
34
  end
@@ -35,6 +35,25 @@ describe Routing do
35
35
  "rack.input"=>'' }
36
36
  end
37
37
 
38
+ context 'empty?' do
39
+ context 'no routes' do
40
+ before(:each) do
41
+ @routing.reset_routes
42
+ end
43
+ it 'returns the right answer' do
44
+ @routing.empty?.should == true
45
+ end
46
+ end
47
+ context 'with routes' do
48
+ before(:each) do
49
+ @routing.route %r{something} => :some_query
50
+ end
51
+ it 'returns the right answer' do
52
+ @routing.empty?.should == false
53
+ end
54
+ end
55
+ end
56
+
38
57
  context 'real routes' do
39
58
  before(:each) do
40
59
  @routing.reset_routes
@@ -2,33 +2,115 @@ require 'spec_helper'
2
2
 
3
3
  describe Sources::Couch do
4
4
 
5
- context "without database" do
6
- it "should fail correctly" do
7
- lambda { @source = Sources::Couch.new(:a, :b, :c) }.should raise_error(Sources::NoCouchDBGiven)
5
+ describe 'UUIDKeys' do
6
+ before(:each) do
7
+ @keys = Sources::Couch::UUIDKeys.new
8
+ end
9
+ it 'converts uuids' do
10
+ @keys.to_i('550e8400-e29b-41d4-a716-446655440000').should == 113059749145936325402354257176981405696
8
11
  end
9
12
  end
10
-
11
- context "with database" do
13
+ describe 'HexKeys' do
14
+ before(:each) do
15
+ @keys = Sources::Couch::HexKeys.new
16
+ end
17
+ it 'converts uuids' do
18
+ @keys.to_i('7f').should == 127
19
+ end
20
+ end
21
+ describe 'IntegerKeys' do
12
22
  before(:each) do
13
- @source = Sources::Couch.new :a, :b, :c, url: 'http://localhost:5984/picky'
14
- RestClient::Request.should_receive(:execute).any_number_of_times.and_return %{{"rows":[{"doc":{"_id":"7f","a":"a data","b":"b data","c":"c data"}}]}}
15
- end
16
-
17
- describe "harvest" do
18
- it "yields the right data" do
19
- field = stub :b, :from => :b
20
- @source.harvest :anything, field do |id, token|
21
- id.should eql(127)
22
- token.should eql('b data')
23
- end.should have(1).item
23
+ @keys = Sources::Couch::IntegerKeys.new
24
+ end
25
+ it 'converts uuids' do
26
+ @keys.to_i('123').should == '123'
27
+ end
28
+ end
29
+
30
+ describe 'special keys' do
31
+ context 'uuid keys' do
32
+ context "with database" do
33
+ before(:each) do
34
+ @source = Sources::Couch.new :a, :b, :c, url: 'http://localhost:5984/picky', keys: Sources::Couch::UUIDKeys.new
35
+ RestClient::Request.should_receive(:execute).any_number_of_times.and_return %{{"rows":[{"doc":{"_id":"550e8400-e29b-41d4-a716-446655440000","a":"a data","b":"b data","c":"c data"}}]}}
36
+ end
37
+
38
+ describe "harvest" do
39
+ it "yields the right data" do
40
+ field = stub :b, :from => :b
41
+ @source.harvest :anything, field do |id, token|
42
+ id.should eql(113059749145936325402354257176981405696)
43
+ token.should eql('b data')
44
+ end.should have(1).item
45
+ end
46
+ end
47
+
48
+ describe "get_data" do
49
+ it "yields each line" do
50
+ @source.get_data do |data|
51
+ data.should == { "_id" => "550e8400-e29b-41d4-a716-446655440000", "a" => "a data", "b" => "b data", "c" => "c data" }
52
+ end.should have(1).item
53
+ end
54
+ end
24
55
  end
25
56
  end
26
-
27
- describe "get_data" do
28
- it "yields each line" do
29
- @source.get_data do |data|
30
- data.should == { "_id" => "7f", "a" => "a data", "b" => "b data", "c" => "c data" }
31
- end.should have(1).item
57
+ context 'integer keys' do
58
+ context "with database" do
59
+ before(:each) do
60
+ @source = Sources::Couch.new :a, :b, :c, url: 'http://localhost:5984/picky', keys: Sources::Couch::IntegerKeys.new
61
+ RestClient::Request.should_receive(:execute).any_number_of_times.and_return %{{"rows":[{"doc":{"_id":"123","a":"a data","b":"b data","c":"c data"}}]}}
62
+ end
63
+
64
+ describe "harvest" do
65
+ it "yields the right data" do
66
+ field = stub :b, :from => :b
67
+ @source.harvest :anything, field do |id, token|
68
+ id.should eql('123')
69
+ token.should eql('b data')
70
+ end.should have(1).item
71
+ end
72
+ end
73
+
74
+ describe "get_data" do
75
+ it "yields each line" do
76
+ @source.get_data do |data|
77
+ data.should == { "_id" => "123", "a" => "a data", "b" => "b data", "c" => "c data" }
78
+ end.should have(1).item
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ context 'default keys' do
86
+ context "without database" do
87
+ it "should fail correctly" do
88
+ lambda { @source = Sources::Couch.new(:a, :b, :c) }.should raise_error(Sources::NoCouchDBGiven)
89
+ end
90
+ end
91
+
92
+ context "with database" do
93
+ before(:each) do
94
+ @source = Sources::Couch.new :a, :b, :c, url: 'http://localhost:5984/picky'
95
+ RestClient::Request.should_receive(:execute).any_number_of_times.and_return %{{"rows":[{"doc":{"_id":"7f","a":"a data","b":"b data","c":"c data"}}]}}
96
+ end
97
+
98
+ describe "harvest" do
99
+ it "yields the right data" do
100
+ field = stub :b, :from => :b
101
+ @source.harvest :anything, field do |id, token|
102
+ id.should eql(127)
103
+ token.should eql('b data')
104
+ end.should have(1).item
105
+ end
106
+ end
107
+
108
+ describe "get_data" do
109
+ it "yields each line" do
110
+ @source.get_data do |data|
111
+ data.should == { "_id" => "7f", "a" => "a data", "b" => "b data", "c" => "c data" }
112
+ end.should have(1).item
113
+ end
32
114
  end
33
115
  end
34
116
  end
@@ -15,7 +15,7 @@ describe Sources::Delicious do
15
15
  @source.should_receive(:require).any_number_of_times.and_raise LoadError
16
16
  end
17
17
  it "puts & exits" do
18
- @source.should_receive(:puts).once.with "Delicious gem missing!\nTo use the delicious source, you need to:\n 1. Add the following line to Gemfile:\n gem 'www-delicious'\n 2. Then, run:\n bundle update\n"
18
+ @source.should_receive(:puts).once.with "www-delicious gem missing!\nTo use the delicious source, you need to:\n 1. Add the following line to Gemfile:\n gem 'www-delicious'\n 2. Then, run:\n bundle update\n"
19
19
  @source.should_receive(:exit).once.with 1
20
20
 
21
21
  @source.check_gem
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 0
9
- version: 1.2.0
8
+ - 1
9
+ version: 1.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-17 00:00:00 +01:00
17
+ date: 2010-12-20 00:00:00 +01:00
18
18
  default_executable: picky
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -161,6 +161,7 @@ files:
161
161
  - spec/lib/cacher/weights_generator_spec.rb
162
162
  - spec/lib/calculations/location_spec.rb
163
163
  - spec/lib/character_substituters/west_european_spec.rb
164
+ - spec/lib/cli_spec.rb
164
165
  - spec/lib/configuration/index_spec.rb
165
166
  - spec/lib/cores_spec.rb
166
167
  - spec/lib/extensions/array_spec.rb
@@ -267,6 +268,7 @@ test_files:
267
268
  - spec/lib/cacher/weights_generator_spec.rb
268
269
  - spec/lib/calculations/location_spec.rb
269
270
  - spec/lib/character_substituters/west_european_spec.rb
271
+ - spec/lib/cli_spec.rb
270
272
  - spec/lib/configuration/index_spec.rb
271
273
  - spec/lib/cores_spec.rb
272
274
  - spec/lib/extensions/array_spec.rb