cdb-crawlr 0.0.2 → 0.0.3

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/bin/cdb ADDED
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cdb-crawlr'
4
+ # load 'lib/cdb-crawlr.rb'
5
+ require 'optparse'
6
+
7
+ $cli = CDB::CLI.new
8
+
9
+ def print_help(opt = @global, error=nil)
10
+ puts error if error
11
+ puts opt
12
+ exit 1
13
+ end
14
+
15
+ @global = OptionParser.new do |opts|
16
+ opts.banner = "Usage: cdb [-h|--help] [-v|--version] <command> [<args>]"
17
+
18
+ opts.on("-h", "--help", "Display this screen"){ print_help }
19
+ opts.on("-v", "--version", "Show version information") do
20
+ puts "cdb #{CDB::VERSION}"; exit
21
+ end
22
+ end
23
+
24
+ @search = OptionParser.new do |opts|
25
+ opts.banner = "Usage: cdb search [-h|--help] [-s|--scope=<type>] <query>"
26
+
27
+ opts.on("-h", "--help", "Display this screen"){ print_help opts }
28
+ opts.on("-s", "--scope=<type>", "Specify type of search. Default: all") do |v|
29
+ begin
30
+ $cli[:scope] = v
31
+ rescue
32
+ print_help opts, "invalid scope: #{v}"
33
+ end
34
+ end
35
+ end
36
+
37
+ @command_opts = {
38
+ 'search' => @search
39
+ }
40
+
41
+ # Parse global flags
42
+ begin
43
+ @global.order!
44
+ rescue OptionParser::InvalidOption => e
45
+ puts e; print_help
46
+ end
47
+
48
+ # Pop and verify command
49
+ begin
50
+ command = ARGV.shift
51
+ $cli[:command] = command
52
+ command_opt = @command_opts[$cli[:command]]
53
+ rescue
54
+ error = "invalid_command: #{command}" unless command.to_s.empty?
55
+ print_help @global, error
56
+ end
57
+
58
+ # Parse command flags
59
+ begin
60
+ command_opt.order!
61
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
62
+ print_help command_opt, e
63
+ end
64
+
65
+ # Pop and verify args
66
+ begin
67
+ $cli[:args] = ARGV.join(' ')
68
+ rescue => e
69
+ print_help command_opt, e
70
+ end
71
+
72
+ $cli.execute
@@ -1,14 +1,16 @@
1
+ require 'json'
1
2
  require 'nokogiri'
2
3
  require 'open-uri'
3
4
 
4
5
  $:.unshift(File.dirname(__FILE__))
5
6
 
7
+ require 'cdb/cli'
6
8
  require 'cdb/struct'
7
9
  require 'cdb/issue'
8
10
  require 'cdb/title'
9
11
 
10
12
  module CDB
11
- VERSION = '0.0.2'
13
+ VERSION = '0.0.3'
12
14
 
13
15
  BASE_URL = 'http://www.comicbookdb.com'
14
16
  REQUEST_HEADERS = {'Connection' => 'keep-alive'}
@@ -0,0 +1,54 @@
1
+ require 'pp'
2
+
3
+ module CDB
4
+ class CLI
5
+ COMMANDS = %w[search]
6
+ SCOPES = %w[all title issue]
7
+
8
+ def initialize(options={})
9
+ @options = options
10
+ end
11
+
12
+ def [](k)
13
+ @options[k]
14
+ end
15
+
16
+ def []=(k, v)
17
+ case k
18
+ when :command
19
+ v = v.to_s.strip.downcase
20
+ raise unless COMMANDS.include?(v)
21
+ when :scope
22
+ v = v.to_s.strip.downcase.gsub(/^=|s$/, '')
23
+ raise unless SCOPES.include?(v)
24
+ when :args
25
+ v = v.to_s.strip
26
+ if self[:command] == 'search'
27
+ raise "invalid search query" if v.empty?
28
+ end
29
+ end
30
+ @options[k] = v
31
+ end
32
+
33
+ def execute
34
+ send self[:command]
35
+ end
36
+
37
+ private
38
+
39
+ def search
40
+ case self[:scope] || 'all'
41
+ when 'all'
42
+ CDB.search(self[:args]).each do |key, res|
43
+ puts key.to_s.capitalize
44
+ res.each{|r| puts r.to_json}
45
+ end
46
+ when 'title'
47
+ CDB::Title.search(self[:args]).each{|r| puts r.to_json}
48
+ when 'issue'
49
+ CDB::Issue.search(self[:args]).each{|r| puts r.to_json}
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -12,13 +12,13 @@ module CDB
12
12
 
13
13
  def parse_results(node)
14
14
  node.css("a[href^=\"#{WEB_PATH}\"]").map do |link|
15
- id = link.attr('href').split('=').last
15
+ id = link.attr('href').split('=').last.to_i
16
16
  text = link.child.text.strip
17
17
  match = text.match(/^(.* \(\d{4}\)) (.*)$/)
18
18
  title, num = match[1..2]
19
19
  name = link.next_sibling.text.strip.gsub(/^-\s*"|"$/, '').strip
20
20
  new(:cdb_id => id, :title => title, :num => num, :name => name)
21
- end
21
+ end.sort_by(&:cdb_id)
22
22
  end
23
23
 
24
24
  end
@@ -8,10 +8,15 @@ module CDB
8
8
  h.each{|k,v| send("#{k}=", v)}
9
9
  end
10
10
 
11
- def to_json(*a)
11
+ def as_json
12
12
  members.inject({}){|map, m|
13
13
  map[m] = self[m]; map
14
- }.to_json(*a)
14
+ }
15
+ end
16
+
17
+ def to_json(opts={})
18
+ opts = {space:' ', object_nl:' '}.merge(opts)
19
+ self.as_json.to_json(opts)
15
20
  end
16
21
 
17
22
  end
@@ -12,13 +12,13 @@ module CDB
12
12
 
13
13
  def parse_results(node)
14
14
  node.css("a[href^=\"#{WEB_PATH}\"]").map do |link|
15
- id = link.attr('href').split('=').last
15
+ id = link.attr('href').split('=').last.to_i
16
16
  text = link.child.text.strip
17
17
  name = text.slice(0..-8)
18
18
  year = text.slice(-5..-2)
19
19
  pub = link.next_sibling.text.gsub(/^\s*\(|\)\s*$/, '')
20
20
  new(:cdb_id => id, :name => name, :publisher => pub, :begin_date => year)
21
- end
21
+ end.sort_by(&:cdb_id)
22
22
  end
23
23
 
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdb-crawlr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000Z
12
+ date: 2012-11-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &4367990 !ruby/object:Gem::Requirement
16
+ requirement: &18435060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *4367990
24
+ version_requirements: *18435060
25
25
  description: cdb-crawlr is a Ruby gem and command-line tool for querying ComicBookDB.com
26
26
  email:
27
27
  - sgt.floydpepper@gmail.com
28
- executables: []
28
+ executables:
29
+ - cdb
29
30
  extensions: []
30
31
  extra_rdoc_files: []
31
32
  files:
33
+ - lib/cdb/cli.rb
32
34
  - lib/cdb/issue.rb
33
35
  - lib/cdb/struct.rb
34
36
  - lib/cdb/title.rb
35
37
  - lib/cdb-crawlr.rb
38
+ - bin/cdb
36
39
  homepage: https://github.com/sgtFloyd/cdb-crawlr
37
40
  licenses: []
38
41
  post_install_message: