google-short-links 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -40,7 +40,7 @@ RDoc::Task.new do |rdoc|
40
40
  version = File.exist?('VERSION') ? File.read('VERSION') : ''
41
41
 
42
42
  rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "cleverbot #{version}"
43
+ rdoc.title = "google-short-links #{version}"
44
44
  rdoc.rdoc_files.include('README*')
45
45
  rdoc.rdoc_files.include(File.join('lib', '**', '*.rb'))
46
46
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{google-short-links}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benjamin Manns"]
@@ -1,3 +1,5 @@
1
+ # Holds the client class for Google Short Links.
2
+ # See GoogleShortLinks::Client.
1
3
  module GoogleShortLinks
2
4
  autoload :Client, 'google_short_links/client'
3
5
  autoload :Parser, 'google_short_links/parser'
@@ -6,19 +6,63 @@ require 'uri'
6
6
 
7
7
  require 'httparty'
8
8
 
9
+ # Client for Google Short Links.
10
+ #
11
+ # ==== Examples
12
+ #
13
+ # client = GoogleShortLinks::Client.new :server => 'short.example.com', :secret => 'abcdef1234567890', :email => 'ben@example.com'
14
+ # link = client.get_or_create_hash 'http://example.com/parent/child/long_document_name.html', :is_public => true
15
+ # link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>98, "shortcut"=>"abc12", "usage_count"=>0, "owner"=>"ben@example.com", "is_public"=>true, "is_hash"=>true}
9
16
  class GoogleShortLinks::Client
10
17
  include HTTParty
11
18
 
12
- attr_accessor :server, :secret, :email
19
+ # The domain name where Google Short Links is hosted.
20
+ attr_accessor :server
21
+
22
+ # Your HMAC-SHA1 secret.
23
+ attr_accessor :secret
24
+
25
+ # The email for the account under which the links will be created.
26
+ attr_accessor :email
13
27
 
14
28
  parser GoogleShortLinks::Parser
15
29
 
30
+ # Initializes a new Client.
31
+ #
32
+ # ==== Parameters
33
+ #
34
+ # [<tt>options</tt>] Optional Hash containing configuration values.
35
+ #
36
+ # ==== Options
37
+ #
38
+ # [:server]
39
+ # The domain name where Google Short Links is hosted.
40
+ # [:secret]
41
+ # Your HMAC-SHA1 secret.
42
+ # [:email]
43
+ # The email for the account under which the links will be created.
16
44
  def initialize options={}
17
45
  self.server = options[:server]
18
46
  self.secret = options[:secret]
19
47
  self.email = options[:email]
20
48
  end
21
49
 
50
+ # Creates a URL that is used to create a hash.
51
+ #
52
+ # ==== Parameters
53
+ #
54
+ # [<tt>url</tt>] Required String containing the URL to be shortened.
55
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
56
+ #
57
+ # ==== Parameters
58
+ #
59
+ # [:is_public]
60
+ # Boolean for whether or not the short link can be accessed without logging in to Google Apps.
61
+ #
62
+ # ==== Examples
63
+ #
64
+ # link_url = client.get_or_create_hash_url 'http://example.com/parent/child/long_document_name.html', :is_public => true
65
+ # link_url # => "http://short.example.com/js/get_or_create_hash?is_public=true&oauth_signature_method=HMAC-SHA1&timestamp=1321663459.0&url=http%3A%2F%2Fexample.com%2Fparent%2Fchild%2Flong_document_name.html&user=ben%40example.com&oauth_signature=JElQ0Oq59q%2BOsinYuMzGX%2F8Tn2U%3D"
22
66
  def get_or_create_hash_url url, params={}
23
67
  request_path = request_path(:get_or_create_hash)
24
68
 
@@ -27,6 +71,23 @@ class GoogleShortLinks::Client
27
71
  digest_url(request_path, query)
28
72
  end
29
73
 
