svenaas-trim 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,6 +30,14 @@ Windows users should omit the sudo, of course.
30
30
  require 'trim'
31
31
 
32
32
  Trim.trim('http://www.google.com') # Returns trimmed URL, or nil if any error occurs
33
+
34
+ If you want to use an account that tr.im knows about you can provide
35
+ your valid username and password:
36
+
37
+ Trim.trim('http://www.google.com', 'username', 'password')
38
+
39
+ NOTE: as of this writing (3 Mar 2009) tr.im does not support SSL so
40
+ this username and password will be sent over unencrypted HTTP.
33
41
 
34
42
  == Testing
35
43
 
@@ -7,12 +7,10 @@ require 'libxml'
7
7
  module Trim
8
8
 
9
9
  #
10
- # Alias for Trim::trim_simple
10
+ # Alias for #Trim::trim_simple
11
11
  #
12
- def Trim::trim(url)
13
- response = nil
14
- open("http://api.tr.im/api/trim_simple?url=#{url}") {|http| response = http.read }
15
- response.strip!.size > 0 ? response : nil
12
+ def Trim::trim(url, username = nil, password = nil)
13
+ trim_simple(url, username, password)
16
14
  end
17
15
 
18
16
  #
@@ -20,11 +18,22 @@ module Trim
20
18
  #
21
19
  # trimmed_url = Trim.trim_simple('http://sample.com/some/long/url')
22
20
  #
23
- # Utilizes the tr.im API method trim_simple
21
+ # If a +username+ and +password+ are provided they will be passed to tr.im
22
+ # for authentication. NOTE: as of this writing (3 Mar 2009) tr.im does
23
+ # not support SSL so this username and password will be sent over
24
+ # unencrypted HTTP.
25
+ #
26
+ # If the username and password provided are not valid, tr.tim will still
27
+ # generate a shortened URL, but will do so as if no username and password
28
+ # were provided. The account credentials can be verified with #Trim::valid_account?
29
+ #
30
+ # This method utilizes the tr.im API method trim_simple
24
31
  #
25
- def Trim::trim_simple(url)
32
+ def Trim::trim_simple(url, username = nil, password = nil)
26
33
  response = nil
27
- open("http://api.tr.im/api/trim_simple?url=#{url}") {|http| response = http.read }
34
+ request_url = "http://api.tr.im/api/trim_simple?url=#{url}"
35
+ request_url += "&username=#{username}&password=#{password}" if (username and password)
36
+ open(request_url) {|http| response = http.read }
28
37
  response.strip!.size > 0 ? response : nil
29
38
  end
30
39
 
@@ -32,14 +41,44 @@ module Trim
32
41
  # Returns a trimmed URL based on +url+, or nil if any error occurs.
33
42
  #
34
43
  # trimmed_url = Trim.trim_url('http://sample.com/some/long/url')
44
+ #
45
+ # If a +username+ and +password+ are provided they will be passed to tr.im
46
+ # for authentication. NOTE: as of this writing (3 Mar 2009) tr.im does
47
+ # not support SSL so this username and password will be sent over
48
+ # unencrypted HTTP.
49
+ #
50
+ # If the username and password provided are not valid, tr.tim will still
51
+ # generate a shortened URL, but will do so as if no username and password
52
+ # were provided. The account credentials can be verified with #Trim::valid_account?
35
53
  #
36
54
  # Utilizes the tr.im API method trim_url.xml
37
55
  #
38
- def Trim::trim_url(url)
56
+ def Trim::trim_url(url, username = nil, password = nil)
39
57
  xml_response = nil
40
- open("http://api.tr.im/api/trim_url.xml?url=#{url}") {|http| xml_response = http.read }
58
+ request_url = "http://api.tr.im/api/trim_url.xml?url=#{url}"
59
+ request_url += "&username=#{username}&password=#{password}" if (username and password)
60
+ open(request_url) {|http| xml_response = http.read }
41
61
  get_url_from_xml_response(xml_response)
42
62
  end
