kronk 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,17 @@
1
+ === 1.2.0 / 2011-01-20
2
+
3
+ * Enhancements:
4
+
5
+ * Added IRB option to manipulate retrieved data in IRB console.
6
+
7
+ * Bugfixes:
8
+
9
+ * Updated encoding of binary data to force_encode to binary encoding.
10
+
11
+ * Moved gem requires to avoid preloading unneeded libs.
12
+
13
+ * Added Windows support.
14
+
1
15
  === 1.1.2 / 2011-01-17
2
16
 
3
17
  * Bugfixes:
data/README.rdoc CHANGED
@@ -19,6 +19,8 @@ Kronk was made possible by the sponsoring of AT&T Interactive.
19
19
 
20
20
  * Support for custom data parsers and diff formatters.
21
21
 
22
+ * Launch IRB console with the retrieved response data.
23
+
22
24
  * Support for proxies, ssl, and basic auth.
23
25
 
24
26
  * Cookie/session handling.
@@ -65,6 +67,10 @@ Run it to display raw data with headers:
65
67
 
66
68
  $ kronk http://host.com/path.json --raw -i
67
69
 
70
+ Parse the data and run an IRB console with the response:
71
+
72
+ $ kronk http://host.com/path.json --irb
73
+
68
74
  == CONFIGURATION:
69
75
 
70
76
  Kronk pulls it's config from $HOME/.kronk and supports the following:
data/lib/kronk.rb CHANGED
@@ -1,18 +1,9 @@
1
1
  require 'rubygems'
2
- require 'plist'
2
+
3
3
  require 'json'
4
- require 'nokogiri'
5
4
  require 'cookiejar'
6
5
  require 'rack'
7
6
 
8
- # Support for new and old versions of ActiveSupport
9
- begin
10
- require 'active_support/inflector'
11
- rescue LoadError => e
12
- raise unless e.message =~ /-- active_support/
13
- require 'activesupport'
14
- end
15
-
16
7
  require 'net/https'
17
8
  require 'optparse'
18
9
  require 'yaml'
@@ -20,7 +11,15 @@ require 'yaml'
20
11
  class Kronk
21
12
 
22
13
  # This gem's version.
23
- VERSION = '1.1.2'
14
+ VERSION = '1.2.0'
15
+
16
+
17
+ ##
18
+ # Returns true if kronk is running on ruby for windows.
19
+
20
+ def self.windows?
21
+ !!(RUBY_PLATFORM.downcase =~ /mswin|mingw|cygwin/)
22
+ end
24
23
 
25
24
 
26
25
  require 'kronk/data_set'
@@ -299,6 +298,7 @@ class Kronk
299
298
  # Supports the following options:
300
299
  # :data:: Hash/String - the data to pass to the http request
301
300
  # :query:: Hash/String - the data to append to the http request path
301
+ # :follow_redirects:: Integer/Bool - number of times to follow redirects
302
302
  # :headers:: Hash - extra headers to pass to the request
303
303
  # :http_method:: Symbol - the http method to use; defaults to :get
304
304
  # :user_agent:: String - user agent string or alias; defaults to 'kronk'
@@ -314,9 +314,9 @@ class Kronk
314
314
  #
315
315
  # Returns a diff object.
316
316
 
317
- def self.compare query1, query2, options={}
318
- str1 = retrieve_data_string query1, options
319
- str2 = retrieve_data_string query2, options
317
+ def self.compare uri1, uri2, options={}
318
+ str1 = retrieve_data_string uri1, options
319
+ str2 = retrieve_data_string uri2, options
320
320
 
321
321
  Diff.new str1, str2
322
322
  end
@@ -326,25 +326,52 @@ class Kronk
326
326
  # Return a data string, parsed or raw.
327
327
  # See Kronk.compare for supported options.
328
328
 
329
- def self.retrieve_data_string query, options={}
330
- options = merge_options_for_uri query, options
329
+ def self.retrieve_data_string uri, options={}
330
+ options = merge_options_for_uri uri, options
331
+
332
+ resp = Request.retrieve uri, options
331
333
 
332
- resp = Request.retrieve query, options
334
+ if options[:irb]
335
+ irb resp
333
336
 
334
- if options[:raw]
337
+ elsif options[:raw]
335
338
  resp.selective_string options
