tiny_grabber 0.2.1 → 0.2.2
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 +4 -4
- data/README.md +8 -0
- data/lib/tiny_grabber/agent.rb +24 -24
- data/lib/tiny_grabber/version.rb +1 -1
- data/lib/tiny_grabber.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4782960145d5f33281aacf96f4c4773f702d75d
|
4
|
+
data.tar.gz: 86fd979f097604e8383d98a03fd0157c150de4d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 425e372d1753b7becf320681f385369af9fdb27002831c5371049a01c1100b9009e6f41904e31dbf447d34a57dd1b57ef9763bce371b42d397d82f1a47afd36e
|
7
|
+
data.tar.gz: a91b5a6fd8fed450d467495f54dc54e86c339590eb512c94359d0b1d2d8d0905a83b022cdceede87999d54b3d278d297ee33fdd3e5911ee43ae0c75a756c0c31
|
data/README.md
CHANGED
@@ -39,6 +39,11 @@ params = { key: 'value' }
|
|
39
39
|
tg = TinyGrabber.new
|
40
40
|
# Set debug flag for view log information
|
41
41
|
tg.debug = true
|
42
|
+
# Or set debug configuration
|
43
|
+
# active - flag to save log information
|
44
|
+
# destination - save log to file or print: [:file, :print]
|
45
|
+
# save_html - flag to save response html to file
|
46
|
+
tg.debug = { active: true, destination: :file, save_html: true }
|
42
47
|
# Set max time to execute request
|
43
48
|
tg.read_timeout = read_timeout
|
44
49
|
# Set web browser name
|
@@ -79,6 +84,9 @@ response.body
|
|
79
84
|
|
80
85
|
## Changelog
|
81
86
|
|
87
|
+
* *v 0.2.2*
|
88
|
+
* Added debug configurations.
|
89
|
+
|
82
90
|
* *v 0.2.1*
|
83
91
|
* Setting random user_agent from list if it not seted
|
84
92
|
* Remove headers attribute from singleton methods
|
data/lib/tiny_grabber/agent.rb
CHANGED
@@ -5,6 +5,10 @@
|
|
5
5
|
class TinyGrabber::Agent
|
6
6
|
# Debug flag for detilazition log and save result HTML to /log/*.html file
|
7
7
|
attr_accessor :debug
|
8
|
+
# Debug destination type
|
9
|
+
attr_accessor :debug_destination
|
10
|
+
# Debug flag for save html in file
|
11
|
+
attr_accessor :debug_save_html
|
8
12
|
# Max time to execute request
|
9
13
|
attr_accessor :read_timeout
|
10
14
|
# Web browser name
|
@@ -46,6 +50,8 @@ class TinyGrabber::Agent
|
|
46
50
|
#
|
47
51
|
def initialize
|
48
52
|
@debug = false
|
53
|
+
@debug_destination = :file
|
54
|
+
@debug_save_html = false
|
49
55
|
|
50
56
|
# Initialize variables agent attributes
|
51
57
|
@user_agent = AGENT_ALIASES[rand(AGENT_ALIASES.count) - 1]
|
@@ -148,10 +154,10 @@ class TinyGrabber::Agent
|
|
148
154
|
#
|
149
155
|
def fetch url, method = :get, headers = {}, params = {}
|
150
156
|
if @debug
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
157
|
+
Debug::save @debug_destination, '=============================='
|
158
|
+
Debug::save @debug_destination, "#{method.upcase} #{url}"
|
159
|
+
Debug::save @debug_destination, "-> [params] = #{params}"
|
160
|
+
Debug::save @debug_destination, '------------------------------'
|
155
161
|
end
|
156
162
|
set_uri url
|
157
163
|
case method
|
@@ -170,22 +176,23 @@ class TinyGrabber::Agent
|
|
170
176
|
case @response
|
171
177
|
# HTTP response code 1xx
|
172
178
|
when Net::HTTPInformation
|
179
|
+
Debug::save @debug_destination, "<- [response] = Net::HTTPInformation" if @debug
|
173
180
|
# HTTP response code 2xx
|
174
181
|
when Net::HTTPSuccess
|
175
182
|
save_headers if @response.header
|
176
183
|
save_cookies if @response.cookies
|
184
|
+
Debug::save @debug_destination, "<- [response] = #{@response.code} Net::HTTPSuccess" if @debug
|
177
185
|
# HTTP response code 3xx
|
178
186
|
when Net::HTTPRedirection
|
187
|
+
Debug::save @debug_destination, "<- [response] = #{@response.code} Net::HTTPRedirection" if @debug
|
179
188
|
# HTTP response code 4xx
|
180
189
|
when Net::HTTPClientError
|
190
|
+
Debug::save @debug_destination, "<- [response] = #{@response.code} Net::HTTPClientError" if @debug
|
181
191
|
# HTTP response code 5xx
|
182
192
|
when Net::HTTPServerError
|
193
|
+
Debug::save @debug_destination, "<- [response] = #{@response.code} Net::HTTPServerError" if @debug
|
183
194
|
end
|
184
|
-
if @
|
185
|
-
debug_filename = "log/#{method.upcase}_#{@uri.to_s.gsub(/[\/:]/, '_').gsub(/_+/, '_')}"
|
186
|
-
File.open(debug_filename, 'wb') { |f| f << @response.body } if @debug
|
187
|
-
p "#{debug_initial_word} <- [html_file] = #{debug_filename}"
|
188
|
-
end
|
195
|
+
Debug::save_to_file @response.body if @debug_save_html
|
189
196
|
@response
|
190
197
|
end
|
191
198
|
|
@@ -197,7 +204,7 @@ class TinyGrabber::Agent
|
|
197
204
|
def set_uri url
|
198
205
|
# It's magic work with escaped url
|
199
206
|
@uri = URI(URI.escape(URI.unescape(url)))
|
200
|
-
|
207
|
+
Debug::save @debug_destination, "-> [uri] = #{@uri}" if @debug
|
201
208
|
end
|
202
209
|
|
203
210
|
|
@@ -205,7 +212,7 @@ class TinyGrabber::Agent
|
|
205
212
|
#
|
206
213
|
def set_user_agent
|
207
214
|
@headers['User-Agent'] = @user_agent
|
208
|
-
|
215
|
+
Debug::save @debug_destination, "-> [user_agent] = #{@user_agent}" if @debug
|
209
216
|
end
|
210
217
|
|
211
218
|
|
@@ -213,7 +220,7 @@ class TinyGrabber::Agent
|
|
213
220
|
#
|
214
221
|
def set_basic_auth
|
215
222
|
@request.basic_auth @basic_auth[:username], @basic_auth[:password]
|
216
|
-
|
223
|
+
Debug::save @debug_destination, "-> [basic_auth] = #{@basic_auth}" if @debug
|
217
224
|
end
|
218
225
|
|
219
226
|
|
@@ -221,7 +228,7 @@ class TinyGrabber::Agent
|
|
221
228
|
#
|
222
229
|
def set_headers
|
223
230
|
@headers.each { |k, v| @request.add_field(String(k), v) }
|
224
|
-
|
231
|
+
Debug::save @debug_destination, "-> [headers] = #{@headers}" if @debug
|
225
232
|
end
|
226
233
|
|
227
234
|
|
@@ -229,7 +236,7 @@ class TinyGrabber::Agent
|
|
229
236
|
#
|
230
237
|
def set_cookies
|
231
238
|
@request['Cookie'] = @cookies
|
232
|
-
|
239
|
+
Debug::save @debug_destination, "-> [cookies] = #{@cookies}" if @debug
|
233
240
|
end
|
234
241
|
|
235
242
|
|
@@ -239,7 +246,7 @@ class TinyGrabber::Agent
|
|
239
246
|
def send_request
|
240
247
|
@http.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http|
|
241
248
|
http.read_timeout = @read_timeout
|
242
|
-
|
249
|
+
Debug::save @debug_destination, "-> [read_timeout] = #{@read_timeout}" if @debug
|
243
250
|
http.request(@request)
|
244
251
|
end
|
245
252
|
end
|
@@ -251,7 +258,7 @@ class TinyGrabber::Agent
|
|
251
258
|
@headers = @response.headers
|
252
259
|
# Delete header TRANSFER_ENCODING for chain of requests
|
253
260
|
@headers.delete('transfer-encoding')
|
254
|
-
|
261
|
+
Debug::save @debug_destination, "<- [headers] = #{@headers}" if @debug
|
255
262
|
end
|
256
263
|
|
257
264
|
|
@@ -259,7 +266,7 @@ class TinyGrabber::Agent
|
|
259
266
|
#
|
260
267
|
def save_cookies
|
261
268
|
@cookies = @response.cookies
|
262
|
-
|
269
|
+
Debug::save @debug_destination, "<- [cookies] = #{@cookies}" if @debug
|
263
270
|
end
|
264
271
|
|
265
272
|
|
@@ -269,11 +276,4 @@ class TinyGrabber::Agent
|
|
269
276
|
@headers = {}
|
270
277
|
@cookies = nil
|
271
278
|
end
|
272
|
-
|
273
|
-
|
274
|
-
# Tiny grabber initial word for debug
|
275
|
-
#
|
276
|
-
def debug_initial_word
|
277
|
-
"TG | #{Time.now.strftime('%Y%m%d-%H%M%S')} |"
|
278
|
-
end
|
279
279
|
end
|
data/lib/tiny_grabber/version.rb
CHANGED
data/lib/tiny_grabber.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'tiny_grabber/version'
|
2
2
|
require 'tiny_grabber/agent'
|
3
|
+
require 'tiny_grabber/debug'
|
3
4
|
|
4
5
|
require 'uri'
|
5
6
|
require 'net/http'
|
@@ -80,7 +81,15 @@ class TinyGrabber
|
|
80
81
|
# @param debug Flag to start debug
|
81
82
|
#
|
82
83
|
def debug= debug
|
83
|
-
|
84
|
+
if debug.is_a?(TrueClass) || debug.is_a?(FalseClass)
|
85
|
+
@agent.debug = debug
|
86
|
+
@agent.debug_destination = :print
|
87
|
+
@agent.debug_save_html = false
|
88
|
+
elsif debug.is_a? Hash
|
89
|
+
@agent.debug = debug[:active]
|
90
|
+
@agent.debug_destination = debug[:destination]
|
91
|
+
@agent.debug_save_html = debug[:save_html]
|
92
|
+
end
|
84
93
|
end
|
85
94
|
|
86
95
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_grabber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksandr Chernyshev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: socksify
|