Class: Bitly::V3::User

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

Overview

A user requires an oauth access token. The flow is as follows:

o = Bitly::V3::OAuth.new(consumer_token, consumer_secret)
o.authorize_url(redirect_url)
#=> "https://bit.ly/oauth/authorize?client_id=#{consumer_token}&type=web_server&redirect_uri=http%3A%2F%2Ftest.local%2Fbitly%2Fauth"

Redirect your users to this url, when they authorize your application they will be redirected to the url you provided with a code parameter. Use that parameter, and the exact same redirect url as follows:

o.get_access_token_from_code(params[:code], redirect_url)
#=> #<OAuth2::AccessToken ...>

Then use that access token to create your user object.

u=Bitly::V3::User.new(o.access_token)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ User

Returns a new instance of User


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

def initialize(access_token)
  @access_token = access_token
  @login = access_token['login'] || access_token['username']
  @api_key = access_token['apiKey'] || access_token['api_key']
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key


21
22
23
# File 'lib/bitly/v3/user.rb', line 21

def api_key
  @api_key
end

#loginObject

Returns the value of attribute login


21
22
23
# File 'lib/bitly/v3/user.rb', line 21

def 
  @login
end

Instance Method Details

#clicks(opts = {}) ⇒ Object

OAuth 2 endpoint that provides the total clicks per day on a user's bit.ly links.

code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks


71
72
73
74
# File 'lib/bitly/v3/user.rb', line 71

def clicks(opts={})
  get_clicks(opts)
  @clicks
end

#clientObject

Returns a Bitly Client using the credentials of the user.


83
84
85
# File 'lib/bitly/v3/user.rb', line 83

def client
  @client ||= Bitly::V3::Client.new(, api_key)
end

#countries(opts = {}) ⇒ Object

OAuth 2 endpoint that provides a list of countries from which clicks on a given user's bit.ly links are originating, and the number of clicks per country.

code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries


44
45
46
47
48
49
# File 'lib/bitly/v3/user.rb', line 44

def countries(opts={})
  if @countries.nil? || opts.delete(:force)
    @countries = get_method(:countries, Bitly::V3::Country, opts)
  end
  @countries
end

OAuth 2 endpoint that provides a given user's link shortening history, in reverse chronological order (most recent to least recent).


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bitly/v3/user.rb', line 89

def link_history(opts={})
  opts.merge!(:access_token => @access_token.token)
  result = self.class.get("/user/link_history", :query => opts)
  if result['status_code'] == 200
    results = result['data']['link_history'].inject([]) do |rs, obj|
      obj['short_url'] = obj['link']
      obj['hash'] = obj['link'].split('/').last
      rs << Url.new(client, obj)
    end
    return results
  else
    raise BitlyError.new(result['status_txt'], result['status_code'])
  end
end

OAuth 2 endpoint that provides a given user's 100 most popular links based on click traffic in the past hour, and the number of clicks per link.

code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bitly/v3/user.rb', line 55

def realtime_links(opts={})
  if @realtime_links.nil? || opts.delete(:force)
    opts.merge!(:access_token => @access_token.token)
    result = self.class.get("/user/realtime_links", :query => opts)
    if result['status_code'] == 200
      @realtime_links = result['data']['realtime_links'].map { |rs| Bitly::V3::RealtimeLink.new(rs) }
    else
      raise BitlyError.new(result['status_txt'], result['status_code'])
    end
  end
  @realtime_links
end

#referrers(opts = {}) ⇒ Object

OAuth 2 endpoint that provides a list of top referrers (up to 500 per day) for a given user's bit.ly links, and the number of clicks per referrer.

code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers


33
34
35
36
37
38
# File 'lib/bitly/v3/user.rb', line 33

def referrers(opts={})
  if @referrers.nil? || opts.delete(:force)
    @referrers = get_method(:referrers, Bitly::V3::Referrer, opts)
  end
  @referrers
end

#total_clicks(opts = {}) ⇒ Object

Displays the total clicks returned from the clicks method.


77
78
79
80
# File 'lib/bitly/v3/user.rb', line 77

def total_clicks(opts={})
  get_clicks(opts)
  @total_clicks
end