74
+ # Creates a URL that is used to create a shortlink with a customized shortcut.
75
+ #
76
+ # ==== Parameters
77
+ #
78
+ # [<tt>url</tt>] Required String containing the URL to be shortened.
79
+ # [<tt>shortcut</tt>] Required String containing the custom shortcut.
80
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
81
+ #
82
+ # ==== Parameters
83
+ #
84
+ # [:is_public]
85
+ # Boolean for whether or not the short link can be accessed without logging in to Google Apps.
86
+ #
87
+ # ==== Examples
88
+ #
89
+ # link_url = client.get_or_create_shortlink_url 'http://example.com/parent/child/long_document_name.html', 'example', :is_public => true
90
+ # link_url # => "http://short.example.com/js/get_or_create_shortlink?is_public=true&oauth_signature_method=HMAC-SHA1&shortcut=example&timestamp=1321663613.0&url=http%3A%2F%2Fexample.com%2Fparent%2Fchild%2Flong_document_name.html&user=ben%40example.com&oauth_signature=FsOEjCDw3qpz%2B59Pdi4d1Qvv0Os%3D"
30
91
  def get_or_create_shortlink_url url, shortcut, params={}
31
92
  request_path = request_path(:get_or_create_shortlink)
32
93
 
@@ -35,6 +96,17 @@ class GoogleShortLinks::Client
35
96
  digest_url(request_path, query)
36
97
  end
37
98
 
99
+ # Creates a URL that is used to get details about a link from its shortcut.
100
+ #
101
+ # ==== Parameters
102
+ #
103
+ # [<tt>shortcut</tt>] Required String containing the URL to be shortened.
104
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
105
+ #
106
+ # ==== Examples
107
+ #
108
+ # link_url = client.get_details_url 'example'
109
+ # link_url # => "http://short.example.com/js/get_details?oauth_signature_method=HMAC-SHA1&shortcut=example&timestamp=1321663629.0&user=ben%40example.com&oauth_signature=p8XqzF0lITupKN3Ta0qu2E8hqBI%3D"
38
110
  def get_details_url shortcut, params={}
39
111
  request_path = request_path(:get_details)
40
112
 
@@ -43,25 +115,69 @@ class GoogleShortLinks::Client
43
115
  digest_url(request_path, query)
44
116
  end
45
117
 
118
+ # Shortens a URL.
119
+ #
120
+ # ==== Parameters
121
+ #
122
+ # [<tt>url</tt>] Required String containing the URL to be shortened.
123
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
124
+ #
125
+ # ==== Parameters
126
+ #
127
+ # [:is_public]
128
+ # Boolean for whether or not the short link can be accessed without logging in to Google Apps.
129
+ #
130
+ # ==== Examples
131
+ #
132
+ # link = client.get_or_create_hash 'http://example.com/parent/child/long_document_name.html', :is_public => true
133
+ # link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>98, "shortcut"=>"abc12", "usage_count"=>0, "owner"=>"ben@example.com", "is_public"=>true, "is_hash"=>true}
46
134
  def get_or_create_hash url, params={}
47
135
  self.class.get(get_or_create_hash_url(url, params)).parsed_response
48
136
  end
49
137
 
138
+ # Creates a shortlink with a customized shortcut.
139
+ #
140
+ # ==== Parameters
141
+ #
142
+ # [<tt>url</tt>] Required String containing the URL to be shortened.
143
+ # [<tt>shortcut</tt>] Required String containing the custom shortcut.
144
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
145
+ #
146
+ # ==== Parameters
147
+ #
148
+ # [:is_public]
149
+ # Boolean for whether or not the short link can be accessed without logging in to Google Apps.
150
+ #
151
+ # ==== Examples
152
+ #
153
+ # link = client.get_or_create_shortlink 'http://example.com/parent/child/long_document_name.html', 'example', :is_public => true
154
+ # link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>99, "shortcut"=>"example", "usage_count"=>0, "owner"=>"ben@example.com", "is_public"=>true, "is_hash"=>false}
50
155
  def get_or_create_shortlink url, shortcut, params={}
51
156
  self.class.get(get_or_create_shortlink_url(url, shortcut, params)).parsed_response
52
157
  end
53
158
 