63
+
64
+ #
65
+ # Verifies that the provided +username+ and +password+ successfully
66
+ # validate as an account known to tr.im
67
+ #
68
+ # is_valid_account = Trim.valid_account?('good_username', 'good_password')
69
+ #
70
+ # NOTE: as of this writing (3 Mar 2009) tr.im does not support SSL so this
71
+ # username and password will be sent over unencrypted HTTP.
72
+ #
73
+ # Utilizes the tr.im API method verify.xml
74
+ #
75
+ def Trim::valid_account?(username, password)
76
+ xml_response = nil
77
+ request_url = "http://api.tr.im/api/verify.xml?username=#{username}&password=#{password}"
78
+ open(request_url) {|http| xml_response = http.read }
79
+ status_code = get_status_code_from_xml_response(xml_response)
80
+ "200" == status_code
81
+ end
43
82
 
44
83
  private
45
84
  def Trim::get_url_from_xml_response(xml_response)
@@ -47,4 +86,9 @@ private
47
86
  urls = parser.parse.find('url')
48
87
  urls.size > 0 ? urls.first.content : nil
49
88
  end
89
+
90
+ def Trim::get_status_code_from_xml_response(xml_response)
91
+ parser = LibXML::XML::Parser.string xml_response
92
+ status = parser.parse.root.attributes['code']
93
+ end
50
94
  end
@@ -2,8 +2,8 @@ module Trim
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 1
6
- TINY = 3
5
+ MINOR = 2
6
+ TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -4,99 +4,142 @@ require 'mocha'
4
4
  class TrimTest < Test::Unit::TestCase
5
5
 
6
6
  VALID_SHORTENED_URL = "http://tr.im/OK"
7
-
8
- context "A valid URL" do
9
-
10
- setup do
11
- @long_url = 'http://www.google.com/'
12
- end
13
-
14
- context "A trim_simple request" do
7
+
8
+ context "With a valid account" do
9
+ setup do
10
+ @username = 'user'
11
+ @password = 'pass'
12
+ @long_url = 'http://www.google.com/'
13
+ end
14
+
15
+ context "account verification" do
16
+ should "succeed" do
17
+ Trim.expects(:valid_account?).with(@username, @password).returns(true)
18
+ assert Trim.valid_account?(@username, @password)
19
+ end
20
+ end
21
+
22
+ context "a trim_simple request" do
15
23
  should "provide a working shortened URL" do
16
- Trim.expects(:trim_simple).with(@long_url).returns(VALID_SHORTENED_URL)
17
- trimmed_url = Trim.trim_simple @long_url
24
+ Trim.expects(:trim_simple).with(@long_url, @username, @password).returns(VALID_SHORTENED_URL)
25
+ trimmed_url = Trim.trim_simple @long_url, @username, @password
18
26
  assert_equal VALID_SHORTENED_URL, trimmed_url
19
27
  end
20
28
  end
21
29
 
22
- context "A trim_url request" do
23
- should "provide a working shortened URL" do
24
- Trim.expects(:trim_url).with(@long_url).returns(VALID_SHORTENED_URL)
25
- trimmed_url = Trim.trim_url @long_url
26
- assert_equal VALID_SHORTENED_URL, trimmed_url
27
- end
28
- end
29
-
30
- end
31
-
32
- context "An invalid URL" do
33
-
30
+ context "a trim_url request" do
31
+ should "provide a working shortened URL" do
32
+ Trim.expects(:trim_url).with(@long_url, @username, @password).returns(VALID_SHORTENED_URL)
33
+ trimmed_url = Trim.trim_url @long_url, @username, @password
34
+ assert_equal VALID_SHORTENED_URL, trimmed_url
35
+ end
36
+ end
37
+ end
38
+
39
+ context "With an invalid account" do
34
40
  setup do
35
- @long_url = 'www'
41
+ @username = 'baduser'
42
+ @password = 'badpass'
36
43
  end
37
-
38
- context "A trim_simple request" do
39
- should "return nil" do
40
- Trim.expects(:trim_simple).with(@long_url).returns(nil)
41
- trimmed_url = Trim.trim_simple @long_url
42
- assert_nil trimmed_url
44
+
45
+ context "account verification" do
46
+ should "fail" do
47
+ Trim.expects(:valid_account?).with(@username, @password).returns(false)
48
+ assert ! Trim.valid_account?(@username, @password)
43
49
  end
