shb 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e59ea891242f1ed837c35ba9be734526226019f1
4
- data.tar.gz: ff0757e7ed3e2e7a556122804181ab9215cc733c
3
+ metadata.gz: cdc3a72b6032d46883aed8f77e69cb31cb383b93
4
+ data.tar.gz: 08b208e4f0947c2e0c21c893f9ffbe46a7c6c7f2
5
5
  SHA512:
6
- metadata.gz: c0ca8bb25eda6b02228174552d1c23d90199f6c35490899f28ababf6675c7efb161f9c3f84622903dfd1f4f3e73669e8de7c51b998c6948f43c7b645c71580dc
7
- data.tar.gz: c16c2157ea4be0f51df260c250911e02df0d52d097fcaaae53422fe6c18cc65d0fe0d3741c20a299e5b328108c7c70bf9759d8848504ff8cfb8d82d694cae106
6
+ metadata.gz: 3885df3451532f906bc6c857d5cb2daa84f1b3311e034522096ca4af6f3aee71e2668c59a3cf1388d2319f63e560686e92a2d20a98caa1a7df7845d897f91bdf
7
+ data.tar.gz: 52781eb715d77113aa179d333761fbd35b6148522af22621689594c78e9546b6ed38741dad2713dcecdd916d0ab7a5655270d1116306ec3b069927ee1607b0d5
@@ -19,6 +19,8 @@ module Shb
19
19
  config.cycle_user_agent = false
20
20
  config.use_cookies = false
21
21
  config.logger = nil
22
+ config.debug = false
23
+ config.debug_log = nil
22
24
 
23
25
  parser ::Shb::Parser
24
26
  follow_redirects false
@@ -52,6 +54,7 @@ module Shb
52
54
  cycle_user_agent!
53
55
  set_cookies!
54
56
  response = self.class.send(method, uri.to_s, options, &block)
57
+ debug!(method, uri, options, response)
55
58
  save_cookies!(response)
56
59
  cache_write(response, uri, options)
57
60
  end
@@ -72,6 +75,39 @@ module Shb
72
75
  logger.info "#{method.to_s.upcase} #{uri.to_s}#{options[:query].nil? ? nil : "?#{HashConversions.to_params(options[:query])}"}"
73
76
  end
74
77
 
78
+ #
79
+ def debug!(method, uri, options, response)
80
+ return unless config.debug
81
+
82
+ io = if config.debug_log
83
+ File.open(config.debug_log, 'a')
84
+ elsif defined?(::Rails)
85
+ File.open(File.join(::Rails.root, 'log', 'shb-debug.log'), 'a')
86
+ else
87
+ STDERR
88
+ end
89
+
90
+ io.puts
91
+ io.puts ">>>>>>>>>> #{response.request.http_method::METHOD} #{response.request.last_uri.to_s}"
92
+ response.request.options[:headers].sort.each do |k,v|
93
+ io.puts "#{k}: #{v}"
94
+ end
95
+ io.puts
96
+ io.puts response.request.options[:body]
97
+
98
+ io.puts
99
+ io.puts "<<<<<<<<<< #{response.code} #{response.message}"
100
+ response.headers.sort.each do |k,v|
101
+ io.puts "#{k}: #{v}"
102
+ end
103
+ io.puts
104
+ io.puts response.body
105
+ io.puts
106
+ ensure
107
+ io.close if io.is_a? File # Don't close STDERR
108
+ end
109
+
110
+
75
111
  #
76
112
  def cycle_user_agent!
77
113
  return unless config.cycle_user_agent
data/lib/shb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Shb
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe Shb::Client do
5
+ before do
6
+ stub_request(:any, 'supremegolf.com')
7
+ end
8
+
9
+ it "does not debug by default" do
10
+ shb = Shb::Client.new
11
+ tmpfile = Tempfile.new('shb-debug')
12
+ shb.class.config.debug_log = tmpfile.path
13
+ expect(File).not_to receive(:open).with(tmpfile.path, 'a')
14
+ shb.get('/')
15
+ end
16
+
17
+ it "debug to specified logfile" do
18
+ shb = Shb::Client.new
19
+ tmpfile = Tempfile.new('shb-debug')
20
+ shb.class.config.debug = true
21
+ shb.class.config.debug_log = tmpfile.path
22
+ expect(File).to receive(:open).with(tmpfile.path, 'a').and_call_original
23
+ shb.get('/')
24
+ end
25
+
26
+ it "debug to STDERR" do
27
+ shb = Shb::Client.new
28
+ tmpfile = Tempfile.new('shb-debug')
29
+ shb.class.config.debug = true
30
+ shb.class.config.debug_log = nil
31
+ expect(STDERR).to receive(:puts).at_least(:twice).and_call_original
32
+ shb.get('/')
33
+ end
34
+
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Hallstrom
@@ -130,6 +130,7 @@ files:
130
130
  - shb.gemspec
131
131
  - spec/cache_spec.rb
132
132
  - spec/cycle_user_agent_spec.rb
133
+ - spec/debug_spec.rb
133
134
  - spec/html_spec.rb
134
135
  - spec/json_spec.rb
135
136
  - spec/logger_spec.rb
@@ -164,6 +165,7 @@ summary: ''
164
165
  test_files:
165
166
  - spec/cache_spec.rb
166
167
  - spec/cycle_user_agent_spec.rb
168
+ - spec/debug_spec.rb
167
169
  - spec/html_spec.rb
168
170
  - spec/json_spec.rb
169
171
  - spec/logger_spec.rb