339
+
336
340
  else
337
341
  begin
338
342
  data = resp.selective_data options
339
343
  Diff.ordered_data_string data, options[:struct]
344
+
340
345
  rescue Response::MissingParser
341
- verbose "Warning: No parser for #{resp['Content-Type']} [#{query}]"
346
+ verbose "Warning: No parser for #{resp['Content-Type']} [#{uri}]"
342
347
  resp.selective_string options
343
348
  end
344
349
  end
345
350
  end
346
351
 
347
352
 
353
+
354
+ ##
355
+ # Start an IRB console with the given http response object.
356
+
357
+ def self.irb resp
358
+ require 'irb'
359
+
360
+ $http_response = resp
361
+ $response = begin
362
+ resp.parsed_body
363
+ rescue Response::MissingParser
364
+ resp.body
365
+ end
366
+
367
+ puts "\nHTTP Response is in $http_response"
368
+ puts "Response data is in $response\n\n"
369
+
370
+ IRB.start
371
+ exit 1
372
+ end
373
+
374
+
348
375
  ##
349
376
  # Runs the kronk command with the given terminal args.
350
377
 
@@ -501,6 +528,11 @@ Kronk runs diffs against data from live and cached http responses.
501
528
  end
502
529
 
503
530
 
531
+ opt.on('--irb', 'Start an IRB console') do
532
+ options[:irb] = true
533
+ end
534
+
535
+
504
536
  opt.on('--lines', 'Show line numbers') do
505
537
  config[:show_lines] = true
506
538
  end
@@ -636,13 +668,17 @@ Kronk runs diffs against data from live and cached http responses.
636
668
  options[:uris].concat argv
637
669
  options[:uris].slice!(2..-1)
638
670
 
639
- if options[:uris].empty?
640
- $stderr << "\nError: You must enter at least one URI\n\n"
641
- $stderr << opts.to_s
642
- exit 1
643
- end
671
+ argv.clear
672
+
673
+ raise OptionParser::MissingArgument, "You must enter at least one URI" if
674
+ options[:uris].empty?
644
675
 
645
676
  options
677
+
678
+ rescue => e
679
+ $stderr << "\nError: #{e.message}\n"
680
+ $stderr << "See 'kronk --help' for usage\n\n"
681
+ exit 1
646
682
  end
647
683
 
648
684
 
data/lib/kronk/diff.rb CHANGED
@@ -43,7 +43,18 @@ class Kronk
43
43
 
44
44
  class ColorFormat
45
45
 
46
+ def self.require_win_color
47
+ begin
48
+ require 'Win32/Console/ANSI'
49
+ rescue LoadError
50
+ puts "Warning: You must gem install win32console to use color"
51
+ end
52
+ end
53
+
54
+
46
55
  def self.lines line_nums, col_width
56
+ require_win_color if Kronk.windows?
57
+
47
58
  out =
48
59
  [*line_nums].map do |lnum|
49
60
  lnum.to_s.rjust col_width
@@ -54,11 +65,13 @@ class Kronk
54
65
 
55
66
 
56
67
  def self.deleted str
68
+ require_win_color if Kronk.windows?
57
69
  "\033[31m#{str}\033[0m"
58
70
  end
59
71
 
60
72
 
61
73
  def self.added str
74
+ require_win_color if Kronk.windows?
62
75
  "\033[32m#{str}\033[0m"
63
76
  end
64
77
 
@@ -9,6 +9,7 @@ class Kronk
9
9
  # Alias for Plist.parse_xml
10
10
 
11
11
  def self.parse plist
12
+ require 'plist'
12
13
  Plist.parse_xml plist
13
14
  end
14
15
  end
data/lib/kronk/request.rb CHANGED
@@ -6,6 +6,7 @@ class Kronk
6
6
  class Request
7
7
 
8
8
 
9
+ # Raised when the URI was not resolvable.
9
10
  class NotFoundError < Exception; end
10
11
 
11
12
  ##
@@ -57,7 +58,7 @@ class Kronk
57
58
  end
58
59
 
59
60
  begin
60
- File.open(options[:cache_response], "w+") do |file|
61
+ File.open(options[:cache_response], "wb+") do |file|
61
62
  file.write resp.raw
62
63
  end if options[:cache_response]
