googlus 0.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/.hgignore ADDED
@@ -0,0 +1,5 @@
1
+ syntax: glob
2
+ pkg/*
3
+ html/*
4
+ *.gem
5
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in googl.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,49 @@
1
+ -*-markdown-*-
2
+
3
+ Googlus
4
+ =======
5
+
6
+ Description
7
+ -----------
8
+
9
+ Ruby wrapper for Google URL Shortener REST API.
10
+
11
+ Features:
12
+
13
+ - shorten URLs
14
+ - expand URLs
15
+ - analyse URLs usage
16
+
17
+ Installation
18
+ ------------
19
+
20
+ gem install googlus
21
+
22
+ Usage
23
+ -----
24
+
25
+ In your Ruby code:
26
+
27
+ Googlus::Shorten.new(long_url).short_url
28
+ Googlus::Expand.new(short_url).url
29
+
30
+ On the command-line:
31
+
32
+ $ googlus -s http://www.bounga.org # Shorten URL
33
+ $ googlus -e http://goo.gl/arOIJ # Expand short URL
34
+ $ googlus -a http://goo.gl/arOIJ # Get analytics for short URL
35
+
36
+ Other
37
+ -----
38
+
39
+ If you want to contribute you should take a look at:
40
+
41
+ - [Source Style](http://www.bitbucket.org/Bounga/googlus/SourceStyle)
42
+ - [Ticket Guidelines](http://www.bitbucket.org/Bounga/googlus/wiki/TicketGuidelines)
43
+ - [Contributing](http://www.bitbucket.org/Bounga/gus/googlus/Contributing)
44
+
45
+ You can have a look at [RDoc](http://rubydoc.info/gems/googlus/) too.
46
+
47
+ Problems, comments, and suggestions are welcome on the [ticket system](http://www.bitbucket.org/Bounga/googlus/issues/new/).
48
+
49
+ Copyright (c) 2011 Nicolas Cavigneaux, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'rdoc/task'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ Rake::RDocTask.new(:rdoc_dev) do |rd|
6
+ rd.main = "README"
7
+ rd.rdoc_files.include("README", "lib/**/*.rb")
8
+ rd.options << "--all"
9
+ end
data/bin/googlus ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "googlus"
5
+ require "optparse"
6
+
7
+ action = :shorten
8
+ opts = OptionParser.new
9
+
10
+ opts.banner = "Usage: #{File.basename($0)} [options] URL"
11
+ opts.separator ""
12
+ opts.on("-s", "--shorten", "Shorten URL") { action = :shorten }
13
+ opts.on("-e", "--expand", "Expand SHORT_URL") { action = :expand }
14
+ opts.on("-a", "--analytics", "Analytics for SHORT_URL") { action = :analytics }
15
+ opts.on("-v", "--version", "Show #{Googlus::NAME} version") { puts Googlus::NAME + " v" + Googlus::VERSION; exit(0) }
16
+ opts.on("-L", "--license", "Show #{Googlus::NAME} license") { puts Googlus::LICENSE; exit(0) }
17
+ opts.on("-V", "--verbose", "Run #{Googlus::NAME} in verbose mode") { $VERBOSE = true }
18
+ opts.on_tail("-h", "--help", "Show this help") { puts opts.to_s; exit(0) }
19
+
20
+ if ARGV.empty?
21
+ puts opts.to_s
22
+ exit(1)
23
+ end
24
+
25
+ url = opts.parse(ARGV)
26
+
27
+ case action
28
+ when :shorten
29
+ puts Googlus::Shorten.new(url).short_url
30
+ when :expand
31
+ puts Googlus::Expand.new(url).url
32
+ when :analytics
33
+ puts Googlus::Analytic.new(url).to_s
34
+ end
35
+
36
+ exit(0)
data/googlus.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "googlus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "googlus"
7
+ s.version = Googlus::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nicolas Cavigneaux"]
10
+ s.email = ["nico@bounga.org"]
11
+ s.homepage = "https://bitbucket.org/Bounga/googlus/"
12
+ s.summary = %q{Ruby wrapper for Google URL Shortener REST API}
13
+ s.description = %q{Provides an easy access to Google URL Shortener API using Ruby code or command-line}
14
+
15
+ s.add_development_dependency "rspec"
16
+ s.add_development_dependency "rdoc"
17
+ s.add_dependency "json_pure"
18
+
19
+ s.rubyforge_project = "Googlus"
20
+
21
+ s.files = `hg manifest`.split("\n")
22
+ s.test_files = `hg manifest`.split("\n").select { |f| f =~ /^(test|spec|features)/ }
23
+ s.executables = `hg manifest`.split("\n").select { |f| f =~ /^bin/ }.map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
data/lib/googlus.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ # Wrapper around Google URL Shortener API
6
+ module Googlus
7
+
8
+ end
9
+
10
+ class Net::HTTP
11
+ alias_method :old_initialize, :initialize
12
+ def initialize(*args)
13
+ old_initialize(*args)
14
+ @ssl_context = OpenSSL::SSL::SSLContext.new
15
+ @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
16
+ end
17
+ end
18
+
19
+ Dir.glob('lib/googlus/*.rb').each { |f| require f }
@@ -0,0 +1,27 @@
1
+ module Googlus
2
+ # Retrieve analytics for given short URL
3
+ class Analytic
4
+ attr_reader :response
5
+
6
+ # Short URL to analyze
7
+ def initialize(url)
8
+ http = Net::HTTP.new("www.googleapis.com", 443)
9
+ http.use_ssl = true
10
+
11
+ resp, body = http.get("/urlshortener/v1/url?shortUrl=#{url}&projection=FULL")
12
+ body = JSON.parse(body)
13
+
14
+ if resp.code.to_i == 200
15
+ @response = body
16
+ else
17
+ error = body["error"]
18
+ raise "Request error: #{error["code"]} - #{error["message"]}"
19
+ end
20
+ end
21
+
22
+ # Pretty printed version of analytics info
23
+ def to_s
24
+ @response.inspect
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module Googlus
2
+ # Expands Google shorten URLs
3
+ class Expand
4
+ attr_reader :short_url, :url
5
+
6
+ # Short URL to expand
7
+ def initialize(url)
8
+ http = Net::HTTP.new("www.googleapis.com", 443)
9
+ http.use_ssl = true
10
+
11
+ resp, body = http.get("/urlshortener/v1/url?shortUrl=#{url}")
12
+ body = JSON.parse(body)
13
+
14
+ if resp.code.to_i == 200
15
+ @short_url = body["id"]
16
+ @url = body["longUrl"]
17
+ @kind = body["kind"]
18
+ @status = body["status"]
19
+ else
20
+ error = body["error"]
21
+ raise "Expanding error: #{error["code"]} - #{error["message"]}"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module Googlus
2
+ # Shorten a URL
3
+ class Shorten
4
+ attr_reader :short_url, :url
5
+
6
+ # Long URL to shorten
7
+ def initialize(url)
8
+ http = Net::HTTP.new("www.googleapis.com", 443)
9
+ http.use_ssl = true
10
+
11
+ params = {'longUrl' => url}.to_json
12
+ resp, body = http.post("/urlshortener/v1/url", params, {'Content-Type' => 'application/json'})
13
+ body = JSON.parse(body)
14
+
15
+ if resp.code.to_i == 200
16
+ @short_url = body["id"]
17
+ @url = body["longUrl"]
18
+ else
19
+ error = body["error"]
20
+ raise "Shortening error: #{error["code"]} - #{error["message"]}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Googlus
2
+ NAME = "Googlus"
3
+ VERSION = "0.0.1"
4
+ LICENSE = "Copyright (c) 2011 Nicolas Cavigneaux, released under the MIT license"
5
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: googlus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Nicolas Cavigneaux
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-18 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rdoc
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: json_pure
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ description: Provides an easy access to Google URL Shortener API using Ruby code or command-line
64
+ email:
65
+ - nico@bounga.org
66
+ executables:
67
+ - googlus
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .hgignore
74
+ - Gemfile
75
+ - README
76
+ - Rakefile
77
+ - bin/googlus
78
+ - googlus.gemspec
79
+ - lib/googlus.rb
80
+ - lib/googlus/analytic.rb
81
+ - lib/googlus/expand.rb
82
+ - lib/googlus/shorten.rb
83
+ - lib/googlus/version.rb
84
+ has_rdoc: true
85
+ homepage: https://bitbucket.org/Bounga/googlus/
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project: Googlus
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Ruby wrapper for Google URL Shortener REST API
118
+ test_files: []
119
+