159
+ # Gets details about a link from its shortcut.
160
+ #
161
+ # ==== Parameters
162
+ #
163
+ # [<tt>shortcut</tt>] Required String containing the URL to be shortened.
164
+ # [<tt>params</tt>] Optional Hash that will add or override GET parameters in the URL.
165
+ #
166
+ # ==== Examples
167
+ #
168
+ # link = client.get_details 'example'
169
+ # link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>100, "shortcut"=>"example", "usage_count"=>0, "owner"=>"ben@example.com", "is_public"=>true, "is_hash"=>false}
54
170
  def get_details shortcut, params={}
55
171
  self.class.get(get_details_url(shortcut, params)).parsed_response
56
172
  end
57
173
 
58
174
  protected
59
175
 
60
- def request_path action
176
+ def request_path action #:nodoc:
61
177
  "http://#{server}/js/#{action}"
62
178
  end
63
179
 
64
- def base_params
180
+ def base_params #:nodoc:
65
181
  {
66
182
  :oauth_signature_method => 'HMAC-SHA1',
67
183
  :timestamp => Time.now.to_i.to_f,
@@ -69,11 +185,11 @@ class GoogleShortLinks::Client
69
185
  }
70
186
  end
71
187
 
72
- def params_to_query params
188
+ def params_to_query params #:nodoc:
73
189
  params.to_a.sort_by { |param| param.to_s }.map { |(key, value)| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join('&')
74
190
  end
75
191
 
76
- def digest request_path, query
192
+ def digest request_path, query #:nodoc:
77
193
  content = "GET&#{CGI.escape(request_path)}&#{CGI.escape(query)}"
78
194
 
79
195
  digest = OpenSSL::Digest::Digest.new('sha1')
@@ -81,7 +197,7 @@ class GoogleShortLinks::Client
81
197
  CGI.escape(Base64.encode64(digest_raw).chomp)
82
198
  end
83
199
 
84
- def digest_url request_path, query
200
+ def digest_url request_path, query #:nodoc:
85
201
  request_uri = URI.parse(request_path)
86
202
  request_uri.query = "#{query}&oauth_signature=#{digest(request_path, query)}"
87
203
  request_uri.to_s
@@ -1,9 +1,13 @@
1
1
  require 'httparty'
2
2
  require 'multi_json'
3
3
 
4
+ # Parses the JSON that Google Short Links returns.
4
5
  class GoogleShortLinks::Parser < HTTParty::Parser
6
+ # Google returns the JSON response as a text/html mime type.
7
+ # This class only supports JSON currently.
5
8
  SupportedFormats = { 'text/html' => :json }
6
9
 
10
+ # Decodes JSON into a hash.
7
11
  def json
8
12
  MultiJson.decode(body)
9
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-short-links
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
17
- requirement: &22456740 !ruby/object:Gem::Requirement
17
+ requirement: &26633460 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *22456740
25
+ version_requirements: *26633460
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: multi_json
28
- requirement: &22456100 !ruby/object:Gem::Requirement
28
+ requirement: &26632780 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *22456100
36
+ version_requirements: *26632780
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: jeweler
39
- requirement: &22455460 !ruby/object:Gem::Requirement
39
+ requirement: &26632180 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *22455460
47
+ version_requirements: *26632180
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rcov
50
- requirement: &22454860 !ruby/object:Gem::Requirement
50
+ requirement: &26631580 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *22454860
58
+ version_requirements: *26631580
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rdoc
61
- requirement: &22454260 !ruby/object:Gem::Requirement
61
+ requirement: &26630980 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *22454260
69
+ version_requirements: *26630980
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rspec
72
- requirement: &22453640 !ruby/object:Gem::Requirement
72
+ requirement: &26630340 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *22453640
80
+ version_requirements: *26630340
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: timecop
83
- requirement: &22453020 !ruby/object:Gem::Requirement
83
+ requirement: &26629740 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *22453020
91
+ version_requirements: *26629740
92
92
  description: Ruby wrapper for Google Short Links.
93
93
  email: benmanns@gmail.com
94
94
  executables: []
@@ -131,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  segments:
133
133
  - 0
134
- hash: 2703034883636259662
134
+ hash: -3436815904282187362
135
135
  required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  none: false
137
137
  requirements: