google_url_shortener 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :gemcutter
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google_url_shortener (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ bundler (>= 1.0.0)
15
+ google_url_shortener!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Josh Nesbitt <josh@josh-nesbitt.net>
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/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/google/url_shortener/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "google_url_shortener"
6
+ s.version = Google::UrlShortener::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Josh Nesbitt"]
9
+ s.email = ["josh@josh-nesbitt.net"]
10
+ s.homepage = "http://rubygems.org/gems/google_url_shortener"
11
+ s.summary = "A simple library to shorten and expand goo.gl URL's."
12
+ s.description = "Google URL Shortener is a library to compress and expand goo.gl URL's. It also provides an interface to view analytics of any given URL."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "google_url_shortener"
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
+
23
+ end
File without changes
@@ -0,0 +1,24 @@
1
+ # TODO: remove
2
+ require 'rubygems'
3
+ require 'restclient'
4
+ require 'json'
5
+ # EO TODO
6
+
7
+ module Google
8
+ module UrlShortener
9
+ class Base
10
+ URL = "https://www.googleapis.com/urlshortener/v1/url"
11
+
12
+ class << self
13
+
14
+ def api_key=(key)
15
+ @@api_key = key
16
+ end
17
+
18
+ def api_key
19
+ @@api_key
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ module Google
2
+ module UrlShortener
3
+ class Url
4
+ attr_reader :long_url, :short_url, :status
5
+
6
+ def initialize(opts={})
7
+ opts.each_pair do |k, v|
8
+ self.instance_variable_set(:"@#{k}", v)
9
+ end
10
+ end
11
+
12
+ def decode!
13
+ # check for short
14
+ resp = get(:shortUrl => self.short_url, :projection => "FULL")
15
+ # TODO: get more info from response
16
+ @long_url = resp["longUrl"]
17
+ end
18
+
19
+ def encode!
20
+ # check for long
21
+ resp = post(:longUrl => self.long_url)
22
+ @short_url = resp["id"]
23
+ end
24
+
25
+ private
26
+ def post(params={})
27
+ # check for API key
28
+ params.merge!(:key => Base.api_key)
29
+ json = RestClient.post Base::URL, params.to_json, :content_type => :json, :accept => :json
30
+ JSON.parse(json)
31
+ end
32
+
33
+ def get(params={})
34
+ # check for API key
35
+ params.merge!(:key => Base.api_key)
36
+
37
+ json = RestClient.get Base::URL + "?" + params.collect { |k, v| "#{k}=#{v}" }.join("&")
38
+ puts json
39
+ JSON.parse(json)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ module Google
2
+ module UrlShortener
3
+ VERSION = "0.0.1"
4
+
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ require 'google/url_shortener/version'
2
+ require 'google/url_shortener/base'
3
+ require 'google/url_shortener/url'
4
+
5
+ RestClient.log = $stdout
6
+
7
+ Google::UrlShortener::Base.api_key = "AIzaSyCDp22BuCNkkd_LwQmnzOKA-kA7GXjvOsI"
8
+
9
+ url = Google::UrlShortener::Url.new(:long_url => "http://blog.josh-nesbitt.net")
10
+ puts url.encode!.inspect
11
+
12
+ url = Google::UrlShortener::Url.new(:short_url => url.short_url)
13
+ puts url.decode!.inspect
14
+
15
+ # http://code.google.com/apis/urlshortener/v1/reference.html#method_urlshortener_url_get
16
+
data/readme.textile ADDED
@@ -0,0 +1,46 @@
1
+ h1. Google Url Shortener.
2
+
3
+ * Overview
4
+ * Installation
5
+ * Usage
6
+ * Bugs
7
+
8
+
9
+
10
+ h2. Overview
11
+
12
+
13
+
14
+ h2. Installation
15
+
16
+ The project is hosted on rubygems.org. Getting it is simple:
17
+
18
+ gem install google_url_shortener
19
+
20
+
21
+
22
+ h2. Usage
23
+
24
+
25
+
26
+ h2. Bugs
27
+
28
+ If you have any problems with Google Url Shortener, please file an "issue":http://github.com/joshnesbitt/google_url_shortener/issues.
29
+
30
+
31
+
32
+ h2. Note on Patches/Pull Requests
33
+
34
+ * Fork the project.
35
+ * Make your feature addition or bug fix.
36
+ * Add tests for it. This is important so I don't break it in a
37
+ future version unintentionally.
38
+ * Commit, do not mess with rakefile, version, or history.
39
+ (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)
40
+ * Send me a pull request. Bonus points for topic branches.
41
+
42
+
43
+
44
+ h2. Copyright
45
+
46
+ Copyright (c) 2010 Josh Nesbitt <josh@josh-nesbitt.net>. See LICENSE for details.
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_url_shortener
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
+ - Josh Nesbitt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-14 00:00:00 +00: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: Google URL Shortener is a library to compress and expand goo.gl URL's. It also provides an interface to view analytics of any given URL.
38
+ email:
39
+ - josh@josh-nesbitt.net
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - Rakefile
52
+ - google_url_shortener.gemspec
53
+ - lib/google/url_shortener/analytics.rb
54
+ - lib/google/url_shortener/base.rb
55
+ - lib/google/url_shortener/url.rb
56
+ - lib/google/url_shortener/version.rb
57
+ - lib/google_url_shortener.rb
58
+ - readme.textile
59
+ has_rdoc: true
60
+ homepage: http://rubygems.org/gems/google_url_shortener
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 23
83
+ segments:
84
+ - 1
85
+ - 3
86
+ - 6
87
+ version: 1.3.6
88
+ requirements: []
89
+
90
+ rubyforge_project: google_url_shortener
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A simple library to shorten and expand goo.gl URL's.
95
+ test_files: []
96
+