google_url_shortener 0.0.4 → 0.0.5

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/USAGE ADDED
@@ -0,0 +1,10 @@
1
+ Google URL Shortener
2
+
3
+ Examples:
4
+
5
+ googl install myapikey
6
+ googl {shorten,s} http://example.com
7
+ googl {expand,e} http://goo.gl/1234
8
+ googl expand http://goo.gl/1234 -a
9
+
10
+ Options:
data/bin/googl ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/ruby
2
+ require 'google_url_shortener'
3
+ require 'google/url_shortener/cli'
4
+
5
+ runner = Google::UrlShortener::CLI.new(ARGV)
6
+ runner.run!
@@ -5,6 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.name = "google_url_shortener"
6
6
  s.version = Google::UrlShortener::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
+ s.executables = %w{ googl }
8
9
  s.authors = ["Josh Nesbitt"]
9
10
  s.email = ["josh@josh-nesbitt.net"]
10
11
  s.homepage = "http://rubygems.org/gems/google_url_shortener"
@@ -15,6 +16,7 @@ Gem::Specification.new do |s|
15
16
  s.rubyforge_project = "google_url_shortener"
16
17
  s.add_dependency "json", "1.4.6"
17
18
  s.add_dependency "rest-client", "1.6.1"
19
+ s.add_dependency('trollop', '1.16.2')
18
20
  s.add_development_dependency "bundler", ">= 1.0.0"
19
21
 
20
22
  s.files = `git ls-files`.split("\n")
@@ -0,0 +1,122 @@
1
+ require 'trollop'
2
+ module Google
3
+ module UrlShortener
4
+ class CLI
5
+ KEY_FILE = ".googl"
6
+ KEY_PATH = "#{ENV["HOME"]}/#{KEY_FILE}"
7
+ attr_reader :key, :options
8
+
9
+ def initialize(args)
10
+ @options = parse!(args)
11
+ end
12
+
13
+ def run!
14
+ command = self.options[:command].to_sym
15
+ args = self.options[:args]
16
+
17
+ unless [:install].include?(command)
18
+ validate_key!
19
+ end
20
+
21
+ case command
22
+ when :shorten, :s
23
+ url = validate!(:url, args.first)
24
+ short = UrlShortener.shorten!(url)
25
+
26
+ log(short)
27
+ when :expand, :e
28
+ short = validate!(:url, args.first)
29
+ url = Url.new(:short_url => short)
30
+ url.expand!
31
+
32
+ out = [url.long_url]
33
+
34
+ if self.options[:analytics]
35
+ out << decode_analytics(url)
36
+ out.flatten!
37
+ end
38
+
39
+ log(out)
40
+ when :install
41
+ key = validate!(:key, args.first)
42
+
43
+ install_key!(key)
44
+ log "+ Installed key '#{key}' to #{KEY_PATH}"
45
+ else
46
+ die("unknown command '#{self.options[:command]}'")
47
+ end
48
+ end
49
+
50
+ private
51
+ def decode_analytics(url)
52
+ out = [""]
53
+ groups = [:all, :month, :week, :day, :two_hours]
54
+ sub_groups = [:short_url_clicks, :long_url_clicks, :referrers, :countries, :browsers, :platforms]
55
+
56
+ groups.each do |g|
57
+ out << g.upcase
58
+ group = url.analytics.send(g)
59
+
60
+ sub_groups.each do |s|
61
+ sub = group.send(s)
62
+
63
+ if sub.is_a?(Hash)
64
+ out << "#{" " * 2}#{s}:"
65
+ out << sub.collect do |k, v|
66
+ "#{" " * 4}#{k}: #{v}"
67
+ end
68
+ else
69
+ out << "#{" " * 2}#{s}: #{sub}"
70
+ end
71
+ end
72
+
73
+ out << ""
74
+ end
75
+
76
+ out
77
+ end
78
+
79
+ def parse!(args)
80
+ opts = Trollop::options(args) do
81
+ version VERSION
82
+ banner File.read("#{GEM_ROOT}/USAGE")
83
+
84
+ opt :analytics, "Show analytics when expanding a URL", :short => "-a", :default => false
85
+ end
86
+
87
+ opts.merge!(:command => args.shift)
88
+ opts.merge!(:args => args)
89
+
90
+ die("missing parameter (command)") if opts[:command].nil?
91
+
92
+ opts
93
+ end
94
+
95
+ def install_key!(key)
96
+ File.open(KEY_PATH, 'w') {|f| f.write(key) }
97
+ end
98
+
99
+ def validate_key!
100
+ if File.exists?(KEY_PATH)
101
+ @key = File.read(KEY_PATH)
102
+
103
+ Base.api_key = @key
104
+ else
105
+ die("could not find API key. Please run the 'install' command to initialize.")
106
+ end
107
+ end
108
+
109
+ def log(*things)
110
+ $stdout.puts(*things)
111
+ end
112
+
113
+ def validate!(key, value)
114
+ value.nil? ? die("missing parameter (#{key})") : value
115
+ end
116
+
117
+ def die(message)
118
+ Trollop::die(message)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -9,6 +9,6 @@ module Google
9
9
  end
10
10
  end
11
11
 
12
- class MissingApiKey < UrlShortenerError;end
12
+ class MissingApiKey < UrlShortenerError; end
13
13
  end
14
14
  end
@@ -1,6 +1,6 @@
1
1
  module Google
2
2
  module UrlShortener
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
 
5
5
  end
6
6
  end
data/readme.textile CHANGED
@@ -3,13 +3,18 @@ h1. Google Url Shortener.
3
3
  * Overview
4
4
  * Installation
5
5
  * Usage
6
+ * CLI
6
7
  * Bugs
7
8
 
8
9
 
9
10
 
10
11
  h2. Overview
11
12
 
12
- Google URL Shortener is a library to compress and expand goo.gl URL's. It also provides an interface to review the analytics of a short URL.
13
+ Google URL Shortener is a library to interact with the goo.gl URL shortener API. It provides:
14
+
15
+ * A CLI to easily shorten/expand URL's
16
+ * A clean API to handle shortening/expanding URL's in an application
17
+ * An interface to view analytical data for any short URL
13
18
 
14
19
 
15
20
 
@@ -26,7 +31,7 @@ h2. Usage
26
31
 
27
32
  h3. Setup
28
33
 
29
- You need to provide an API key to use the URL shortener service. To do this set the @api_key@ class variable:
34
+ You need to provide an API key (get one "here":https://code.google.com/apis/console) to use the URL shortener service. To do this set the @api_key@:
30
35
 
31
36
  pre. Google::UrlShortener::Base.api_key = "KEY"
32
37
 
@@ -83,6 +88,53 @@ two_hours
83
88
 
84
89
  E.g: @url.analytics.month@.
85
90
 
91
+
92
+
93
+ h2. CLI
94
+
95
+
96
+ h3. Basic usage
97
+
98
+ There is also a CLI wrapper for Google URL Shortener. Firstly, you need to tell the CLI your API key (get one "here":https://code.google.com/apis/console):
99
+
100
+ pre. googl install AIzaSyByl4x5CMcnm2rNWafmaUz5sljmzMWIgZ0
101
+
102
+ This saves your API key in @~/.googl@.
103
+
104
+ To shorten a URL:
105
+
106
+ pre. googl shorten http://example.com
107
+ googl s http://example.com
108
+
109
+ To expand a URL:
110
+
111
+ pre. googl expand http://goo.gl/1234
112
+ googl e http://goo.gl/1234
113
+
114
+ You can also get analytical data from a short URL using the @-a@ or @--analytics@ flag:
115
+
116
+ pre. googl expand http://goo.gl/1234 -a
117
+
118
+
119
+ h3. Tips
120
+
121
+ You can use the CLI alonsgide something like @pbcopy@ to make shortening URL's crazy fast:
122
+
123
+ pre. googl shorten http://example.com | pbcopy
124
+
125
+ Also use a bash function to simplify it further:
126
+
127
+ pre. shorten (){
128
+ googl shorten $1 | pbcopy
129
+ echo "$1 shortened and copied to clipboard"
130
+ }
131
+
132
+ Which can be used as:
133
+
134
+ pre. shorten http://example.com
135
+
136
+
137
+
86
138
  h2. Bugs
87
139
 
88
140
  If you have any problems with Google Url Shortener, please file an "issue":http://github.com/joshnesbitt/google_url_shortener/issues.
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Josh Nesbitt
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-14 00:00:00 +00:00
17
+ date: 2011-01-19 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,9 +48,24 @@ dependencies:
48
48
  type: :runtime
49
49
  version_requirements: *id002
50
50
  - !ruby/object:Gem::Dependency
51
- name: bundler
51
+ name: trollop
52
52
  prerelease: false
53
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - "="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 16
61
+ - 2
62
+ version: 1.16.2
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: bundler
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
54
69
  none: false
55
70
  requirements:
56
71
  - - ">="
@@ -61,12 +76,12 @@ dependencies:
61
76
  - 0
62
77
  version: 1.0.0
63
78
  type: :development
64
- version_requirements: *id003
79
+ version_requirements: *id004
65
80
  description: Google URL Shortener is a library to compress and expand goo.gl URL's. It also provides an interface to review the analytics of a short URL.
66
81
  email:
67
82
  - josh@josh-nesbitt.net
68
- executables: []
69
-
83
+ executables:
84
+ - googl
70
85
  extensions: []
71
86
 
72
87
  extra_rdoc_files: []
@@ -77,11 +92,14 @@ files:
77
92
  - Gemfile.lock
78
93
  - LICENSE
79
94
  - Rakefile
95
+ - USAGE
96
+ - bin/googl
80
97
  - google_url_shortener.gemspec
81
98
  - lib/google/url_shortener.rb
82
99
  - lib/google/url_shortener/analytics.rb
83
100
  - lib/google/url_shortener/analytics_group.rb
84
101
  - lib/google/url_shortener/base.rb
102
+ - lib/google/url_shortener/cli.rb
85
103
  - lib/google/url_shortener/errors.rb
86
104
  - lib/google/url_shortener/request.rb
87
105
  - lib/google/url_shortener/url.rb