quote 0.1.1 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +11 -4
  2. data/bin/quote +9 -6
  3. data/lib/quote.rb +62 -8
  4. metadata +40 -3
@@ -1,8 +1,7 @@
1
1
  ## Quotes ##
2
2
 
3
- Maintain and quote your awesome quotes from movies, games and other sources from command line.
4
-
5
- ## Usage ##
3
+ Maintain and quote your awesome quotes from movies, games and other sources from command line, plug it to any of
4
+ your apps/projects.
6
5
 
7
6
  ### Quoting ###
8
7
 
@@ -14,12 +13,20 @@ To search for a specific quote check usage:
14
13
 
15
14
  ### Adding quote ###
16
15
 
17
- WIP
16
+ To add a quote:
17
+ <code>quote --add <argument></code>
18
+
19
+ Note: Your argument needs to be a valid JSON containing source, context, quote, theme params.
18
20
 
19
21
  ### Deleting quote(s) ###
20
22
 
21
23
  WIP
22
24
 
25
+ ### Configuring ###
26
+
27
+ You can see pretty colorized output if you set environment variable `QUOTE_COLORIZE` to 'true'.
28
+ You can specify your own path to quotes files via environment variable `QUOTE_SOURCE`.
29
+
23
30
  ## To-do ##
24
31
 
25
32
  * Full text searching
data/bin/quote CHANGED
@@ -4,12 +4,17 @@ require 'quote'
4
4
  require 'getoptlong'
5
5
 
6
6
  USAGE = <<-EOU
7
- To get a random quote type: #{'quote'.green}\nOr search for a random quote using one or many of following criteria:
7
+ To get a random quote type: #{'quote'.green}\n
8
+ Or you may use following options:
8
9
  #{'quote --theme/-t <argument>'.green} to search for a match in themes (ex. 'movie')
9
10
  #{'quote --quote/-q <argument>'.green} to search for a match in quote itself (ex. 'kick ass and chew gum')
10
11
  #{'quote --context/-c <argument>'.green} to search for a match in context quote was originated (ex. 'pulp fiction')
11
12
  #{'quote --source/-s <argument>'.green} to search for a match in originator of the quote (ex. 'the dude')
13
+ #{'quote --add/-a <argument>'.green} to add a quote your argument needs to be a valid JSON containing source, context, quote, theme params
12
14
  #{'quote --help/-h'.green} to see this message
15
+ You can see pretty colorized output if you set environment variable QUOTE_COLORIZE to 'true'.
16
+ You can specify your own path to quotes files via environment variable QUOTE_SOURCE.
17
+ Search by multiple criteria is supported.
13
18
  EOU
14
19
 
15
20
  opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT],
@@ -20,11 +25,9 @@ opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT],
20
25
  criteria = {}
21
26
  begin
22
27
  opts.each do |flag, arg|
23
- if flag == '--help'
24
- puts USAGE; exit(0)
25
- else
26
- criteria[flag.gsub('--','')] = arg
27
- end
28
+ puts USAGE; exit(0) if flag == '--help'
29
+ puts Quote.add(arg); exit(0) if flag == '--add'
30
+ criteria[flag.gsub('--','')] = arg
28
31
  end
29
32
  rescue GetoptLong::InvalidOption => e
30
33
  puts "Please type quote -h for usage"
@@ -1,5 +1,7 @@
1
+ require 'rubygems'
1
2
  require 'yajl'
2
3
  require 'colored'
4
+ require 'ruby-debug'
3
5
 
4
6
  module ArrayExtensions
5
7
  def where(criteria ={})
@@ -11,20 +13,72 @@ module ArrayExtensions
11
13
  end
12
14
  result
13
15
  end
16
+
17
+ def random
18
+ self[rand(self.size)]
19
+ end
14
20
  end
15
21
  Array.send(:include, ArrayExtensions)
16
22
 
17
- module Quote
18
- def self.say(criteria = {})
19
- quotes = Yajl::Parser.parse(File.new(File.dirname(__FILE__) + '/quotes.json'))
20
- quote(random_select(quotes.where(criteria)))
23
+ module QuoteHashExtensions
24
+ def format_with_color
25
+ "#{self['theme']}: \"#{self['quote'].green}\" - #{self['source'].red}, #{self['context'].red}."
21
26
  end
