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(login, api_key) ⇒ Client

Requires a login and api key. Get yours from your account page at bit.ly/a/account



11
12
13
# File 'lib/bitly/v3/client.rb', line 11

def initialize(, api_key)
  @default_query_opts = { :login => , :apiKey => api_key }
end

Instance Method Details

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

Checks whether a domain is a bitly.Pro domain



23
24
25
26
# File 'lib/bitly/v3/client.rb', line 23

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



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

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

#expand(input) ⇒ Object

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

Returns the results in the order they were entered



46
47
48
# File 'lib/bitly/v3/client.rb', line 46

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

#info(input) ⇒ Object

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



58
59
60
# File 'lib/bitly/v3/client.rb', line 58

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bitly/v3/client.rb', line 75

def lookup(input)
  input = [input] if input.is_a?(String)
  query = input.inject([]) { |query, i| query << "url=#{CGI.escape(i)}" }
  query = "/lookup?" + query.join('&')
  response = get(query)
  results = response['data']['lookup'].inject([]) do |results, url|
    url['long_url'] = url['url']
    url['url'] = nil
    if url['error'].nil?
      # builds the results array in the same order as the input
      results[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
      results[input.index(url['long_url'])] = Bitly::V3::MissingUrl.new(url)
      input[input.index(url['long_url'])] = nil
    end
    results
  end
  return results.length > 1 ? results : results[0]
end

#referrers(input) ⇒ Object

Provides a list of referring sites for a specified bit.ly short link, and the number of clicks per referrer.



63
64
65
66
67
68
69
70
# File 'lib/bitly/v3/client.rb', line 63

def referrers(input)
  if is_a_short_url?(input)
    query = { :shortUrl => CGI.escape(input) }
  else
    query = { :hash => CGI.escape(input) }
  end
  get("/referrers", :query => query)["data"]
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)



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

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

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

Validates a login and api key



16
17
18
19
# File 'lib/bitly/v3/client.rb', line 16

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