Class: BitlyOAuth::Client

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

Constant Summary collapse

BASE_URL =
'https://api-ssl.bit.ly/v3/'

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/bitly_oauth/client.rb', line 8

def initialize(client_id, client_secret)
  @client_id     = client_id
  @client_secret = client_secret
end

Instance Method Details

#authorize_url(redirect_url) ⇒ Object



13
14
15
# File 'lib/bitly_oauth/client.rb', line 13

def authorize_url(redirect_url)
  client.auth_code.authorize_url(:redirect_uri => redirect_url)
end

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



36
37
38
# File 'lib/bitly_oauth/client.rb', line 36

def bitly_pro_domain(domain)
  get(:bitly_pro_domain, :domain => domain)['bitly_pro_domain']
end

#clicks(input) ⇒ Object



56
57
58
# File 'lib/bitly_oauth/client.rb', line 56

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

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



68
69
70
71
# File 'lib/bitly_oauth/client.rb', line 68

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

#clicks_by_minute(input) ⇒ Object



64
65
66
# File 'lib/bitly_oauth/client.rb', line 64

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

#countries(input) ⇒ Object



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

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

#expand(input) ⇒ Object



52
53
54
# File 'lib/bitly_oauth/client.rb', line 52

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

#get_access_token_from_code(code, redirect_url) ⇒ Object



17
18
19
20
# File 'lib/bitly_oauth/client.rb', line 17

def get_access_token_from_code(code, redirect_url)
  access_token = client.auth_code.get_token(code, :redirect_uri => redirect_url, :parse => :query)
  BitlyOAuth::AccessToken.new(access_token)
end

#get_access_token_from_token(token, params = {}) ⇒ Object



26
27
28
29
30
# File 'lib/bitly_oauth/client.rb', line 26

def get_access_token_from_token(token, params={})
  params.stringify_keys!
  access_token = ::OAuth2::AccessToken.new(client, token, params)
  BitlyOAuth::AccessToken.new(access_token)
end

#info(input) ⇒ Object



60
61
62
# File 'lib/bitly_oauth/client.rb', line 60

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

#lookup(input) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bitly_oauth/client.rb', line 88

def lookup(input)
  input = input.to_a
  response = get(:lookup, :url => input)
  results = response['lookup'].inject([]) do |results, url|
    index = input.index(url['long_url'] = url.delete('url'))
    if url['error'].nil?
      results[index] = BitlyOAuth::Url.new(self, url)
    else
      results[index] = BitlyOAuth::MissingUrl.new(url)
    end
    input[index] = nil
    results
  end
  results.length > 1 ? results : results[0]
end

#referrers(input) ⇒ Object



77
78
79
# File 'lib/bitly_oauth/client.rb', line 77

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

#referring_domains(link, options = {}) ⇒ Object



81
82
83
84
85
86
# File 'lib/bitly_oauth/client.rb', line 81

def referring_domains(link, options={})
  response = get('link/referring_domains', :link => link)
  response['referring_domains'].map do |referring_domain|
    BitlyOAuth::ReferringDomain.new(referring_domain)
  end
end

#set_access_token_from_code(*args) ⇒ Object



22
23
24
# File 'lib/bitly_oauth/client.rb', line 22

def set_access_token_from_code(*args)
  @access_token ||= get_access_token_from_code(*args)
end

#set_access_token_from_token(*args) ⇒ Object



32
33
34
# File 'lib/bitly_oauth/client.rb', line 32

def set_access_token_from_token(*args)
  @access_token ||= get_access_token_from_token(*args)
end

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



47
48
49
50
# File 'lib/bitly_oauth/client.rb', line 47

def shorten(long_url, options={})
  response = get(:shorten, { :longUrl => long_url }.merge(options))
  BitlyOAuth::Url.new(self, response)
end

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

Validates a login and api key



42
43
44
# File 'lib/bitly_oauth/client.rb', line 42

def validate(, x_api_key)
  get(:validate, :x_login => , :x_apiKey => x_api_key)['valid'] == 1
end