Class: Bitly::V3::Client
- Inherits:
-
Object
- Object
- Bitly::V3::Client
- 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
-
#bitly_pro_domain(domain) ⇒ Object
(also: #pro?)
Checks whether a domain is a bitly.Pro domain.
-
#clicks(input) ⇒ Object
Expands either a hash, short url or array of either and gets click data too.
-
#clicks_by_day(input, opts = {}) ⇒ Object
Takes a short url, hash or array of either and gets the clicks by day.
-
#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.
-
#countries(input) ⇒ Object
Expands either a short link or hash and gets the country data for that link.
-
#expand(input) ⇒ Object
Expands either a hash, short url or array of either.
-
#info(input) ⇒ Object
Like expand, but gets the title of the page and who created it.
-
#initialize(login, api_key) ⇒ Client
constructor
Requires a login and api key.
-
#lookup(input) ⇒ Object
Looks up the short url and global hash of a url or array of urls.
-
#referrers(input) ⇒ Object
Expands either a short link or hash and gets the referrer data for that link.
-
#shorten(long_url, opts = {}) ⇒ Object
Shortens a long url.
-
#validate(x_login, x_api_key) ⇒ Object
(also: #valid?)
Validates a login and api key.
Constructor Details
#initialize(login, api_key) ⇒ Client
Requires a login and api key. Get yours from your account page at http://bit.ly/a/account
11 12 13 |
# File 'lib/bitly/v3/client.rb', line 11 def initialize(login, api_key) @default_query_opts = { :login => 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 |
#clicks_by_day(input, opts = {}) ⇒ Object
Takes a short url, hash or array of either and gets the clicks by day
107 108 109 110 |
# File 'lib/bitly/v3/client.rb', line 107 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
102 103 104 |
# File 'lib/bitly/v3/client.rb', line 102 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
97 98 99 |
# File 'lib/bitly/v3/client.rb', line 97 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
46 47 48 |
# File 'lib/bitly/v3/client.rb', line 46 def (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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bitly/v3/client.rb', line 65 def lookup(input) input = arrayize(input) 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
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
90 91 92 |
# File 'lib/bitly/v3/client.rb', line 90 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)
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_login, x_api_key) response = get('/validate', :query => { :x_login => x_login, :x_apiKey => x_api_key }) return response['data']['valid'] == 1 end |