kronk 1.2.0 → 1.2.1

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.
@@ -1,3 +1,10 @@
1
+ === 1.2.1 / 2011-01-21
2
+
3
+ * Enhancements:
4
+
5
+ * Added support for setting the encoding from the Content-Type charset
6
+ in ruby 1.9.x
7
+
1
8
  === 1.2.0 / 2011-01-20
2
9
 
3
10
  * Enhancements:
@@ -11,7 +11,7 @@ require 'yaml'
11
11
  class Kronk
12
12
 
13
13
  # This gem's version.
14
- VERSION = '1.2.0'
14
+ VERSION = '1.2.1'
15
15
 
16
16
 
17
17
  ##
@@ -101,8 +101,7 @@ class Kronk
101
101
 
102
102
  rescue Net::HTTPBadResponse
103
103
  file.rewind
104
- resp = HeadlessResponse.new file.read
105
- resp['Content-Type'] = File.extname path
104
+ resp = HeadlessResponse.new file.read, File.extname(path)
106
105
  end
107
106
  end
108
107
 
@@ -235,9 +234,7 @@ class Kronk
235
234
  use_cookies? options
236
235
 
237
236
  resp.extend Response::Helpers
238
-
239
- r_req, r_resp, r_bytes = Response.read_raw_from socket_io
240
- resp.raw = r_resp
237
+ resp.set_helper_attribs socket_io
241
238
 
242
239
  resp
243
240
  end
@@ -21,65 +21,93 @@ class Kronk
21
21
  end
22
22
 
23
23
  resp.extend Helpers
24
-
25
- r_req, r_resp, r_bytes = read_raw_from socket_io
26
- resp.raw = r_resp
27
- resp.instance_variable_set "@read", true
28
- resp.instance_variable_set "@socket", true
29
-
30
- resp.instance_variable_set "@body", resp.raw.split("\r\n\r\n",2)[1] if
31
- !resp.body
24
+ resp.set_helper_attribs socket_io, true, true
32
25
 
33
26
  resp
34
27
  end
35
28
 
36
29
 
30
+
37
31
  ##
38
- # Read the raw response from a debug_output instance and return an array
39
- # containing the raw request, response, and number of bytes received.
32
+ # Helper methods for Net::HTTPResponse objects.
40
33
 
41
- def self.read_raw_from debug_io
42
- req = nil
43
- resp = ""
44
- bytes = nil
34
+ module Helpers
45
35
 
46
- debug_io.rewind
47
- output = debug_io.read.split "\n"
36
+ ##
37
+ # Read the raw response from a debug_output instance and return an array
38
+ # containing the raw request, response, and number of bytes received.
48
39
 
49
- if output.first =~ %r{<-\s(.*)}
50
- req = instance_eval $1
51
- output.delete_at 0
52
- end
40
+ def read_raw_from debug_io
41
+ req = nil
42
+ resp = ""
43
+ bytes = nil
53
44
 
54
- if output.last =~ %r{read (\d+) bytes}
55
- bytes = $1.to_i
56
- output.delete_at(-1)
57
- end
45
+ debug_io.rewind
46
+ output = debug_io.read.split "\n"
58
47
 
59
- output.map do |line|
60
- next unless line[0..2] == "-> "
61
- resp << instance_eval(line[2..-1])
48
+ if output.first =~ %r{<-\s(.*)}
49
+ req = instance_eval $1
50
+ output.delete_at 0
51
+ end
52
+
53
+ if output.last =~ %r{read (\d+) bytes}
54
+ bytes = $1.to_i
55
+ output.delete_at(-1)
56
+ end
57
+
58
+ output.map do |line|
59
+ next unless line[0..2] == "-> "
60
+ resp << instance_eval(line[2..-1])
61
+ end
62
+
63
+ [req, resp, bytes]
62
64
  end
63
65
 
64
- [req, resp, bytes]
65
- end
66
66
 
