chuck 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@chuck --install --create
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'getopt'
7
+ gem 'httparty'
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "rspec"
13
+ gem "yard"
14
+ gem "bundler"
15
+ gem "jeweler"
16
+ end
@@ -0,0 +1,40 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ getopt (1.4.1)
6
+ git (1.2.5)
7
+ httparty (0.10.0)
8
+ multi_json (~> 1.0)
9
+ multi_xml
10
+ jeweler (1.8.4)
11
+ bundler (~> 1.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ rdoc
15
+ json (1.7.6)
16
+ multi_json (1.5.0)
17
+ multi_xml (0.5.2)
18
+ rake (10.0.3)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ rspec (2.12.0)
22
+ rspec-core (~> 2.12.0)
23
+ rspec-expectations (~> 2.12.0)
24
+ rspec-mocks (~> 2.12.0)
25
+ rspec-core (2.12.2)
26
+ rspec-expectations (2.12.1)
27
+ diff-lcs (~> 1.1.3)
28
+ rspec-mocks (2.12.1)
29
+ yard (0.8.3)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler
36
+ getopt
37
+ httparty
38
+ jeweler
39
+ rspec
40
+ yard
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 max thom stahl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # chuck
2
+
3
+ Add helpful facts about Chuck Norris, Bruce Schneier, or anyone else, to your
4
+ Ruby apps.
5
+
6
+ ## Usage
7
+
8
+ In its simplest form, if you just want a random Chuck Norris fact from the
9
+ Internet Chuck Norris Database, you can just call the class method `retrieve`,
10
+ which will return you a Chuck instance.
11
+
12
+ 1.9.3p194 :008 > Chuck.retrieve.jokes
13
+ => ["Chuck Norris can compile syntax errors."]
14
+
15
+ Some options may be passed to the retrieve method to be sent along with the API
16
+ request to the Internet Chuck Norris Database, like so...
17
+
18
+ 1.9.3p194 :008 > Chuck.retrieve(first_name: 'Max', last_name: 'Stahl').joke
19
+
20
+ ## Changelog
21
+
22
+ ### Version 0.1.0
23
+
24
+ First release. Not everything working. Still throws HTTParty exceptions
25
+ whenever anything goes wrong (and it will, because the Internet Chuck Norris
26
+ Database is frequently down).
27
+
28
+ ## Contributing to chuck
29
+
30
+ * Check out the latest master to make sure the feature hasn't been implemented
31
+ or the bug hasn't been fixed yet.
32
+ * Check out the issue tracker to make sure someone already hasn't requested it
33
+ and/or contributed it.
34
+ * Fork the project.
35
+ * Start a feature/bugfix branch.
36
+ * Commit and push until you are happy with your contribution.
37
+ * Make sure to add tests for it. This is important so I don't break it in a
38
+ future version unintentionally.
39
+ * Please try not to mess with the Rakefile, version, or history. If you want to
40
+ have your own version, or is otherwise necessary, that is fine, but please
41
+ isolate to its own commit so I can cherry-pick around it.
42
+
43
+ ## Copyright
44
+
45
+ Copyright (c) 2012 max thom stahl. See LICENSE.txt for further details.
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "chuck"
18
+ gem.homepage = "http://github.com/mstahl/chuck"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Gem for interacting with the Internet Chuck Norris Database.}
21
+ gem.description = %Q{Use this gem to retrieve jokes from the Internet Chuck Norris Database, with a variety of options and object-oriented excellence, for the people. http://www.icndb.com/}
22
+ gem.email = "max@villainousindustries.com"
23
+ gem.authors = ["max thom stahl"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby -wKU
2
+
3
+ require 'getopt/std'
4
+
5
+ # require 'chuck'
6
+ # require 'chuck/joke'
7
+ Dir[File.join(File.dirname(__FILE__), '..', 'lib', '**', '*.rb')].each{|f| require f}
8
+
9
+ options = Getopt::Std.getopts('c:Ch')
10
+
11
+ USAGE = %Q{
12
+ Usage: chuck [Ch] [-c quantity]
13
+
14
+ -C : Just list the categories available
15
+ -c : Specify the quantity of jokes to fetch
16
+ -h : Output this helpful message
17
+ }
18
+
19
+ if options['h'] then
20
+ puts USAGE
21
+ exit
22
+ end
23
+
24
+ if options['C'] then
25
+ p Chuck.categories
26
+ exit
27
+ end
28
+
29
+ quantity = ARGV.shift || 1
30
+
31
+ Chuck::Request.retrieve(quantity: quantity).jokes.each{|j| puts j}
@@ -0,0 +1,51 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ require 'pp'
5
+
6
+ Dir[File.join(File.dirname(__FILE__), 'chuck', '**', '*.rb')].each{|f| require f}
7
+
8
+ module Chuck
9
+ class Request
10
+
11
+ include HTTParty
12
+ base_uri 'http://api.icndb.com'
13
+
14
+ attr_reader :first_name, :last_name, :jokes, :id, :raw_response
15
+
16
+ # Instance methods ==========================================================
17
+
18
+ def initialize(options = {})
19
+ options.each{|k, v| self.instance_variable_set(:"@#{k}", v) if self.respond_to?(k)}
20
+ end
21
+
22
+ def success
23
+ @raw_response['type'] == 'success'
24
+ end
25
+
26
+ # Class methods =============================================================
27
+
28
+ def self.categories
29
+ get("/categories").parsed_response
30
+ end
31
+
32
+ def self.retrieve(options = {})
33
+ url = "/jokes/random/#{options[:quantity] or 1}"
34
+
35
+ query = Hash.new
36
+ query[:firstName] = options[:first_name] if options[:first_name]
37
+ query[:lastName] = options[:last_name] if options[:last_name]
38
+ query[:limitTo] = "[#{options[:only].join(',')}]" if options[:only]
39
+ query[:exclude] = "[#{options[:exclude].join(',')}]" if options[:except]
40
+
41
+ result = JSON.parse(get(url, query).parsed_response)
42
+
43
+ Chuck::Request.new jokes: Chuck::Joke.jokes_from_result(result['value']),
44
+ first_name: query[:firstName],
45
+ last_name: query[:lastName],
46
+ raw_response: result
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ module Chuck
2
+ class Joke
3
+ attr_accessor :id, :body, :categories
4
+
5
+ def initialize(options = {})
6
+ options.each{|k, v| self.send("#{k}=", v) if respond_to?(k) }
7
+ end
8
+
9
+ def self.jokes_from_result(result)
10
+ result.map do |j|
11
+ Joke.new body: j['joke'], categories: j['categories'], id: j['id']
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ @body
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chuck" do
4
+ before do
5
+ HTTParty.stub_chain(:get, :parsed_response) do
6
+ {
7
+ "type" => "success",
8
+ "value" => [
9
+ {
10
+ "id" => 490,
11
+ "joke" => "Chuck Norris doesn't need to use AJAX because pages are too afraid to postback anyways.",
12
+ "categories" => ["nerdy", "chuck norris"]
13
+ }, {
14
+ "id" => 416,
15
+ "joke" => "Chuck Norris plays racquetball with a waffle iron and a bowling ball.",
16
+ "categories" => ["chuck norris"]
17
+ }, {
18
+ "id" => 413, "joke" => "Those aren't credits that roll after Walker Texas Ranger. It is actually a list of fatalities that occurred during the making of the episode.",
19
+ "categories" => ["chuck norris"]
20
+ }
21
+ ]
22
+ }
23
+ end
24
+ end
25
+
26
+ let(:chuck) { Chuck.retrieve(quantity: 3) }
27
+
28
+ it 'should grab success value' do
29
+ chuck.success.should eq(true)
30
+ end
31
+
32
+ it 'should grab joke ID' do
33
+ chuck.id.should eq(268)
34
+ end
35
+
36
+ it 'should grab the joke' do
37
+ chuck.joke.should eq("Time waits for no man. Unless that man is Chuck Norris.")
38
+ end
39
+
40
+ it 'should pass params to ICNDb properly' do
41
+
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'chuck'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chuck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - max thom stahl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: getopt
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Use this gem to retrieve jokes from the Internet Chuck Norris Database,
111
+ with a variety of options and object-oriented excellence, for the people. http://www.icndb.com/
112
+ email: max@villainousindustries.com
113
+ executables:
114
+ - chuck
115
+ extensions: []
116
+ extra_rdoc_files:
117
+ - LICENSE.txt
118
+ - README.md
119
+ files:
120
+ - .document
121
+ - .rspec
122
+ - .rvmrc
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - VERSION
129
+ - bin/chuck
130
+ - lib/chuck.rb
131
+ - lib/chuck/joke.rb
132
+ - spec/chuck_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: http://github.com/mstahl/chuck
135
+ licenses:
136
+ - MIT
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ segments:
148
+ - 0
149
+ hash: -4086379836956308407
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.23
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Gem for interacting with the Internet Chuck Norris Database.
162
+ test_files: []