qrcx 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 ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in qrcx.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module Qrcx
2
+ VERSION = "0.1.2"
3
+ end
data/lib/qrcx.rb ADDED
@@ -0,0 +1,54 @@
1
+ module Qrcx
2
+
3
+ class Error < StandardError; end
4
+ class InvalidOrUnsupportedURL < Error; end
5
+ class Timeout < Error; end
6
+ class RedirectURLUnsupported < InvalidOrUnsupportedURL; end
7
+
8
+ #ruby standard library
9
+ require 'net/http'
10
+ require 'cgi'
11
+ require 'uri'
12
+
13
+ #throws QRCX::Error, QRCX::InvalidOrUnsupportedURL (< Error), QRCX::Timeout (< Error)
14
+ # QRCX::RedirectURLUnsupported (< InvalidOrUnsupportedURL)
15
+ #throws all Errors that Net::HTTP.get_response may throw
16
+ def self.shorten(url)
17
+ url = CGI.escape(url)
18
+ retry_count = 0
19
+ begin
20
+ short_url = Net::HTTP.get_response(URI.parse("http://qr.cx/api/?longurl=#{url}")).body
21
+ rescue Timeout::Error
22
+ retry_count = retry_count + 1
23
+ retry if retry_count <= 1
24
+ raise Timeout.new($!)
25
+ end
26
+
27
+ #error handling
28
+ if short_url.include? "error: either unsupported url or the url is not valid"
29
+ response = Net::HTTP.get_response(URI.parse(CGI.unescape(url)))
30
+ raise RedirectURLUnsupported.new("#{response.code} #{response.message} (#{CGI.unescape(url)})") if response.code =~ /3\d\d/
31
+ raise InvalidOrUnsupportedURL
32
+ end
33
+
34
+ raise Error.new(short_url) if short_url.include? "error"
35
+
36
+ return short_url
37
+ end
38
+
39
+ def self.makelong(url)
40
+ url = CGI.escape(url)
41
+ retry_count = 0
42
+
43
+ begin
44
+ short_url = Net::HTTP.get_response(URI.parse("http://qr.cx/api/?get=#{url}")).body
45
+ rescue Timeout::Error
46
+ retry_count = retry_count + 1
47
+ retry if retry_count <= 1
48
+ raise Timeout
49
+ end
50
+ raise Error.new(short_url) if short_url.include? "error"
51
+
52
+ return short_url
53
+ end
54
+ end
data/qrcx.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/qrcx/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "qrcx"
6
+ s.version = Qrcx::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Hartwig Brandl"]
9
+ s.email = ["mail@hartwigbrandl.at"]
10
+ s.homepage = "http://rubygems.org/gems/qrcx"
11
+ s.summary = "A small ruby library to use the url shortener http://qr.cx"
12
+ s.description = "A small ruby library to use the url shortener http://qr.cx"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ # s.rubyforge_project = "qrcx"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
21
+ s.require_path = 'lib'
22
+ end
data/test/test_qrcx.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require 'qrcx'
3
+
4
+ class TestQrcx < Test::Unit::TestCase
5
+ def test_shorten_and_make_long
6
+ long_url = "http://google.com"
7
+ short_url = Qrcx.shorten(long_url)
8
+ assert short_url.include?("http://qr.cx/")
9
+ assert short_url.size > 15
10
+ assert short_url.size < 20
11
+ returned_long_url = Qrcx.makelong(short_url)
12
+ assert_equal long_url, returned_long_url
13
+ end
14
+
15
+ #returned if a 3XX error code is sent by qr.cx
16
+ def test_redirect_url_unsupported
17
+ unsupported_url = "http://qr.cx/VRzY"
18
+ assert_raise (Qrcx::RedirectURLUnsupported) {Qrcx.shorten(unsupported_url)}
19
+ end
20
+
21
+ def test_timeout
22
+ #not yet tested
23
+ #assert_raise (Qrcx::Timeout) {...}
24
+ end
25
+
26
+ def test_invalid_or_unsupported_url
27
+ #not yet tested
28
+ #assert_raise (Qrcx::InvalidOrUnsupportedURL) {...}
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qrcx
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Hartwig Brandl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-07 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A small ruby library to use the url shortener http://qr.cx
38
+ email:
39
+ - mail@hartwigbrandl.at
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Rakefile
50
+ - lib/qrcx.rb
51
+ - lib/qrcx/version.rb
52
+ - qrcx.gemspec
53
+ - test/test_qrcx.rb
54
+ has_rdoc: true
55
+ homepage: http://rubygems.org/gems/qrcx
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 23
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 6
82
+ version: 1.3.6
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A small ruby library to use the url shortener http://qr.cx
90
+ test_files: []
91
+