22
27
 
23
- def self.random_select(enumerable)
24
- enumerable[rand(enumerable.size)]
28
+ def format
29
+ "#{self['theme']}: \"#{self['quote']}\" - #{self['source']}, #{self['context']}."
25
30
  end
31
+ end
32
+ Hash.send(:include, QuoteHashExtensions)
33
+
34
+ module Quote
35
+ CRITERIA = {
36
+ "context" => ["from", "in"],
37
+ "source" => ["by"],
38
+ "quote" => ["with", "containing", "matching"],
39
+ "theme" => ["category"]
40
+ }
41
+
42
+ class << self
43
+ def say(criteria = {})
44
+ random_quote = load_quotes.where(criteria).random
45
+ ENV['QUOTE_COLORIZE'] == "true" ? random_quote.format_with_color : random_quote.format
46
+ end
47
+
48
+ def add(json)
49
+ quote = Yajl::Parser.parse(json)
50
+ missing_criteria = []
51
+ CRITERIA.keys.each do |criterion|
52
+ missing_criteria << criterion unless quote[criterion]
53
+ end
54
+ missing_criteria.empty? ? add_quote(quote) : "Can't add quote: missing #{missing_criteria.sort.join(', ')}"
55
+ rescue Yajl::ParseError
56
+ return "Please supply a valid json"
57
+ end
26
58
 
27
- def self.quote(entry)
28
- "#{entry['theme']}: \"#{entry['quote'].green}\" - #{entry['source'].red}, #{entry['context'].red}."
59
+ def load_quotes
60
+ Yajl::Parser.parse(File.new(source_file_path))
61
+ end
62
+
63
+ CRITERIA.each do |key, match_array|
64
+ match_array.each do |match_criterion|
65
+ define_method(match_criterion) { |criterion_text| criterion_text ? say({key => criterion_text}) : say }
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def add_quote(quote)
72
+ quotes = load_quotes
73
+ quotes += [quote]
74
+ File.open(source_file_path, 'w') do |f|
75
+ f.write Yajl::Encoder.encode(quotes.uniq)
76
+ end
77
+ return "Your quote has been successfully added"
78
+ end
79
+
80
+ def source_file_path
81
+ ENV['QUOTE_SOURCE'] || File.dirname(__FILE__) + '/quotes.json'
82
+ end
29
83
  end
30
84
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quote
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 17
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ - 5
10
+ version: 0.1.5
10
11
  platform: ruby
11
12
  authors:
12
13
  - tjbladez
@@ -21,9 +22,11 @@ dependencies:
21
22
  name: yajl-ruby
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
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,14 +36,44 @@ dependencies:
33
36
  name: colored
34
37
  prerelease: false
35
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
36
40
  requirements:
37
41
  - - ">="
38
42
  - !ruby/object:Gem::Version
43
+ hash: 3
39
44
  segments:
40
45
  - 0
41
46
  version: "0"
42
47
  type: :development
43
48
  version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: riot
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
44
77
  description: Awesome quotes - maintain them, get them conviniently from command line
45
78
  email: tjbladez@gmail.com
46
79
  executables:
@@ -64,23 +97,27 @@ rdoc_options: []
64
97
  require_paths:
65
98
  - lib
66
99
  required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
67
101
  requirements:
68
102
  - - ">="
69
103
  - !ruby/object:Gem::Version
104
+ hash: 3
70
105
  segments:
71
106
  - 0
72
107
  version: "0"
73
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
74
110
  requirements:
75
111
  - - ">="
76
112
  - !ruby/object:Gem::Version
113
+ hash: 3
77
114
  segments:
78
115
  - 0
79
116
  version: "0"
80
117
  requirements: []
81
118
 
82
119
  rubyforge_project:
83
- rubygems_version: 1.3.6
120
+ rubygems_version: 1.3.7
84
121
  signing_key:
85
122
  specification_version: 3
86
123
  summary: Awesome quotes from different sources