67
+ ##
68
+ # Instantiates helper attributes from the debug socket io.
67
69
 
68
- ##
69
- # Helper methods for Net::HTTPResponse objects.
70
+ def set_helper_attribs socket_io, socket=nil, body_read=nil
71
+ @raw = udpate_encoding read_raw_from(socket_io)[1]
72
+
73
+ @read = body_read unless body_read.nil?
74
+ @socket ||= socket
75
+ @body ||= @raw.split("\r\n\r\n",2)[1]
76
+
77
+ udpate_encoding @body
78
+
79
+ puts "#{@raw.length} - #{@body.length}" if
80
+ @raw.length == 0 && @body.length != 0
81
+ self
82
+ end
70
83
 
71
- module Helpers
72
84
 
73
85
  ##
74
- # Assigns the raw http response value. Sets the encoding to binary
75
- # if the encoding is invalid in Ruby 1.9.x
86
+ # Returns the encoding provided in the Content-Type header or
87
+ # "binary" if charset is unavailable.
88
+ # Returns "utf-8" if no content type header is missing.
89
+
90
+ def encoding
91
+ content_types = self.to_hash["content-type"]
92
+
93
+ return "utf-8" if !content_types
76
94
 
77
- def raw= value
78
- if value.respond_to?(:valid_encoding?) && !value.valid_encoding?
79
- value.force_encoding "binary"
95
+ content_types.each do |c_type|
96
+ return $2 if c_type =~ /(^|;\s?)charset=(.*?)\s*(;|$)/
80
97
  end
81
98
 
82
- @raw = value
99
+ "binary"
100
+ end
101
+
102
+
103
+ ##
104
+ # Assigns self.encoding to the passed string if
105
+ # it responds to 'force_encoding'.
106
+ # Returns the string given with the new encoding.
107
+
108
+ def udpate_encoding str
109
+ str.force_encoding self.encoding if str.respond_to? :force_encoding
110
+ str
83
111
  end
84
112
 
85
113
 
@@ -213,10 +241,15 @@ class Kronk
213
241
 
214
242
  attr_accessor :body, :code
215
243
 
216
- def initialize body
244
+ def initialize body, file_ext=nil
217
245
  @body = body
218
246
  @raw = body
219
- @header = {}
247
+
248
+ encoding = body.encoding rescue "UTF-8"
249
+
250
+ @header = {
251
+ 'Content-Type' => ["#{file_ext}; charset=#{encoding}"]
252
+ }
220
253
  end
221
254
 
222
255
 
@@ -3,6 +3,11 @@ require "kronk"
3
3
  require "mocha"
4
4
 
5
5
 
6
+ def mock_resp name
7
+ File.read File.join("test/mocks", name)
8
+ end
9
+
10
+
6
11
  def mock_200_response
7
12
  @mock_200 ||= File.read 'test/mocks/200_response.txt'
8
13
  end
@@ -324,7 +324,7 @@ class TestRequest < Test::Unit::TestCase
324
324
 
325
325
 
326
326
  def test_call_ssl
327
- expect_request "GET", "https://example.com" do |http, req, resp|
327
+ expr = expect_request "GET", "https://example.com" do |http, req, resp|
328
328
  req.expects(:use_ssl=).with true
329
329
  end
330
330
 
@@ -498,7 +498,8 @@ class TestRequest < Test::Unit::TestCase
498
498
  def expect_request req_method, url, options={}
499
499
  uri = URI.parse url
500
500
 
501
- resp = mock 'resp'
501
+ resp_io = StringIO.new(mock_200_response)
502
+ resp = Kronk::Response.read_new resp_io #mock 'resp'
502
503
  resp.stubs(:code).returns(options[:status] || '200')
503
504
  resp.stubs(:to_hash).returns Hash.new
504
505
 
@@ -524,9 +525,10 @@ class TestRequest < Test::Unit::TestCase
524
525
  Net::HTTP.expects(:new).with(uri.host, uri.port).returns req
525
526
  req.expects(:start).yields(http).returns resp
526
527
 
527
- Kronk::Response.expects(:read_raw_from).returns ["", mock_200_response, 0]
528
+ resp.expects(:read_raw_from).returns [nil, mock_200_response, nil]
528
529
 
529
530
  yield http, req, resp if block_given?
531
+
530
532
  resp
531
533
  end
532
534
  end
@@ -33,7 +33,8 @@ class TestResponse < Test::Unit::TestCase
33
33
 
34
34
  io = StringIO.new str
35
35
 
36
- req, resp, bytes = Kronk::Response.read_raw_from io
36
+ resp = Kronk::Response.read_new StringIO.new(mock_200_response)
37
+ req, resp, bytes = resp.read_raw_from io
37
38
 
38
39
  assert_equal "mock debug request", req
39
40
  assert_equal mock_200_response, resp
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kronk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
7
  - 2
9
- - 0
10
- version: 1.2.0
8
+ - 1
9
+ version: 1.2.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jeremie Castagna
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-01-20 00:00:00 -08:00
17
+ date: 2011-01-21 00:00:00 -08:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 3
32
30
  - 1
@@ -42,7 +40,6 @@ dependencies:
42
40
  requirements:
43
41
  - - ~>
44
42
  - !ruby/object:Gem::Version
45
- hash: 11
46
43
  segments:
47
44
  - 1
48
45
  - 2
@@ -57,7 +54,6 @@ dependencies:
57
54
  requirements:
58
55
  - - ~>
59
56
  - !ruby/object:Gem::Version
60
- hash: 9
61
57
  segments:
62
58
  - 1
63
59
  - 3
@@ -72,7 +68,6 @@ dependencies:
72
68
  requirements:
73
69
  - - ~>
74
70
  - !ruby/object:Gem::Version
75
- hash: 1
76
71
  segments:
77
72
  - 0
78
73
  - 5
@@ -87,7 +82,6 @@ dependencies:
87
82
  requirements:
88
83
  - - ">="
89
84
  - !ruby/object:Gem::Version
90
- hash: 15
91
85
  segments:
92
86
  - 2
93
87
  - 0
@@ -103,7 +97,6 @@ dependencies:
103
97
  requirements:
104
98
  - - ~>
105
99
  - !ruby/object:Gem::Version
106
- hash: 19
107
100
  segments:
108
101
  - 0
109
102
  - 3
@@ -119,7 +112,6 @@ dependencies:
119
112
  requirements:
120
113
  - - ~>
121
114
  - !ruby/object:Gem::Version
122
- hash: 15
123
115
  segments:
124
116
  - 1
125
117
  - 0
@@ -134,7 +126,6 @@ dependencies:
134
126
  requirements:
135
127
  - - ">="
136
128
  - !ruby/object:Gem::Version
137
- hash: 7
138
129
  segments:
139
130
  - 2
140
131
  - 0
@@ -150,7 +141,6 @@ dependencies:
150
141
  requirements:
151
142
  - - ~>
152
143
  - !ruby/object:Gem::Version
153
- hash: 47
154
144
  segments:
155
145
  - 0
156
146
  - 9
@@ -166,7 +156,6 @@ dependencies:
166
156
  requirements:
167
157
  - - ">="
168
158
  - !ruby/object:Gem::Version
169
- hash: 19
170
159
  segments:
171
160
  - 2
172
161
  - 6
@@ -229,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
218
  requirements:
230
219
  - - ">="
231
220
  - !ruby/object:Gem::Version
232
- hash: 3
221
+ hash: -195272185281626433
233
222
  segments:
234
223
  - 0
235
224
  version: "0"
@@ -238,7 +227,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
227
  requirements:
239
228
  - - ">="
240
229
  - !ruby/object:Gem::Version
241
- hash: 3
242
230
  segments:
243
231
  - 0
244
232
  version: "0"