Class: Bitly4R::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly4r/client.rb

Overview

  • It supports Access Token credentials.

See the API documentation:

Direct Known Subclasses

SimpleClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops = {}) ⇒ Client

Constructs a new client.

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

You must provide an Access Token (token).

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/bitly4r/client.rb', line 26

def initialize(ops={})
  #  for the readers
  #  not necessary, but polite
  ops.each do |k, v|
    instance_variable_set "@#{k}".to_sym, v
  end

  raise Error.new("you must provide an Access Token") unless self.token
end

Instance Attribute Details

#tokenObject (readonly)

The Access Token credential provided at construction.



19
20
21
# File 'lib/bitly4r/client.rb', line 19

def token
  @token
end

Instance Method Details

#expand(url) ⇒ Object

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



72
73
74
75
76
77
78
79
80
# File 'lib/bitly4r/client.rb', line 72

def expand(url)
  return nil unless url
          uri = URI.parse(url)
          bitlink = "#{uri.host}#{uri.path}"

  return execute_post('expand', :long_url) do |params|
    params[:bitlink_id] = bitlink
  end
end

#info(url) ⇒ Object

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.



97
98
99
100
101
102
103
104
# File 'lib/bitly4r/client.rb', line 97

def info(url)
  return nil unless url
          uri = URI.parse(url)
          bitlink = "#{uri.host}#{uri.path}"

          command = "bitlinks/#{bitlink}"
  return execute_get(command, :long_url)
end

#shorten(long_url, ops = {}) ⇒ Object

ops allows for additional API parameters <ul>

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

</ul>



52
53
54
55
56
57
58
# File 'lib/bitly4r/client.rb', line 52

def shorten(long_url, ops={})
  return nil unless long_url
          return execute_post('shorten', :link) do |params|
              params[:long_url] = long_url
              params.merge!(ops)
          end
end