Skip to content

Commit

Permalink
Adds link history to user
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Jan 8, 2012
1 parent 62eb7a0 commit ab55abd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
35 changes: 23 additions & 12 deletions lib/bitly/v3/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module V3
# Url objects should only be created by the client object as it collects the correct information
# from the API.
class Url
attr_reader :short_url, :long_url, :user_hash, :global_hash
attr_reader :short_url, :long_url, :user_hash, :global_hash, :aggregate_link

# Initialize with a bitly client and optional hash to fill in the details for the url.
def initialize(client, opts={})
@client = client
Expand All @@ -18,6 +18,8 @@ def initialize(client, opts={})
@global_clicks = opts['global_clicks']
@title = opts['title']
@created_by = opts['created_by']
@created_at = Time.at opts['created_at'] if opts['created_at']
@aggregate_link = opts['aggregate_link']
@referrers = opts['referrers'].inject([]) do |results, referrer|
results << Bitly::V3::Referrer.new(referrer)
end if opts['referrers']
Expand All @@ -34,44 +36,44 @@ def initialize(client, opts={})
end
@short_url = "http://bit.ly/#{@user_hash}" unless @short_url
end

# Returns true if the user hash was created first for this call
def new_hash?
@new_hash
end

# If the url already has click statistics, returns the user clicks.
# IF there are no click statistics or <tt>:force => true</tt> is passed,
# updates the stats and returns the user clicks
def user_clicks(opts={})
update_clicks_data if @global_clicks.nil? || opts[:force]
@user_clicks
end

# If the url already has click statistics, returns the global clicks.
# IF there are no click statistics or <tt>:force => true</tt> is passed,
# updates the stats and returns the global clicks
def global_clicks(opts={})
update_clicks_data if @global_clicks.nil? || opts[:force]
@global_clicks
end

# If the url already has the title, return it.
# IF there is no title or <tt>:force => true</tt> is passed,
# updates the info and returns the title
def title(opts={})
update_info if @title.nil? || opts[:force]
@title
end

# If the url already has the creator, return it.
# IF there is no creator or <tt>:force => true</tt> is passed,
# updates the info and returns the creator
def created_by(opts={})
update_info if @created_by.nil? || opts[:force]
@created_by
end

# If the url already has referrer data, return it.
# IF there is no referrer or <tt>:force => true</tt> is passed,
# updates the referrers and returns them
Expand All @@ -88,6 +90,14 @@ def countries(opts={})
@countries
end

# If the url already has created at data, return it.
# If there is no created at data or <tt>:force => true</tt> is passed,
# updates the info and returns it
def created_at(opts={})
update_info if @created_at.nil? || opts[:force]
@created_at
end

def clicks_by_minute(opts={})
if @clicks_by_minute.nil? || opts[:force]
full_url = @client.clicks_by_minute(@user_hash || @short_url)
Expand All @@ -103,21 +113,22 @@ def clicks_by_day(opts={})
end
@clicks_by_day
end

private

def update_clicks_data
full_url = @client.clicks(@user_hash || @short_url)
@global_clicks = full_url.global_clicks
@user_clicks = full_url.user_clicks
end

def update_info
full_url = @client.info(@user_hash || @short_url)
@created_by = full_url.created_by
@title = full_url.title
@created_at = full_url.created_at
end

def update_referrers
full_url = @client.referrers(@user_hash || @short_url)
@referrers = full_url.referrers
Expand Down
61 changes: 42 additions & 19 deletions lib/bitly/v3/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ module V3
#
# u=Bitly::V3::User.new(o.access_token)
class User
include HTTParty
base_uri 'https://api-ssl.bit.ly/v3/'
attr_accessor :login, :api_key

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

# 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.
# day) for a given user’s bit.ly links, and the number of clicks per referrer.
#
# http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
def referrers(opts={})
Expand All @@ -34,9 +36,9 @@ def referrers(opts={})
end
@referrers
end

# 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.
# on a given user’s bit.ly links are originating, and the number of clicks per country.
#
# http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
def countries(opts={})
Expand All @@ -45,46 +47,66 @@ def countries(opts={})
end
@countries
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.
# based on click traffic in the past hour, and the number of clicks per link.
#
# http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
def realtime_links(opts={})
if @realtime_links.nil? || opts.delete(:force)
result = Crack::JSON.parse(@access_token.get("/v3/user/realtime_links", opts))
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(results['status_txt'], results['status_code'])
raise BitlyError.new(result['status_txt'], result['status_code'])
end
end
@realtime_links
end

# OAuth 2 endpoint that provides the total clicks per day on a user’s bit.ly links.
#
# http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
def clicks(opts={})
get_clicks(opts)
@clicks
end

# Displays the total clicks returned from the clicks method.
def total_clicks(opts={})
get_clicks(opts)
@total_clicks
end

# Returns a Bitly Client using the credentials of the user.
def client
@client ||= Bitly::V3::Client.new(login, api_key)
end


# OAuth 2 endpoint that OAuth 2 endpoint that provides a given user’s link
# shortening history, in reverse chronological order (most recent to least
# recent).
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

private

def get_method(method, klass, opts)
result = Crack::JSON.parse(@access_token.get("/v3/user/#{method.to_s}", opts))
opts.merge!(:access_token => @access_token.token)
result = self.class.get("/user/#{method.to_s}", :query => opts)
if result['status_code'] == 200
results = result['data'][method.to_s].map do |rs|
rs.inject([]) do |results, obj|
Expand All @@ -93,18 +115,19 @@ def get_method(method, klass, opts)
end
return results
else
raise BitlyError.new(results['status_txt'], results['status_code'])
raise BitlyError.new(result['status_txt'], result['status_code'])
end
end

def get_clicks(opts={})
if @clicks.nil? || opts.delete(:force)
result = Crack::JSON.parse(@access_token.get("/v3/user/clicks", opts))
opts.merge!(:access_token => @access_token.token)
result = self.class.get("/user/clicks", :query => opts)
if result['status_code'] == 200
@clicks = result['data']['clicks'].map { |rs| Bitly::V3::Day.new(rs) }
@total_clicks = result['data']['total_clicks']
else
raise BitlyError.new(results['status_txt'], results['status_code'])
raise BitlyError.new(result['status_txt'], result['status_code'])
end
end
end
Expand Down

0 comments on commit ab55abd

Please sign in to comment.