class Bitly4R::Client

Client

A client object for accessing the bit.ly API.

See the API documentation:

Attributes

token[R]

The Access Token credential provided at construction.

Public Class Methods

new(ops={}) click to toggle source

Constructs a new client.

Any tuples provided in the optional Hash are injected into instance variables.

You must provide an Access Token (token).

   # File lib/bitly4r/client.rb
26 def initialize(ops={})
27         #    for the readers
28         #    not necessary, but polite
29         ops.each do |k, v|
30                 instance_variable_set "@#{k}".to_sym, v
31         end
32 
33         raise Error.new("you must provide an Access Token") unless self.token
34 end

Public Instance Methods

expand(url) click to toggle source

Invokes the API’s expand method. It reverses a shorten; the original full URL is re-hydrated.

dev.bitly.com/api-reference/#expandBitlink

For url, you can provide a previously-shortened URL from the bit.ly service.

The URL must provide both a domain (hostname) and a Bitlink ID (path). A minimum-viable URL would be something along the lines of ‘bit.ly/3ADCPDo’.

A Response is returned. Response.to_s will return the long_url value.

   # File lib/bitly4r/client.rb
72     def expand(url)
73             return nil unless url
74 uri = URI.parse(url)
75 bitlink = "#{uri.host}#{uri.path}"
76 
77             return execute_post('expand', :long_url) do |params|
78                     params[:bitlink_id] = bitlink
79             end
80     end
info(url) click to toggle source

Invokes the API’s info method. Information about the shortened URL is returned by the service.

dev.bitly.com/api-reference/#getBitlink

For url, you can provide a previously-shortened URL from the bit.ly service.

The URL must provide both a domain (hostname) and a Bitlink ID (path). A minimum-viable URL would be something along the lines of ‘bit.ly/3ADCPDo’.

A Response is returned. Response.to_s will return the long_url value.

There is plenty of other data in the response besides the original full URL. Feel free to access the Response.body and use Response.method_missing to pull out specific element values.

    # File lib/bitly4r/client.rb
 97     def info(url)
 98             return nil unless url
 99 uri = URI.parse(url)
100 bitlink = "#{uri.host}#{uri.path}"
101 
102 command = "bitlinks/#{bitlink}"
103             return execute_get(command, :long_url)
104     end
shorten(long_url, ops={}) click to toggle source

Invokes the API’s shorten method. That’s pretty much what makes bit.ly a valuable service.

dev.bitly.com/api-reference/#createBitlink

A Response is returned. Response.to_s will return the link value.

You can take the shortened URL and re-expand it.

ops allows for additional API parameters <ul>

<li><tt>group_guid</tt></li>
<li><tt>domain</tt></li>

</ul>

   # File lib/bitly4r/client.rb
52     def shorten(long_url, ops={})
53             return nil unless long_url
54 return execute_post('shorten', :link) do |params|
55     params[:long_url] = long_url
56     params.merge!(ops)
57 end
58     end