44
- end
45
-
46
- context "A trim_url request" do
47
- should "return nil" do
48
- Trim.expects(:trim_url).with(@long_url).returns(nil)
49
- trimmed_url = Trim.trim_url @long_url
50
- assert_nil trimmed_url
50
+ end
51
+ end
52
+
53
+ context "Anonymously" do
54
+ context "using a valid URL" do
55
+ setup do
56
+ @long_url = 'http://www.google.com/'
57
+ end
58
+
59
+ context "a trim_simple request" do
60
+ should "provide a working shortened URL" do
61
+ Trim.expects(:trim_simple).with(@long_url).returns(VALID_SHORTENED_URL)
62
+ trimmed_url = Trim.trim_simple @long_url
63
+ assert_equal VALID_SHORTENED_URL, trimmed_url
64
+ end
65
+ end
66
+
67
+ context "a trim_url request" do
68
+ should "provide a working shortened URL" do
69
+ Trim.expects(:trim_url).with(@long_url).returns(VALID_SHORTENED_URL)
70
+ trimmed_url = Trim.trim_url @long_url
71
+ assert_equal VALID_SHORTENED_URL, trimmed_url
72
+ end
51
73
  end
74
+
52
75
  end
53
-
76
+
77
+ context "using an invalid URL" do
78
+ setup do
79
+ @long_url = 'www'
80
+ end
81
+
82
+ context "a trim_simple request" do
83
+ should "return nil" do
84
+ Trim.expects(:trim_simple).with(@long_url).returns(nil)
85
+ trimmed_url = Trim.trim_simple @long_url
86
+ assert_nil trimmed_url
87
+ end
88
+ end
89
+
90
+ context "a trim_url request" do
91
+ should "return nil" do
92
+ Trim.expects(:trim_url).with(@long_url).returns(nil)
93
+ trimmed_url = Trim.trim_url @long_url
94
+ assert_nil trimmed_url
95
+ end
96
+ end
97
+
54
98
  end
55
-
56
- context "An empty URL" do
57
99
 
58
- setup do
100
+ context "using an empty URL" do
101
+ setup do
59
102
  @long_url = ''
60
103
  end
61
-
62
- context "A trim_simple request" do
63
- should "return nil" do
64
- Trim.expects(:trim_simple).with('').returns(nil)
65
- trimmed_url = Trim.trim_simple @long_url
66
- assert_nil trimmed_url
104
+
105
+ context "a trim_simple request" do
106
+ should "return nil" do
107
+ Trim.expects(:trim_simple).with('').returns(nil)
108
+ trimmed_url = Trim.trim_simple @long_url
109
+ assert_nil trimmed_url
110
+ end
67
111
  end
68
- end
69
-
70
- context "A trim_url request" do
71
- should "return nil" do
72
- Trim.expects(:trim_url).with('').returns(nil)
73
- trimmed_url = Trim.trim_url @long_url
74
- assert_nil trimmed_url
112
+
113
+ context "a trim_url request" do
114
+ should "return nil" do
115
+ Trim.expects(:trim_url).with('').returns(nil)
116
+ trimmed_url = Trim.trim_url @long_url
117
+ assert_nil trimmed_url
118
+ end
75
119
  end
120
+
76
121
  end
77
-
78
- end
79
-
80
- context "A nil URL" do
81
122
 
82
- setup do
123
+ context "using a nil URL" do
124
+ setup do
83
125
  @long_url = nil
84
126
  end
85
-
86
- context "A trim_simple request" do
87
- should "return nil" do
88
- trimmed_url = Trim.trim @long_url
89
- assert_nil trimmed_url
90
- end
91
- end
92
-
93
- context "A trim_url request" do
94
- should "return nil" do
95
- trimmed_url = Trim.trim @long_url
96
- assert_nil trimmed_url
127
+
128
+ context "a trim_simple request" do
129
+ should "return nil" do
130
+ Trim.expects(:trim_simple).with(nil).returns(nil)
131
+ trimmed_url = Trim.trim_simple @long_url
132
+ assert_nil trimmed_url
133
+ end
97
134
  end
135
+
136
+ context "a trim_url request" do
137
+ should "return nil" do
138
+ Trim.expects(:trim_url).with(nil).returns(nil)
139
+ trimmed_url = Trim.trim_url @long_url
140
+ assert_nil trimmed_url
141
+ end
142
+ end
98
143
  end
99
-
100
144
  end
101
-
102
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svenaas-trim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Aas