Class: Bitly::V3::Client

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

Overview

The client is the main part of this gem. You need to initialize the client with your username and API key and then you will be able to use the client to perform all the rest of the actions available through the API.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Requires a generic OAuth2 access token or -deprecated- login and api key. dev.bitly.com/authentication.html#apikey Generic OAuth2 access token: bitly.com/a/oauth_apps ApiKey: Get yours from your account page at bitly.com/a/your_api_key Visit your account at bit.ly/a/account


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bitly/v3/client.rb', line 15

def initialize(*args)
  args.compact!
  self.timeout = args.last.is_a?(0.class) ? args.pop : nil
  if args.count == 1
    # Set generic OAuth2 access token
    @default_query_opts = { :access_token => args.first }
  else
    # Deprecated ApiKey authentication
    @default_query_opts = {
      :login => args[0],
      :apiKey => args[1]
    }
  end
end

Instance Method Details

#bitly_pro_domain(domain) ⇒ Object Also known as: pro?

Checks whether a domain is a bitly.Pro domain


38
39
40
41
# File 'lib/bitly/v3/client.rb', line 38

def bitly_pro_domain(domain)
  response = get('/bitly_pro_domain', :query => { :domain => domain })
  return response['data']['bitly_pro_domain']
end

#clicks(input) ⇒ Object

Expands either a hash, short url or array of either and gets click data too.

Returns the results in the order they were entered


68
69
70
# File 'lib/bitly/v3/client.rb', line 68

def clicks(input)
  get_method(:clicks, input)
end

#clicks_by_day(input, opts = {}) ⇒ Object

Takes a short url, hash or array of either and gets the clicks by day


122
123
124
125
# File 'lib/bitly/v3/client.rb', line 122

def clicks_by_day(input, opts={})
  opts.reject! { |k, v| k.to_s != 'days' }
  get_method(:clicks_by_day, input, opts)
end

#clicks_by_minute(input) ⇒ Object

Takes a short url, hash or array of either and gets the clicks by minute of each of the last hour


117
118
119
# File 'lib/bitly/v3/client.rb', line 117

def clicks_by_minute(input)
  get_method(:clicks_by_minute, input)
end

#countries(input) ⇒ Object

Expands either a short link or hash and gets the country data for that link

This method does not take an array as an input


112
113
114
# File 'lib/bitly/v3/client.rb', line 112

def countries(input)
  get_single_method('countries', input)
end

#expand(input) ⇒ Object

Expands either a hash, short url or array of either.

Returns the results in the order they were entered


61
62
63
# File 'lib/bitly/v3/client.rb', line 61

def expand(input)
  get_method(:expand, input)
end

#info(input) ⇒ Object

Like expand, but gets the title of the page and who created it


73
74
75
# File 'lib/bitly/v3/client.rb', line 73

def info(input)
  get_method(:info, input)
end

#lookup(input) ⇒ Object

Looks up the short url and global hash of a url or array of urls

Returns the results in the order they were entered


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bitly/v3/client.rb', line 80

def lookup(input)
  input = arrayize(input)
  query = input.inject([]) { |q, i| q << "url=#{CGI.escape(i)}" }
  query = "/lookup?" + query.join('&')
  response = get(query)
  results = response['data']['lookup'].inject([]) do |rs, url|
    url['long_url'] = url['url']
    url['url'] = nil
    if url['error'].nil?
      # builds the results array in the same order as the input
      rs[input.index(url['long_url'])] = Bitly::V3::Url.new(self, url)
      # remove the key from the original array, in case the same hash/url was entered twice
      input[input.index(url['long_url'])] = nil
    else
      rs[input.index(url['long_url'])] = Bitly::V3::MissingUrl.new(url)
      input[input.index(url['long_url'])] = nil
    end
    rs
  end
  return results.length > 1 ? results : results[0]
end

#referrers(input) ⇒ Object

Expands either a short link or hash and gets the referrer data for that link

This method does not take an array as an input


105
106
107
# File 'lib/bitly/v3/client.rb', line 105

def referrers(input)
  get_single_method('referrers', input)
end

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

Shortens a long url

Options can be:

domain

choose bit.ly or j.mp (bit.ly is default)

x_login and x_apiKey

add this link to another user's history (both required)


52
53
54
55
56
# File 'lib/bitly/v3/client.rb', line 52

def shorten(long_url, opts={})
  query = { :longUrl => long_url }.merge(opts)
  response = get('/shorten', :query => query)
  return Bitly::V3::Url.new(self, response['data'])
end

#timeout=(timeout = nil) ⇒ Object


127
128
129
# File 'lib/bitly/v3/client.rb', line 127

def timeout=(timeout=nil)
  self.class.default_timeout(timeout) if timeout
end

#validate(x_login, x_api_key) ⇒ Object Also known as: valid?

Validates a login and api key


31
32
33
34
# File 'lib/bitly/v3/client.rb', line 31

def validate(, x_api_key)
  response = get('/validate', :query => { :x_login => , :x_apiKey => x_api_key })
  return response['data']['valid'] == 1
end