Skip to content

Commit

Permalink
[SEARCH] added ability to specify limit and page for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
bramswenson committed Feb 15, 2011
1 parent 7bbf52a commit 281f2cb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
4 changes: 4 additions & 0 deletions bin/vimpack
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ when 'search'
opt :game, 'Search for game scripts', :short => '-g'
opt :plugin, 'Search for plugin scripts', :short => '-p'
opt :patch, 'Search for patch scripts', :short => '-a'
opt :limit, 'Limit the number of scripts returned', :short => '-l',
:default => 100
opt :page, 'Page number to get (used with --limit for large result sets)',
:short => '-n', :default => 1
end
Vimpack::Commands::Search.run(options, ARGV)
when 'install'
Expand Down
48 changes: 35 additions & 13 deletions features/commands/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Feature: Search for a vim script
When I run "vimpack search rails"
Then the output should contain:
"""
rails.vim utility
railstab.vim utility
railscasts color scheme
Railscasts-Theme-GUIand256color color scheme
FastGrep utility
Railscasts-Theme-GUIand256color color scheme
apidock.vim utility
rails.vim utility
railscasts color scheme
railstab.vim utility
"""
And the exit status should be 0

Expand All @@ -23,12 +23,12 @@ Feature: Search for a vim script
When I run "vimpack search --utility --color-scheme rails"
Then the output should contain:
"""
rails.vim utility
railstab.vim utility
railscasts color scheme
Railscasts-Theme-GUIand256color color scheme
FastGrep utility
Railscasts-Theme-GUIand256color color scheme
apidock.vim utility
rails.vim utility
railscasts color scheme
railstab.vim utility
"""
And the exit status should be 0

Expand All @@ -37,10 +37,32 @@ Feature: Search for a vim script
When I run "vimpack search --utility rails"
Then the output should contain:
"""
rails.vim utility
railstab.vim utility
FastGrep utility
apidock.vim utility
rails.vim utility
railstab.vim utility
"""
And the exit status should be 0

Scenario: Search for all utility scripts
Given an initialized vimpack in "test_vimpack"
When I run "vimpack search --utility -l 10"
Then the output should contain:
"""
0scan utility
ACScope utility
AGTD utility
Abc-Menu utility
Acpp utility
AddCppClass utility
AddIfndefGuard utility
AfterColors.vim utility
Align utility
Align.vim utility
"""
And the output should not contain:
"""
AllBuffersToOneWindow.vim utility
"""
And the exit status should be 0

Expand All @@ -49,8 +71,8 @@ Feature: Search for a vim script
When I run "vimpack search --color-scheme rails"
Then the output should contain:
"""
railscasts color scheme
Railscasts-Theme-GUIand256color color scheme
railscasts color scheme
"""
And the exit status should be 0

Expand All @@ -69,8 +91,8 @@ Feature: Search for a vim script
When I run "vimpack search --indent ruby"
Then the output should contain:
"""
ruby.vim--IGREQUE indent
indentruby.vim indent
ruby.vim--IGREQUE indent
"""
And the exit status should be 0

Expand All @@ -79,8 +101,8 @@ Feature: Search for a vim script
When I run "vimpack search --game sudoku"
Then the output should contain:
"""
sudoku game
Sudoku-Solver game
sudoku game
"""
And the exit status should be 0

Expand Down
15 changes: 9 additions & 6 deletions lib/vimpack/api/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ module Models

class Script < Base
base_url 'http://api.vimpack.org/api/v1/scripts'
#base_url 'http://localhost:3000/api/v1/scripts'
attr_accessor :name, :script_type, :summary, :repo_url, :script_version,
:description, :author

SCRIPT_TYPES = [ 'utility', 'color scheme', 'syntax', 'ftplugin',
'indent', 'game', 'plugin', 'patch' ]

def self.search(pattern, conditions=Array.new)
unless conditions.empty?
scripts = self.rest_client(:get, "search/#{pattern}",
{ :params => { :script_type => conditions.join(',') } })
else
scripts = self.rest_client(:get, "search/#{pattern}")
def self.search(pattern=nil, conditions=Array.new, limit=100, page=1)
params = { :limit => limit, :page => page }
params[:script_type] = conditions.join(',') unless conditions.empty?
path = pattern.nil? ? 'search' : "search/#{pattern}"
begin
scripts = self.rest_client(:get, path, :params => params)
rescue RestClient::InternalServerError
return []
end
scripts = Script.json_parser.parse(scripts)
scripts = scripts.map do |script|
Expand Down
10 changes: 6 additions & 4 deletions lib/vimpack/commands/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def initialize_options
conditions << script_type if @options[script_type.to_sym]
conditions
end
@limit = @options[:limit]
@page = @options[:page]
end

def initialize_commands
die!("search requires a single argument") unless @commands.size == 1
@pattern = @commands[0]
die!("search requires a single argument") unless (@commands.size == 1 || !@conditions.empty?)
@pattern = @commands[0] if @commands.size >= 1
end

def run
scripts = ::Vimpack::Api::Models::Script.search(@pattern, @conditions)
return say("no scripts found!") if scripts.empty?
scripts = ::Vimpack::Api::Models::Script.search(@pattern, @conditions, @limit, @page)
return exit_with_error!('no scripts found!', 0) if scripts.empty?
linesize = scripts.sort do |a,b|
a.name.size <=> b.name.size
end.reverse.first.name.size + 1
Expand Down
2 changes: 1 addition & 1 deletion lib/vimpack/utils/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def die!(message=nil)

def exit_with_error!(message=nil, exit_code=1)
scream(message) unless message.nil?
exit(1)
exit(exit_code)
end

end
Expand Down

0 comments on commit 281f2cb

Please sign in to comment.