url 0.1.0.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tal Atlas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = URL Gem
2
+
3
+ A simple url object to allow for object oriented based manipulation and usage of a url
4
+
5
+
6
+ === TODO
7
+ * More Documentation
8
+ * More specs
9
+
10
+ == Note on Patches/Pull Requests
11
+
12
+ * Fork the project.
13
+ * Make your feature addition or bug fix.
14
+ * Add tests for it. This is important so I don't break it in a
15
+ future version unintentionally.
16
+ * Commit, do not mess with rakefile, version, or history.
17
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2010 Tal Atlas. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "url"
8
+ gem.summary = %Q{A URL object}
9
+ gem.description = %Q{A simple url object to allow for OO based manipulation and usage of a url}
10
+ gem.email = "me@tal.by"
11
+ gem.homepage = "http://github.com/talby/url"
12
+ gem.authors = ["Tal Atlas"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "url #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
5
+ :build: 1
data/lib/url.rb ADDED
@@ -0,0 +1,109 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require 'uri'
4
+ require 'cgi'
5
+
6
+ files = Dir.glob(File.join(File.dirname(__FILE__),'url','**','*.rb'))
7
+ files.each { |f| require f }
8
+
9
+ #:include: README.rdoc
10
+ class URL
11
+ attr_reader :string, :params
12
+ attr_accessor :subdomain, :domain, :path, :scheme, :format, :port, :hash
13
+ alias_method :subdomains, :subdomain
14
+
15
+ def initialize str
16
+ @string = str
17
+ sp = URI.split(@string)
18
+ @scheme = sp[0]
19
+ @port = sp[3]
20
+ @path = sp[5]
21
+ @format = @path.gsub(/(.+\.)/,'')
22
+ @hash = sp[8]
23
+
24
+ if sp[2]
25
+ host_parts = sp[2].split('.')
26
+ if host_parts[-2] == 'co'
27
+ @domain = host_parts[-3,3].join('.')
28
+ @subdomain = host_parts.first(host_parts.length-3)
29
+ else
30
+ begin
31
+ @domain = host_parts[-2,2].join('.')
32
+ @subdomain = host_parts.first(host_parts.length-2)
33
+ rescue # if there arent at least 2 parts eg: localhost
34
+ @domain = host_parts.join('.')
35
+ end
36
+ end
37
+ else
38
+ @domain = nil
39
+ @subdomain = nil
40
+ end
41
+ if sp[7]
42
+ @params = sp[7].gsub('?','').split('&').inject(ParamsHash.new) do |result,param|
43
+ key,value = param.split('=')
44
+ value = CGI.unescape(value) if value
45
+ result[key.to_sym] = value if key
46
+ result
47
+ end
48
+ else
49
+ @params = ParamsHash.new
50
+ end
51
+ end
52
+
53
+ def host
54
+ [@subdomain,@domain].flatten.compact.join('.')
55
+ end
56
+
57
+ # Outputs the full current url
58
+ def to_s ops={}
59
+ ret = String.new
60
+ ret << %{#{scheme}://} if scheme && ops[:scheme] != false
61
+ ret << host
62
+ ret << %{:#{port}} if port && ops[:port] != false
63
+ if path && ops[:path] != false
64
+ ret << path
65
+ # ret << %{.#{format}} if format && ops[:format] != false
66
+ end
67
+
68
+ ret << params.to_s if params && ops[:params] != false
69
+
70
+ ret << "##{hash.to_s}" if hash && ops[:hash] != false
71
+
72
+ ret
73
+ end
74
+
75
+ # Returns the parsed URI object for the string
76
+ def to_uri
77
+ URI.parse(to_s)
78
+ end
79
+
80
+ class << self
81
+ attr_accessor :req_handler
82
+ end
83
+
84
+ def req_handler #:nodoc:
85
+ self.class.req_handler.new(self)
86
+ end
87
+
88
+ # Performs a get request for the current URL
89
+ def get(*args)
90
+ req_handler.get(*args)
91
+ end
92
+
93
+ # Performs a post request for the current URL
94
+ def post(*args)
95
+ req_handler.post(*args)
96
+ end
97
+
98
+ # Performs a delete request for the current URL
99
+ def delete(*args)
100
+ req_handler.delete(*args)
101
+ end
102
+
103
+ if defined?(Typhoeus)
104
+ self.req_handler = TyHandler
105
+ else
106
+ self.req_handler = NetHandler
107
+ end
108
+ end
109
+
@@ -0,0 +1,98 @@
1
+ class URL
2
+
3
+ class Handler #:nodoc: all
4
+ attr_reader :url
5
+ def initialize(url)
6
+ @url = url
7
+ end
8
+ end
9
+
10
+ class TyHandler < Handler #:nodoc: all
11
+
12
+ def get(args={})
13
+ resp = Typhoeus::Request.get(url.to_s)
14
+
15
+ make_str(resp)
16
+ end
17
+
18
+ def post(args={})
19
+ resp = Typhoeus::Request.post(url.to_s(:params => false), :params => url.params)
20
+
21
+ make_str(resp)
22
+ end
23
+
24
+ def delete
25
+ resp = Typhoeus::Request.delete(url.to_s)
26
+ make_str(resp)
27
+ end
28
+
29
+ private
30
+
31
+ def make_str(resp)
32
+ hsh = {
33
+ :code => resp.code,
34
+ :time => resp.time,
35
+ :body => resp.body,
36
+ :response => resp,
37
+ :url => url.to_s
38
+ }
39
+
40
+ Response.new(hsh)
41
+ end
42
+
43
+ end
44
+
45
+ class NetHandler < Handler #:nodoc: all
46
+ def get(args={})
47
+ puts 'net'
48
+ http = http_obj
49
+ request = Net::HTTP::Get.new(url.path + url.params.to_s)
50
+ t = Time.now
51
+ resp = http.request(request)
52
+ make_str(resp,Time.now-t)
53
+ end
54
+
55
+ def post(args={})
56
+ http = http_obj
57
+ request = Net::HTTP::Post.new(url.path)
58
+ request.set_form_data(url.params)
59
+ t = Time.now
60
+ resp = http.request(request)
61
+ make_str(resp,Time.now-t)
62
+ end
63
+
64
+ def delete(args={})
65
+ http = http_obj
66
+ request = Net::HTTP::Delete.new(url.path + url.params.to_s)
67
+ t = Time.now
68
+ resp = http.request(request)
69
+ make_str(resp,Time.now-t)
70
+ end
71
+
72
+ private
73
+
74
+ def make_str(resp,time)
75
+ hsh = {
76
+ :code => resp.code.to_i,
77
+ :time => time,
78
+ :body => resp.body,
79
+ :response => resp,
80
+ :url => url.to_s
81
+ }
82
+
83
+ Response.new(hsh)
84
+ end
85
+
86
+ def http_obj
87
+ uri = url.to_uri
88
+ http = Net::HTTP.new(uri.host,uri.port)
89
+
90
+ if url.scheme == 'https'
91
+ http.use_ssl = true
92
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
93
+ end
94
+
95
+ http
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,46 @@
1
+ require "delegate"
2
+
3
+ class URL
4
+
5
+ class Response < DelegateClass(String) #:nodoc: all
6
+ attr_reader :body,:time,:code,:response
7
+ def initialize(str,args={})
8
+ if str.is_a?(Hash)
9
+ args = str
10
+ str = args[:body]
11
+ end
12
+
13
+ raise unless str
14
+ super(str)
15
+ args.each do |key, value|
16
+ instance_variable_set "@#{key}", value
17
+ end
18
+ end
19
+
20
+ def success?
21
+ return @successful if @successful
22
+
23
+ code == 200
24
+ end
25
+ end
26
+
27
+ class ParamsHash < Hash
28
+
29
+ # Merges the array into a parameter string of the form <tt>?key=value&foo=bar</tt>
30
+ def to_s
31
+ return '' if empty?
32
+ '?' + to_a.inject(Array.new) do |ret,param|
33
+ val = param[1].to_s
34
+
35
+ val = CGI.escape(val)# if val =~ /(\/|\?|\s)/
36
+
37
+ if param && val
38
+ ret << %{#{param[0].to_s}=#{val}}
39
+ elsif param
40
+ ret << param[0].to_s
41
+ end
42
+ ret
43
+ end.join('&')
44
+ end
45
+ end
46
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'url'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
data/spec/url_spec.rb ADDED
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe URL do
4
+ before do
5
+ @url = URL.new('https://mail.google.com:8080/foo/bar/baz?q=one&foo=bar')
6
+ end
7
+
8
+ it "should roundtrip" do
9
+ @url.to_s.should == 'https://mail.google.com:8080/foo/bar/baz?q=one&foo=bar'
10
+ end
11
+
12
+ it "should parse properly" do
13
+ @url.params.should == {:q => 'one', :foo => 'bar'}
14
+ @url.host.should == 'mail.google.com'
15
+ @url.domain.should == 'google.com'
16
+ @url.subdomain.should == ['mail']
17
+ @url.port.should == '8080'
18
+ @url.scheme.should == 'https'
19
+ end
20
+
21
+ it "should change and add params" do
22
+ @url.params[:foo] = 'foo'
23
+ @url.params[:baz] = 'baz'
24
+ @url.port = '90'
25
+ @url.subdomain = 'test'
26
+ @url.scheme = 'ftp'
27
+ @url.path = '/bar/baz'
28
+
29
+ @url.to_s.should include 'ftp://test.google.com:90/bar/baz?'
30
+ @url.to_s.should include 'q=one'
31
+ @url.to_s.should include 'foo=foo'
32
+ @url.to_s.should include 'baz=baz'
33
+ end
34
+
35
+ it "should work with strange urls" do
36
+ url = URL.new('http://one.mail.google.co.uk')
37
+ url.domain.should == 'google.co.uk'
38
+ url.subdomains.should == ['one','mail']
39
+
40
+ url = URL.new('http://localhost')
41
+ url.domain.should == 'localhost'
42
+ url.subdomain.should be_nil
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url
3
+ version: !ruby/object:Gem::Version
4
+ hash: 69
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ - 1
11
+ version: 0.1.0.1
12
+ platform: ruby
13
+ authors:
14
+ - Tal Atlas
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-11-24 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rspec
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 13
31
+ segments:
32
+ - 1
33
+ - 2
34
+ - 9
35
+ version: 1.2.9
36
+ type: :development
37
+ version_requirements: *id001
38
+ description: A simple url object to allow for OO based manipulation and usage of a url
39
+ email: me@tal.by
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ files:
48
+ - .document
49
+ - .gitignore
50
+ - LICENSE
51
+ - README.rdoc
52
+ - Rakefile
53
+ - VERSION.yml
54
+ - lib/url.rb
55
+ - lib/url/handlers.rb
56
+ - lib/url/helper_classes.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ - spec/url_spec.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/talby/url
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: A URL object
94
+ test_files:
95
+ - spec/spec_helper.rb
96
+ - spec/url_spec.rb