chuckle 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/chuckle ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # in lieu of -w, since we're using env to startup
4
+ $VERBOSE = true
5
+
6
+ require "chuckle"
7
+ require "trollop"
8
+
9
+ class Main
10
+ attr_accessor :options
11
+
12
+ def initialize(options = {})
13
+ self.options = options
14
+ list = IO.readlines(options[:file]).map(&:chomp)
15
+ list.each_with_index do |i, index|
16
+ puts "#{index+1}/#{list.length}. #{i}"
17
+ begin
18
+ chuckle.get(i)
19
+ rescue Chuckle::Error => e
20
+ puts " #{e.message}"
21
+ end
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def chuckle
28
+ @chuckle ||= Chuckle::Client.new(nretries: options[:nretries], timeout: options[:timeout])
29
+ end
30
+ end
31
+
32
+
33
+
34
+ #
35
+ # parse command line
36
+ #
37
+
38
+ options = Trollop.options do
39
+ banner <<EOF
40
+ Usage: chuckle <url_file>
41
+
42
+ chuckle fetches urls as listed in url_file. It just populate the disk
43
+ cache.
44
+ EOF
45
+
46
+ opt :nretries, "Number of retries per URL", type: Integer
47
+ opt :timeout, "Timeout per retry", type: Integer
48
+ end
49
+
50
+ Trollop.die "need <url_file>" if ARGV.length != 1
51
+ options[:file] = ARGV.shift
52
+ Main.new(options)
data/chuckle.gemspec CHANGED
@@ -15,6 +15,8 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.rubyforge_project = "chuckle"
17
17
 
18
+ s.add_runtime_dependency "trollop"
19
+
18
20
  s.add_development_dependency "awesome_print"
19
21
  s.add_development_dependency "json"
20
22
  s.add_development_dependency "minitest", "~> 5.0"
@@ -7,7 +7,8 @@ module Chuckle
7
7
  attr_accessor :options, :cache
8
8
 
9
9
  def initialize(options = {})
10
- self.options = DEFAULT_OPTIONS.merge(options)
10
+ self.options = DEFAULT_OPTIONS.dup
11
+ options.each { |k, v| self.options[k] = v if v }
11
12
  self.cache = Cache.new(self)
12
13
  sanity!
13
14
  end
@@ -56,7 +57,14 @@ module Chuckle
56
57
  def curl(request)
57
58
  vputs request.uri
58
59
  rate_limit!(request)
59
- cache.set(request, Curl.new(request))
60
+
61
+ curl = Curl.new(request)
62
+ begin
63
+ curl.run
64
+ cache.set(request, curl)
65
+ ensure
66
+ curl.cleanup
67
+ end
60
68
  end
61
69
 
62
70
  def raise_errors(response)
@@ -71,7 +79,7 @@ module Chuckle
71
79
  if !cache_errors?
72
80
  cache.clear(response.request)
73
81
  end
74
- raise Error.new("Chuckle::Error, #{error}", response)
82
+ raise Error.new(error, response)
75
83
  end
76
84
 
77
85
  def vputs(s)
data/lib/chuckle/curl.rb CHANGED
@@ -4,7 +4,6 @@ module Chuckle
4
4
  class Curl
5
5
  def initialize(request)
6
6
  @request = request
7
- run
8
7
  end
9
8
 
10
9
  # tmp path for response headers
@@ -24,7 +23,7 @@ module Chuckle
24
23
 
25
24
  # capture exit code, bail on INT
26
25
  exit_code = $?.to_i / 256
27
- if exit_code != 0 && $?.termsig == Signal.list["INT"]
26
+ if $?.termsig == Signal.list["INT"]
28
27
  Process.kill(:INT, $$)
29
28
  end
30
29
 
@@ -37,6 +36,12 @@ module Chuckle
37
36
  end
38
37
  end
39
38
 
39
+ # make sure we don't accidentally leave any files hanging around
40
+ def cleanup
41
+ Util.rm_if_necessary(headers_path)
42
+ Util.rm_if_necessary(body_path)
43
+ end
44
+
40
45
  def self.exit_code_to_headers(exit_code)
41
46
  "exit_code #{exit_code}"
42
47
  end
@@ -1,4 +1,4 @@
1
1
  module Chuckle
2
2
  # Gem version
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
data/test/helper.rb CHANGED
@@ -8,6 +8,7 @@ module Helper
8
8
  CACHE_DIR = "/tmp/_chuckle_tests"
9
9
  URL = "http://chuckle"
10
10
  QUERY = { "b" => "12", "a" => "34", "x y" => "56" }
11
+ TMP = "/tmp/_chuckle_tmp.txt"
11
12
 
12
13
  #
13
14
  # fake responses
@@ -99,4 +100,9 @@ module Helper
99
100
  end
100
101
  Kernel.stub(:system, fake_system) { yield }
101
102
  end
103
+
104
+ def assert_command(cmd)
105
+ system("#{cmd} > #{TMP} 2>&1")
106
+ assert($? == 0, File.read(TMP))
107
+ end
102
108
  end
data/test/test_network.rb CHANGED
@@ -53,4 +53,9 @@ class TestNetwork < Minitest::Test
53
53
  client.get("http://httpbin.org/robots.txt")
54
54
  assert !File.exists?(cookie_jar), "cookie jar should've expired"
55
55
  end
56
+
57
+ def test_bin
58
+ Dir.chdir(File.expand_path("../", __FILE__))
59
+ assert_command("../bin/chuckle url_file.txt")
60
+ end
56
61
  end
data/test/url_file.txt ADDED
@@ -0,0 +1,2 @@
1
+ http://httpbin.org/status/500
2
+ http://httpbin.org/get
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chuckle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-20 00:00:00.000000000 Z
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: trollop
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'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: awesome_print
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +110,8 @@ dependencies:
94
110
  description: An http client that caches on disk.
95
111
  email:
96
112
  - amd@gurge.com
97
- executables: []
113
+ executables:
114
+ - chuckle
98
115
  extensions: []
99
116
  extra_rdoc_files: []
100
117
  files:
@@ -104,6 +121,7 @@ files:
104
121
  - LICENSE
105
122
  - README.md
106
123
  - Rakefile
124
+ - bin/chuckle
107
125
  - chuckle.gemspec
108
126
  - lib/chuckle.rb
109
127
  - lib/chuckle/cache.rb
@@ -120,6 +138,7 @@ files:
120
138
  - test/test_cache.rb
121
139
  - test/test_network.rb
122
140
  - test/test_requests.rb
141
+ - test/url_file.txt
123
142
  homepage: http://github.com/gurgeous/chuckle
124
143
  licenses: []
125
144
  post_install_message:
@@ -140,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
159
  version: '0'
141
160
  segments:
142
161
  - 0
143
- hash: -3317364503700294322
162
+ hash: -116042020902971190
144
163
  requirements: []
145
164
  rubyforge_project: chuckle
146
165
  rubygems_version: 1.8.24
@@ -152,3 +171,4 @@ test_files:
152
171
  - test/test_cache.rb
153
172
  - test/test_network.rb
154
173
  - test/test_requests.rb
174
+ - test/url_file.txt