Class: GoogleShortLinks::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/google_short_links/client.rb

Overview

Client for Google Short Links.

Examples

client = GoogleShortLinks::Client.new :server => 'short.example.com', :secret => 'abcdef1234567890', :email => '[email protected]'
link = client.get_or_create_hash 'http://example.com/parent/child/long_document_name.html', :is_public => true
link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>98, "shortcut"=>"abc12", "usage_count"=>0, "owner"=>"[email protected]", "is_public"=>true, "is_hash"=>true}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initializes a new Client.

Parameters

options

Optional Hash containing configuration values.

Options

:server

The domain name where Google Short Links is hosted.

:secret

Your HMAC-SHA1 secret.

:email

The email for the account under which the links will be created.



44
45
46
47
48
# File 'lib/google_short_links/client.rb', line 44

def initialize options={}
  self.server = options[:server]
  self.secret = options[:secret]
  self.email = options[:email]
end

Instance Attribute Details

#emailObject

The email for the account under which the links will be created.



26
27
28
# File 'lib/google_short_links/client.rb', line 26

def email
  @email
end

#secretObject

Your HMAC-SHA1 secret.



23
24
25
# File 'lib/google_short_links/client.rb', line 23

def secret
  @secret
end

#serverObject

The domain name where Google Short Links is hosted.



20
21
22
# File 'lib/google_short_links/client.rb', line 20

def server
  @server
end

Instance Method Details

#get_details(shortcut, params = {}) ⇒ Object

Gets details about a link from its shortcut.

Parameters

shortcut

Required String containing the URL to be shortened.

params

Optional Hash that will add or override GET parameters in the URL.

Examples

link = client.get_details 'example'
link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>100, "shortcut"=>"example", "usage_count"=>0, "owner"=>"[email protected]", "is_public"=>true, "is_hash"=>false}


170
171
172
# File 'lib/google_short_links/client.rb', line 170

def get_details shortcut, params={}
  self.class.get(get_details_url(shortcut, params)).parsed_response
end

#get_details_url(shortcut, params = {}) ⇒ Object

Creates a URL that is used to get details about a link from its shortcut.

Parameters

shortcut

Required String containing the URL to be shortened.

params

Optional Hash that will add or override GET parameters in the URL.

Examples

link_url = client.get_details_url 'example'
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"


110
111
112
113
114
115
116
# File 'lib/google_short_links/client.rb', line 110

def get_details_url shortcut, params={}
  request_path = request_path(:get_details)

  query = params_to_query(base_params.merge(:shortcut => shortcut).merge(params))

  digest_url(request_path, query)
end

#get_or_create_hash(url, params = {}) ⇒ Object

Shortens a URL.

Parameters

url

Required String containing the URL to be shortened.

params

Optional Hash that will add or override GET parameters in the URL.

Parameters

:is_public

Boolean for whether or not the short link can be accessed without logging in to Google Apps.

Examples

link = client.get_or_create_hash 'http://example.com/parent/child/long_document_name.html', :is_public => true
link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>98, "shortcut"=>"abc12", "usage_count"=>0, "owner"=>"[email protected]", "is_public"=>true, "is_hash"=>true}


134
135
136
# File 'lib/google_short_links/client.rb', line 134

def get_or_create_hash url, params={}
  self.class.get(get_or_create_hash_url(url, params)).parsed_response
end

#get_or_create_hash_url(url, params = {}) ⇒ Object

Creates a URL that is used to create a hash.

Parameters

url

Required String containing the URL to be shortened.

params

Optional Hash that will add or override GET parameters in the URL.

Parameters

:is_public

Boolean for whether or not the short link can be accessed without logging in to Google Apps.

Examples

link_url = client.get_or_create_hash_url 'http://example.com/parent/child/long_document_name.html', :is_public => true
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"


66
67
68
69
70
71
72
# File 'lib/google_short_links/client.rb', line 66

def get_or_create_hash_url url, params={}
  request_path = request_path(:get_or_create_hash)

  query = params_to_query(base_params.merge(:url => url).merge(params))

  digest_url(request_path, query)
end

Creates a shortlink with a customized shortcut.

Parameters

url

Required String containing the URL to be shortened.

shortcut

Required String containing the custom shortcut.

params

Optional Hash that will add or override GET parameters in the URL.

Parameters

:is_public

Boolean for whether or not the short link can be accessed without logging in to Google Apps.

Examples

link = client.get_or_create_shortlink 'http://example.com/parent/child/long_document_name.html', 'example', :is_public => true
link # => {"status"=>"ok", "url"=>"http://example.com/parent/child/long_document_name.html", "estimated_api_calls_remaining"=>99, "shortcut"=>"example", "usage_count"=>0, "owner"=>"[email protected]", "is_public"=>true, "is_hash"=>false}


155
156
157
# File 'lib/google_short_links/client.rb', line 155

def get_or_create_shortlink url, shortcut, params={}
  self.class.get(get_or_create_shortlink_url(url, shortcut, params)).parsed_response
end

Creates a URL that is used to create a shortlink with a customized shortcut.

Parameters

url

Required String containing the URL to be shortened.

shortcut

Required String containing the custom shortcut.

params

Optional Hash that will add or override GET parameters in the URL.

Parameters

:is_public

Boolean for whether or not the short link can be accessed without logging in to Google Apps.

Examples

link_url = client.get_or_create_shortlink_url 'http://example.com/parent/child/long_document_name.html', 'example', :is_public => true
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"


91
92
93
94
95
96
97
# File 'lib/google_short_links/client.rb', line 91

def get_or_create_shortlink_url url, shortcut, params={}
  request_path = request_path(:get_or_create_shortlink)

  query = params_to_query(base_params.merge(:url => url, :shortcut => shortcut).merge(params))

  digest_url(request_path, query)
end