curb-openuri 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -24,16 +24,7 @@ Rake::RDocTask.new do |rdoc|
24
24
  rdoc.rdoc_files.include('lib/**/*.rb')
25
25
  end
26
26
 
27
- require 'spec/rake/spectask'
28
- Spec::Rake::SpecTask.new(:spec) do |t|
29
- t.libs << 'lib' << 'spec'
30
- t.spec_files = FileList['spec/**/*_spec.rb']
31
- end
32
-
33
- Spec::Rake::SpecTask.new(:rcov) do |t|
34
- t.libs << 'lib' << 'spec'
35
- t.spec_files = FileList['spec/**/*_spec.rb']
36
- t.rcov = true
37
- end
27
+ require 'rspec/core/rake_task'
28
+ RSpec::Core::RakeTask.new
38
29
 
39
30
  task :default => :spec
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
- :patch: 0
3
- :minor: 1
4
2
  :major: 0
3
+ :minor: 2
4
+ :patch: 0
5
+ :build:
data/curb-openuri.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{curb-openuri}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roman Shterenzon"]
12
- s.date = %q{2009-11-29}
12
+ s.date = %q{2010-11-10}
13
13
  s.description = %q{open-uri drop-in replacement that uses curb}
14
14
  s.email = %q{romanbsd@yahoo.com}
15
15
  s.extra_rdoc_files = [
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
32
32
  s.homepage = %q{http://github.com/romanbsd/curb-openuri}
33
33
  s.rdoc_options = ["--charset=UTF-8"]
34
34
  s.require_paths = ["lib"]
35
- s.rubygems_version = %q{1.3.5}
35
+ s.rubygems_version = %q{1.3.7}
36
36
  s.summary = %q{open-uri drop-in replacement that uses curb}
37
37
  s.test_files = [
38
38
  "spec/curb_openuri_spec.rb",
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
44
44
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
45
  s.specification_version = 3
46
46
 
47
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
48
  s.add_runtime_dependency(%q<curb>, [">= 0.1.4"])
49
49
  else
50
50
  s.add_dependency(%q<curb>, [">= 0.1.4"])
data/lib/curl_agent.rb CHANGED
@@ -76,7 +76,9 @@ class CurlAgent
76
76
  agent = CurlAgent.new(name, options)
77
77
 
78
78
  agent.perform!
79
- io = StringIO.new(agent.body_str)
79
+ io = IO.new(agent.body_str, agent.header_str)
80
+ io.base_uri = URI.parse(agent.last_effective_url) rescue nil
81
+ io.status = [agent.response_code, '']
80
82
  if block
81
83
  block.call(io)
82
84
  else
@@ -93,4 +95,32 @@ class CurlAgent
93
95
  end
94
96
  return mode, perm, rest
95
97
  end
96
- end
98
+
99
+ class IO < StringIO
100
+ # returns an Array which consists status code and message.
101
+ attr_accessor :status
102
+
103
+ # returns a URI which is base of relative URIs in the data.
104
+ # It may differ from the URI supplied by a user because redirection.
105
+ attr_accessor :base_uri
106
+
107
+ def initialize(body_str, header_str)
108
+ super(body_str)
109
+ @header_str = header_str
110
+ end
111
+
112
+ # returns a Hash which represents header fields.
113
+ # The Hash keys are downcased for canonicalization.
114
+ def meta
115
+ @meta ||= begin
116
+ arr = @header_str.split(/\r?\n/)
117
+ arr.shift
118
+ arr.inject({}) do |hash, hdr|
119
+ key, val = hdr.split(/:\s+/, 2)
120
+ hash[key.downcase] = val
121
+ hash
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'uri'
2
3
 
3
4
  describe "CurlAgent" do
4
5
 
@@ -62,6 +63,7 @@ EOF
62
63
  describe 'when used with open' do
63
64
  before(:each) do
64
65
  @headers = {'User-Agent'=>'foo'}
66
+ @url = 'http://www.example.com/'
65
67
  @curl_easy = mock('curl_easy')
66
68
  Curl::Easy.should_receive(:new).and_return(@curl_easy)
67
69
  @curl_easy.stub!(:headers).and_return(@headers)
@@ -72,23 +74,41 @@ EOF
72
74
  @curl_easy.stub!(:timeout=)
73
75
  @curl_easy.stub!(:perform)
74
76
  @curl_easy.stub!(:body_str).and_return('test')
77
+ @curl_easy.stub!(:response_code).and_return(200)
78
+ @curl_easy.stub!(:last_effective_url).and_return(@url)
79
+ @curl_easy.stub!(:header_str).and_return("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nServer: Apache")
75
80
  end
76
81
 
77
82
  it 'shall permit to specify user-agent' do
78
83
  @curl_easy.headers['User-Agent'].should_not == 'curl'
79
- CurlAgent.open('http://www.example.com/', 'User-Agent'=>'curl')
84
+ CurlAgent.open(@url, 'User-Agent'=>'curl')
80
85
  @curl_easy.headers['User-Agent'].should == 'curl'
81
86
  end
82
87
 
83
88
  it 'shall permit to override timeout' do
84
89
  @curl_easy.should_receive(:'timeout=').once.with(10)
85
- CurlAgent.open('http://www.example.com/', :timeout => 10)
90
+ CurlAgent.open(@url, :timeout => 10)
86
91
  end
87
92
 
88
93
  it 'shall use block when provided' do
89
- CurlAgent.open('http://www.example.com/') {|f| f.read}.should == 'test'
94
+ CurlAgent.open(@url) {|f| f.read}.should == 'test'
90
95
  end
91
96
 
97
+ it 'shall return io object which responds to base_uri' do
98
+ io = CurlAgent.open(@url)
99
+ io.should respond_to(:base_uri)
100
+ io.base_uri.should be_a_kind_of(URI)
101
+ io.base_uri.to_s.should == @url
102
+ end
103
+
104
+ it 'shall return io object which responds to meta' do
105
+ io = CurlAgent.open(@url)
106
+ io.should respond_to(:meta)
107
+ meta = io.meta
108
+ meta.should be_a_kind_of(Hash)
109
+ meta.should have(2).keys
110
+ meta['server'].should == 'Apache'
111
+ end
92
112
  end
93
113
 
94
114
  describe 'when parsing parameters to open' do
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,5 @@
1
- require 'spec'
1
+ require 'rspec'
2
2
 
3
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
 
5
- require 'curb_openuri'
6
-
7
- Spec::Runner.configure do |config|
8
-
9
- end
5
+ require 'curb_openuri'
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curb-openuri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Roman Shterenzon
@@ -9,19 +14,24 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-29 00:00:00 +02:00
17
+ date: 2010-11-10 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: curb
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 1
31
+ - 4
23
32
  version: 0.1.4
24
- version:
33
+ type: :runtime
34
+ version_requirements: *id001
25
35
  description: open-uri drop-in replacement that uses curb
26
36
  email: romanbsd@yahoo.com
27
37
  executables: []
@@ -53,21 +63,25 @@ rdoc_options:
53
63
  require_paths:
54
64
  - lib
55
65
  required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
56
67
  requirements:
57
68
  - - ">="
58
69
  - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
59
72
  version: "0"
60
- version:
61
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
62
75
  requirements:
63
76
  - - ">="
64
77
  - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
65
80
  version: "0"
66
- version:
67
81
  requirements: []
68
82
 
69
83
  rubyforge_project:
70
- rubygems_version: 1.3.5
84
+ rubygems_version: 1.3.7
71
85
  signing_key:
72
86
  specification_version: 3
73
87
  summary: open-uri drop-in replacement that uses curb