Class: Bitlyr::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/bitlyr/url.rb

Overview

Url objects should only be created by the client object as it collects the correct information from the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Url

Initialize with a bitly client and optional hash to fill in the details for the url.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitlyr/url.rb', line 9

def initialize(client, options = {})
  @client        = client
  @title         = options['title'] || '' if options.key?('title')
  @new_hash      = (options['new_hash'] == 1)
  @long_url      = options['long_url']
  @user_hash     = options['hash'] || options['user_hash']
  @short_url     = options['url'] || options['short_url'] || "http://bit.ly/#{@user_hash}"
  @created_by    = options['created_by']
  @global_hash   = options['global_hash']
  @user_clicks   = options['user_clicks']
  @global_clicks = options['global_clicks']

  @referrers = options['referrers'].map{|referrer| Bitlyr::Referrer.new(referrer) } if options['referrers']
  @countries = options['countries'].map{|country| Bitlyr::Country.new(country) } if options['countries']

  if options['clicks'] && options['clicks'][0].is_a?(Hash)
    @clicks_by_day = options['clicks'].map{|day| Bitlyr::Day.new(day)}
  else
    @clicks_by_minute = options['clicks']
  end
end

Instance Attribute Details

#countries(options = {}) ⇒ Object (readonly)

If the url already has country data, return it. IF there is no country or :force => true is passed, updates the countries and returns them



79
80
81
# File 'lib/bitlyr/url.rb', line 79

def countries
  @countries
end

#global_hashObject (readonly)

Returns the value of attribute global_hash.



6
7
8
# File 'lib/bitlyr/url.rb', line 6

def global_hash
  @global_hash
end

#long_urlObject (readonly)

Returns the value of attribute long_url.



6
7
8
# File 'lib/bitlyr/url.rb', line 6

def long_url
  @long_url
end

#referrers(options = {}) ⇒ Object (readonly)

If the url already has referrer data, return it. IF there is no referrer or :force => true is passed, updates the referrers and returns them



71
72
73
# File 'lib/bitlyr/url.rb', line 71

def referrers
  @referrers
end

#short_urlObject (readonly)

Returns the value of attribute short_url.



6
7
8
# File 'lib/bitlyr/url.rb', line 6

def short_url
  @short_url
end

#user_hashObject (readonly)

Returns the value of attribute user_hash.



6
7
8
# File 'lib/bitlyr/url.rb', line 6

def user_hash
  @user_hash
end

Instance Method Details

#clicks_by_day(options = {}) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/bitlyr/url.rb', line 92

def clicks_by_day(options={})
  if @clicks_by_day.nil? || options[:force]
    full_url = @client.clicks_by_day(@user_hash || @short_url)
    @clicks_by_day = full_url.clicks_by_day
  end
  @clicks_by_day
end

#clicks_by_minute(options = {}) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/bitlyr/url.rb', line 84

def clicks_by_minute(options={})
  if @clicks_by_minute.nil? || options[:force]
    full_url = @client.clicks_by_minute(@user_hash || @short_url)
    @clicks_by_minute = full_url.clicks_by_minute
  end
  @clicks_by_minute
end

#created_by(options = {}) ⇒ Object

If the url already has the creator, return it. IF there is no creator or :force => true is passed, updates the info and returns the creator



63
64
65
66
# File 'lib/bitlyr/url.rb', line 63

def created_by(options={})
  update_info if @created_by.nil? || options[:force]
  @created_by
end

#global_clicks(options = {}) ⇒ Object

If the url already has click statistics, returns the global clicks. IF there are no click statistics or :force => true is passed, updates the stats and returns the global clicks



47
48
49
50
# File 'lib/bitlyr/url.rb', line 47

def global_clicks(options={})
  update_clicks_data if @global_clicks.nil? || options[:force]
  @global_clicks
end

#new_hash?Boolean

Returns true if the user hash was created first for this call

Returns:

  • (Boolean)


32
33
34
# File 'lib/bitlyr/url.rb', line 32

def new_hash?
  @new_hash
end

#title(options = {}) ⇒ Object

If the url already has the title, return it. IF there is no title or :force => true is passed, updates the info and returns the title



55
56
57
58
# File 'lib/bitlyr/url.rb', line 55

def title(options={})
  update_info if @title.nil? || options[:force]
  @title
end

#user_clicks(options = {}) ⇒ Object

If the url already has click statistics, returns the user clicks. IF there are no click statistics or :force => true is passed, updates the stats and returns the user clicks



39
40
41
42
# File 'lib/bitlyr/url.rb', line 39

def user_clicks(options={})
  update_clicks_data if @user_clicks.nil? || options[:force]
  @user_clicks
end