url 0.1.1 → 0.1.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.
- data/.gitignore +3 -0
- data/README.rdoc +33 -1
- data/VERSION.yml +1 -1
- data/lib/url.rb +26 -8
- data/lib/url/handlers.rb +15 -1
- data/lib/url/helper_classes.rb +21 -1
- data/spec/url_spec.rb +104 -31
- metadata +3 -3
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= URL Gem
|
2
2
|
|
3
|
-
A simple url object to allow for object oriented based manipulation and usage of a url
|
3
|
+
A simple url object to allow for object oriented based manipulation and usage of a url. It works with Typhoeus if it detects it's installed
|
4
|
+
or defaults back to Net::HTTP.
|
4
5
|
|
5
6
|
== Usage
|
6
7
|
|
@@ -21,9 +22,40 @@ You can easily extract or change any part of the url
|
|
21
22
|
url.params[:foo] = 'bar'
|
22
23
|
url.to_s # => 'https://my.mail.google.com/mail/?foo=bar&shva=1#mbox'
|
23
24
|
|
25
|
+
=== Make Requests
|
26
|
+
|
27
|
+
You can quickly and easily make requests from those urls without having to worry about whether they're https or not.
|
28
|
+
|
29
|
+
url = URL.new('https://graph.facebook.com/37901410')
|
30
|
+
response = url.get
|
31
|
+
response # => "{"id":"37901410","name":"Tal Atlas","first_name":"Tal","last_name":"Atlas","link":"http:\\/\\/www.facebook.com\\/talatlas","gender":"male","locale":"en_US"}"
|
32
|
+
|
33
|
+
A {URL::Response} object is returned from {URL#post}, {URL#get}, or {URL#delete}. This is basically a string with a few additional methods including:
|
34
|
+
|
35
|
+
The time the request took in seconds
|
36
|
+
The http code of the response and whether the request was successful or not (code == 200).
|
37
|
+
|
38
|
+
response.time # => 1.6
|
39
|
+
response.code # => 200
|
40
|
+
response.success? # => true
|
41
|
+
|
42
|
+
You can make GET, POST, or DELETE requests without doing any special formatting.
|
43
|
+
|
44
|
+
url.path << '/feed'
|
45
|
+
url.params[:access_token] = '.....'
|
46
|
+
url.params[:message] = "Hey I'm posting this message with ruby"
|
47
|
+
json = url.post
|
48
|
+
|
49
|
+
post_id = magic_method(json)
|
50
|
+
|
51
|
+
url.path = "/#{post_id}"
|
52
|
+
url.params.delete(:message)
|
53
|
+
url.delete
|
54
|
+
|
24
55
|
== TODO
|
25
56
|
* More Documentation
|
26
57
|
* More specs
|
58
|
+
* Make faster
|
27
59
|
|
28
60
|
== Note on Patches/Pull Requests
|
29
61
|
|
data/VERSION.yml
CHANGED
data/lib/url.rb
CHANGED
@@ -6,12 +6,28 @@ require 'cgi'
|
|
6
6
|
files = Dir.glob(File.join(File.dirname(__FILE__),'url','**','*.rb'))
|
7
7
|
files.each { |f| require f }
|
8
8
|
|
9
|
-
|
9
|
+
# Main class for managing urls
|
10
|
+
# url = URL.new('https://mail.google.com/mail/?shva=1#mbox')
|
11
|
+
# url.params # => {:shva => '1'}
|
12
|
+
# url.scheme # => 'https'
|
13
|
+
# url.host # => 'mail.google.com'
|
14
|
+
# url.domain # => 'google.com'
|
15
|
+
# url.subdomain # => ['mail']
|
16
|
+
# url.path # => '/mail/'
|
17
|
+
# url.hash # => 'mbox'
|
18
|
+
#
|
19
|
+
# url.subdomain = ['my','mail']
|
20
|
+
# url.params[:foo] = 'bar'
|
21
|
+
# url.to_s # => 'https://my.mail.google.com/mail/?foo=bar&shva=1#mbox'
|
10
22
|
class URL
|
11
23
|
attr_reader :string, :params
|
24
|
+
|
25
|
+
# Attributes of the URL which are editable
|
12
26
|
attr_accessor :subdomain, :domain, :path, :scheme, :format, :port, :hash
|
13
27
|
alias_method :subdomains, :subdomain
|
14
28
|
|
29
|
+
# Creates a new URL object
|
30
|
+
# @param [String] URL the starting url to work with
|
15
31
|
def initialize str
|
16
32
|
@string = str
|
17
33
|
sp = URI.split(@string)
|
@@ -55,7 +71,7 @@ class URL
|
|
55
71
|
end
|
56
72
|
|
57
73
|
# Outputs the full current url
|
58
|
-
# @param [Hash
|
74
|
+
# @param [Hash] ops Prevent certain parts of the object from being shown by setting `:scheme`,`:port`,`:path`,`:params`, or `:hash` to `false`
|
59
75
|
# @return [String]
|
60
76
|
def to_s ops={}
|
61
77
|
ret = String.new
|
@@ -64,7 +80,6 @@ class URL
|
|
64
80
|
ret << %{:#{port}} if port && ops[:port] != false
|
65
81
|
if path && ops[:path] != false
|
66
82
|
ret << path
|
67
|
-
# ret << %{.#{format}} if format && ops[:format] != false
|
68
83
|
end
|
69
84
|
|
70
85
|
ret << params.to_s if params && ops[:params] != false
|
@@ -81,13 +96,11 @@ class URL
|
|
81
96
|
end
|
82
97
|
|
83
98
|
class << self
|
99
|
+
# Define the request handler to use. If Typhoeus is setup it will use {TyHandler} otherwise will default back to Net::HTTP with {NetHandler}
|
100
|
+
# @return [Handler]
|
84
101
|
attr_accessor :req_handler
|
85
102
|
end
|
86
103
|
|
87
|
-
def req_handler #:nodoc:
|
88
|
-
self.class.req_handler.new(self)
|
89
|
-
end
|
90
|
-
|
91
104
|
# Performs a get request for the current URL
|
92
105
|
# @return [URL::Response] A subclass of string which also repsonds to a few added mthods storing more information
|
93
106
|
def get(*args)
|
@@ -106,7 +119,7 @@ class URL
|
|
106
119
|
req_handler.delete(*args)
|
107
120
|
end
|
108
121
|
|
109
|
-
def inspect
|
122
|
+
def inspect
|
110
123
|
"#<URL #{to_s}>"
|
111
124
|
end
|
112
125
|
|
@@ -115,5 +128,10 @@ class URL
|
|
115
128
|
else
|
116
129
|
URL.req_handler = NetHandler
|
117
130
|
end
|
131
|
+
|
132
|
+
private
|
133
|
+
def req_handler
|
134
|
+
self.class.req_handler.new(self)
|
135
|
+
end
|
118
136
|
end
|
119
137
|
|
data/lib/url/handlers.rb
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
class URL
|
2
2
|
|
3
|
+
# Handlers for making requests for the URL. To create your own you need to follow the following conventions:
|
4
|
+
# 1. You must define the get, post, and delete instance methods
|
5
|
+
# 2. These methods should return a {Response} object
|
6
|
+
#
|
7
|
+
# To create a {Response} object:
|
8
|
+
# hsh = {
|
9
|
+
# :code => resp.code,
|
10
|
+
# :time => resp.time,
|
11
|
+
# :body => resp.body,
|
12
|
+
# :response => resp,
|
13
|
+
# :url => url.to_s
|
14
|
+
# }
|
15
|
+
# Response.new(hsh)
|
3
16
|
class Handler
|
4
17
|
attr_reader :url
|
5
18
|
def initialize(url)
|
@@ -7,6 +20,7 @@ class URL
|
|
7
20
|
end
|
8
21
|
end
|
9
22
|
|
23
|
+
# Typhoeus handler
|
10
24
|
class TyHandler < Handler
|
11
25
|
|
12
26
|
def get(args={})
|
@@ -42,9 +56,9 @@ class URL
|
|
42
56
|
|
43
57
|
end
|
44
58
|
|
59
|
+
# Net::HTTP Handler
|
45
60
|
class NetHandler < Handler
|
46
61
|
def get(args={})
|
47
|
-
puts 'net'
|
48
62
|
http = http_obj
|
49
63
|
request = Net::HTTP::Get.new(url.path + url.params.to_s)
|
50
64
|
t = Time.now
|
data/lib/url/helper_classes.rb
CHANGED
@@ -9,8 +9,26 @@ class URL
|
|
9
9
|
# * response - the original response object from whatever handler you chose
|
10
10
|
# * time - time taken to make call
|
11
11
|
# * success? - whether the http code is 200
|
12
|
+
# * url - the URL the object was gotten from
|
12
13
|
class Response < DelegateClass(String)
|
13
|
-
|
14
|
+
# The time taken for the request
|
15
|
+
# @returns [Integer]
|
16
|
+
attr_reader :time
|
17
|
+
|
18
|
+
# The http code return
|
19
|
+
# @returns [Integer] eg. 200, 404, 500, 503
|
20
|
+
attr_reader :code
|
21
|
+
|
22
|
+
# The response object generated by the handler
|
23
|
+
# @returns [Net::HTTPResponse,Typhoeus::Response]
|
24
|
+
attr_reader :response
|
25
|
+
|
26
|
+
# The url which generated this response
|
27
|
+
# @returns [String]
|
28
|
+
attr_reader :url
|
29
|
+
|
30
|
+
# @param [String] body The body of the response object, main string
|
31
|
+
# @param [Hash] args Additional arguments: :time,:code,:response,:url
|
14
32
|
def initialize(str,args={})
|
15
33
|
if str.is_a?(Hash)
|
16
34
|
args = str
|
@@ -24,6 +42,8 @@ class URL
|
|
24
42
|
end
|
25
43
|
end
|
26
44
|
|
45
|
+
# Compares {Response#code} to 200
|
46
|
+
# @returns [true,false]
|
27
47
|
def success?
|
28
48
|
return @successful if @successful
|
29
49
|
|
data/spec/url_spec.rb
CHANGED
@@ -5,40 +5,113 @@ describe URL do
|
|
5
5
|
@url = URL.new('https://mail.google.com:8080/foo/bar/baz?q=one&foo=bar')
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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'
|
8
|
+
describe "#initialize" do
|
9
|
+
it "should parse properly" do
|
10
|
+
@url.params.should == {:q => 'one', :foo => 'bar'}
|
11
|
+
@url.host.should == 'mail.google.com'
|
12
|
+
@url.domain.should == 'google.com'
|
13
|
+
@url.subdomain.should == ['mail']
|
14
|
+
@url.port.should == '8080'
|
15
|
+
@url.scheme.should == 'https'
|
16
|
+
end
|
28
17
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
18
|
+
it "should work with strange urls" do
|
19
|
+
url = URL.new('http://one.mail.google.co.uk')
|
20
|
+
url.domain.should == 'google.co.uk'
|
21
|
+
url.subdomains.should == ['one','mail']
|
22
|
+
|
23
|
+
url = URL.new('http://localhost')
|
24
|
+
url.domain.should == 'localhost'
|
25
|
+
url.subdomain.should be_nil
|
26
|
+
end
|
33
27
|
end
|
34
28
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
describe '#to_s' do
|
30
|
+
it "should roundtrip" do
|
31
|
+
@url.to_s.should == 'https://mail.google.com:8080/foo/bar/baz?q=one&foo=bar'
|
32
|
+
end
|
39
33
|
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
it "should change and add params" do
|
35
|
+
@url.params[:foo] = 'foo'
|
36
|
+
@url.params[:baz] = 'baz'
|
37
|
+
@url.port = '90'
|
38
|
+
@url.subdomain = 'test'
|
39
|
+
@url.scheme = 'ftp'
|
40
|
+
@url.path = '/bar/baz'
|
41
|
+
|
42
|
+
@url.to_s.should include 'ftp://test.google.com:90/bar/baz?'
|
43
|
+
@url.to_s.should include 'q=one'
|
44
|
+
@url.to_s.should include 'foo=foo'
|
45
|
+
@url.to_s.should include 'baz=baz'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
shared_examples_for "all requests" do
|
52
|
+
it "should work" do
|
53
|
+
@resp.should be_success
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should have a response of the correct class" do
|
57
|
+
@resp.response.should be_a(@resp_class)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "shoudl have all attribures" do
|
61
|
+
@resp.time.should be_a(Float)
|
62
|
+
@resp.code.should be_a(Integer)
|
63
|
+
@resp.url.should be_a(String)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
shared_examples_for "all builds" do
|
68
|
+
|
69
|
+
|
70
|
+
before do
|
71
|
+
@url = URL.new('http://www.omgpop.com/')
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#get" do
|
75
|
+
before do
|
76
|
+
@resp = @url.get
|
77
|
+
end
|
78
|
+
it_should_behave_like "all requests"
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#post" do
|
82
|
+
before do
|
83
|
+
@resp = @url.post
|
84
|
+
end
|
85
|
+
it_should_behave_like "all requests"
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#delete" do
|
89
|
+
before do
|
90
|
+
@resp = @url.delete
|
91
|
+
end
|
92
|
+
it_should_behave_like "all requests"
|
43
93
|
end
|
44
94
|
end
|
95
|
+
|
96
|
+
describe "Typhoeus", URL do
|
97
|
+
before(:all) do
|
98
|
+
require 'typhoeus'
|
99
|
+
URL.req_handler = URL::TyHandler
|
100
|
+
@resp_class = Typhoeus::Response
|
101
|
+
end
|
102
|
+
|
103
|
+
it_should_behave_like "all builds"
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
describe "Net::HTTP", URL do
|
109
|
+
before(:all) do
|
110
|
+
require 'net/http'
|
111
|
+
URL.req_handler = URL::NetHandler
|
112
|
+
@resp_class = Net::HTTPResponse
|
113
|
+
end\
|
114
|
+
|
115
|
+
it_should_behave_like "all builds"
|
116
|
+
|
117
|
+
end
|
metadata
CHANGED