63
64
  rescue => e
@@ -89,7 +90,12 @@ class Kronk
89
90
  path = Kronk::DEFAULT_CACHE_FILE if path == :cache
90
91
  resp = nil
91
92
 
92
- File.open(path, "r") do |file|
93
+ File.open(path, "rb") do |file|
94
+
95
+ # On windows, read the full file and insert contents into
96
+ # a StringIO to avoid failures with IO#read_nonblock
97
+ file = StringIO.new file.read if Kronk.windows?
98
+
93
99
  begin
94
100
  resp = Response.read_new file
95
101
 
@@ -291,7 +297,7 @@ class Kronk
291
297
  out.join "&"
292
298
 
293
299
  when Hash
294
- out = data.sort.map do |key, value|
300
+ out = data.map do |key, value|
295
301
  key = param.nil? ? key : "#{param}[#{key}]"
296
302
  build_query value, key
297
303
  end
@@ -71,15 +71,12 @@ class Kronk
71
71
  module Helpers
72
72
 
73
73
  ##
74
- # Assigns the raw http response value. Sets the encoding to ASCII-8BIT
75
- # and replaces invalid characters with '?' if the encoding is invalid
76
- # in Ruby 1.9.x
74
+ # Assigns the raw http response value. Sets the encoding to binary
75
+ # if the encoding is invalid in Ruby 1.9.x
77
76
 
78
77
  def raw= value
79
78
  if value.respond_to?(:valid_encoding?) && !value.valid_encoding?
80
- value.encode! "ASCII-8BIT", :invalid => :replace,
81
- :undef => :replace,
82
- :replace => "?"
79
+ value.force_encoding "binary"
83
80
  end
84
81
 
85
82
  @raw = value
@@ -194,14 +191,14 @@ class Kronk
194
191
  data = nil
195
192
 
196
193
  unless options[:no_body]
197
- data = DataSet.new(parsed_body(options[:parser])).modify options
194
+ data = parsed_body options[:parser]
198
195
  end
199
196
 
200
197
  if options[:with_headers]
201
198
  data = [parsed_header(options[:with_headers]), data].compact
202
199
  end
203
200
 
204
- data
201
+ DataSet.new(data).modify options
205
202
  end
206
203
  end
207
204
  end
@@ -5,11 +5,28 @@ class Kronk
5
5
 
6
6
  class XMLParser
7
7
 
8
+ ##
9
+ # Load required gems.
10
+
11
+ def self.require_gems
12
+ require 'nokogiri'
13
+
14
+ # Support for new and old versions of ActiveSupport
15
+ begin
16
+ require 'active_support/inflector'
17
+ rescue LoadError => e
18
+ raise unless e.message =~ /-- active_support/
19
+ require 'activesupport'
20
+ end
21
+ end
22
+
8
23
  ##
9
24
  # Takes an xml string and returns a data hash.
10
25
  # Ignores blank spaces between tags.
11
26
 
12
27
  def self.parse str
28
+ require_gems
29
+
13
30
  root_node = Nokogiri.XML str do |config|
14
31
  config.default_xml.noblanks
15
32
  end
data/test/test_request.rb CHANGED
@@ -82,7 +82,7 @@ class TestRequest < Test::Unit::TestCase
82
82
 
83
83
 
84
84
  def test_retrieve_file_cache
85
- File.expects(:open).with(Kronk::DEFAULT_CACHE_FILE, "r").
85
+ File.expects(:open).with(Kronk::DEFAULT_CACHE_FILE, "rb").
86
86
  yields StringIO.new(mock_200_response)
87
87
 
88
88
  resp = Kronk::Request.retrieve_file :cache
@@ -467,7 +467,7 @@ class TestRequest < Test::Unit::TestCase
467
467
  }
468
468
 
469
469
  assert_equal "a[]=one&a[]=two&b[b1][]=1&b[b1][]=2&b[b2]=test&foo=bar",
470
- Kronk::Request.build_query(hash)
470
+ Kronk::Request.build_query(hash).split("&").sort.join("&")
471
471
  end
472
472
 
473
473
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kronk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 1
9
8
  - 2
10
- version: 1.1.2
9
+ - 0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremie Castagna
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 -08:00
18
+ date: 2011-01-20 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency