google_url_shortener 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +1,65 @@
1
- require 'trollop'
1
+ require 'trollop'
2
2
  module Google
3
3
  module UrlShortener
4
4
  class CLI
5
5
  KEY_FILE = ".googl"
6
6
  KEY_PATH = "#{ENV["HOME"]}/#{KEY_FILE}"
7
7
  attr_reader :key, :options
8
-
8
+
9
9
  def initialize(args)
10
10
  @options = parse!(args)
11
11
  end
12
-
12
+
13
13
  def run!
14
14
  command = self.options[:command].to_sym
15
15
  args = self.options[:args]
16
-
16
+
17
17
  unless [:install].include?(command)
18
18
  validate_key!
19
19
  end
20
-
20
+
21
21
  case command
22
22
  when :shorten, :s
23
23
  url = validate!(:url, args.first)
24
24
  short = UrlShortener.shorten!(url)
25
-
25
+
26
26
  log(short)
27
27
  when :expand, :e
28
28
  short = validate!(:url, args.first)
29
29
  url = Url.new(:short_url => short)
30
30
  url.expand!
31
-
31
+
32
32
  out = [url.long_url]
33
-
33
+
34
34
  if self.options[:analytics]
35
35
  out << decode_analytics(url)
36
36
  out.flatten!
37
37
  end
38
-
38
+
39
39
  log(out)
40
40
  when :install
41
41
  key = validate!(:key, args.first)
42
-
42
+
43
43
  install_key!(key)
44
44
  log "+ Installed key '#{key}' to #{KEY_PATH}"
45
45
  else
46
46
  die("unknown command '#{self.options[:command]}'")
47
47
  end
48
48
  end
49
-
49
+
50
50
  private
51
51
  def decode_analytics(url)
52
52
  out = [""]
53
53
  groups = [:all, :month, :week, :day, :two_hours]
54
54
  sub_groups = [:short_url_clicks, :long_url_clicks, :referrers, :countries, :browsers, :platforms]
55
-
55
+
56
56
  groups.each do |g|
57
- out << g.upcase
57
+ out << g.to_s.upcase
58
58
  group = url.analytics.send(g)
59
-
59
+
60
60
  sub_groups.each do |s|
61
61
  sub = group.send(s)
62
-
62
+
63
63
  if sub.is_a?(Hash)
64
64
  out << "#{" " * 2}#{s}:"
65
65
  out << sub.collect do |k, v|
@@ -69,51 +69,51 @@ module Google
69
69
  out << "#{" " * 2}#{s}: #{sub}"
70
70
  end
71
71
  end
72
-
72
+
73
73
  out << ""
74
74
  end
75
-
75
+
76
76
  out
77
77
  end
78
-
78
+
79
79
  def parse!(args)
80
80
  opts = Trollop::options(args) do
81
81
  version VERSION
82
82
  banner File.read("#{GEM_ROOT}/USAGE")
83
-
83
+
84
84
  opt :analytics, "Show analytics when expanding a URL", :short => "-a", :default => false
85
85
  end
86
-
86
+
87
87
  opts.merge!(:command => args.shift)
88
88
  opts.merge!(:args => args)
89
-
89
+
90
90
  die("missing parameter (command)") if opts[:command].nil?
91
-
91
+
92
92
  opts
93
93
  end
94
-
94
+
95
95
  def install_key!(key)
96
96
  File.open(KEY_PATH, 'w') {|f| f.write(key) }
97
97
  end
98
-
98
+
99
99
  def validate_key!
100
100
  if File.exists?(KEY_PATH)
101
101
  @key = File.read(KEY_PATH)
102
-
102
+
103
103
  Base.api_key = @key
104
104
  else
105
105
  die("could not find API key. Please run the 'install' command to initialize.")
106
106
  end
107
107
  end
108
-
108
+
109
109
  def log(*things)
110
110
  $stdout.puts(*things)
111
111
  end
112
-
112
+
113
113
  def validate!(key, value)
114
114
  value.nil? ? die("missing parameter (#{key})") : value
115
115
  end
116
-
116
+
117
117
  def die(message)
118
118
  Trollop::die(message)
119
119
  end
@@ -3,27 +3,27 @@ module Google
3
3
  module Request
4
4
  BASE_URL = "https://www.googleapis.com/urlshortener/v1/url"
5
5
  REQUEST_HEADERS = { :content_type => :json, :accept => :json }
6
-
6
+
7
7
  def post(params={})
8
8
  response = RestClient.post(BASE_URL, format_post_params(params), REQUEST_HEADERS)
9
9
  parse(response)
10
10
  end
11
-
11
+
12
12
  def get(params={})
13
13
  full_url = [BASE_URL, "?", format_get_params(params)].join
14
14
  response = RestClient.get(full_url)
15
15
  parse(response)
16
16
  end
17
-
17
+
18
18
  private
19
19
  def parse(response)
20
20
  JSON.parse(response)
21
21
  end
22
-
22
+
23
23
  def format_post_params(params={})
24
24
  params.to_json
25
25
  end
26
-
26
+
27
27
  def format_get_params(params={})
28
28
  params.collect { |k, v| "#{k}=#{v}" }.join("&")
29
29
  end
@@ -1,6 +1,6 @@
1
1
  module Google
2
2
  module UrlShortener
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
 
5
5
  end
6
6
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: google_url_shortener
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.6
5
+ version: 0.0.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Josh Nesbitt
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-02-02 00:00:00 +00:00
14
- default_executable:
13
+ date: 2012-07-31 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: json
@@ -105,7 +104,6 @@ files:
105
104
  - spec/lib/google/url_spec.rb
106
105
  - spec/lib/url_shortener_spec.rb
107
106
  - spec/spec_helper.rb
108
- has_rdoc: true
109
107
  homepage: http://rubygems.org/gems/google_url_shortener
110
108
  licenses: []
111
109
 
@@ -129,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
127
  requirements: []
130
128
 
131
129
  rubyforge_project: google_url_shortener
132
- rubygems_version: 1.6.2
130
+ rubygems_version: 1.8.17
133
131
  signing_key:
134
132
  specification_version: 3
135
133
  summary: A simple library to shorten and expand goo.gl URL's.