Class: Bitly::Url

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/bitly/url.rb

Constant Summary collapse

VARIABLES =
['long_url', 'short_url', 'hash', 'user_hash']

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#attr_define, #create_hash_from_url, #create_url, #get_result, #instance_variablise, #underscore

Constructor Details

#initialize(login, api_key, obj = nil) ⇒ Url

Returns a new instance of Url.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bitly/url.rb', line 9

def initialize(,api_key,obj=nil)
  unless obj.nil?
    raise BitlyError.new(obj['errorMessage'],obj['errorCode']) if obj['statusCode'] == "ERROR"
    instance_variablise(obj, VARIABLES)
    @info = obj.fetch(:info, nil)
    @stats = obj.fetch(:stats, nil)
  end
  @login = 
  @api_key = api_key
  raise ArgumentError.new("Please provide a login and api_key") if @login.nil? || @api_key.nil?
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



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

def hash
  @hash
end

#long_urlObject

Returns the value of attribute long_url.



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

def long_url
  @long_url
end

#short_urlObject

Returns the value of attribute short_url.



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

def short_url
  @short_url
end

#user_hashObject

Returns the value of attribute user_hash.



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

def user_hash
  @user_hash
end

Instance Method Details

#bitly_urlObject



94
95
96
# File 'lib/bitly/url.rb', line 94

def bitly_url
  @short_url.nil? ? nil : @short_url.gsub(/j\.mp/,'bit.ly')
end

#expandObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bitly/url.rb', line 37

def expand
  return @long_url if defined? @long_url
  unless !(short_url || hash)
    unless defined? @hash
      @hash = create_hash_from_url(@short_url)
    end
    request = create_url("expand", :hash => @hash)
    result = get_result(request)[@hash]
    if result['statusCode'] == "ERROR"
      raise BitlyError.new(result['errorMessage'],result['errorCode'])
    else
      instance_variablise(result,VARIABLES)
      return @long_url
    end
  else
    raise ArgumentError.new("You need a short_url or a hash in order to expand it")
  end
end

#infoObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitly/url.rb', line 56

def info
  if @info.nil?
    if defined? @hash
      request = create_url "info", :hash => @hash
      result = get_result(request)[@hash]
      instance_variablise(result, VARIABLES)
      @info = result
    elsif defined? @short_url
      @hash = create_hash_from_url(@short_url)
      request = create_url "info", :hash => hash
      result = get_result(request)[hash]
      instance_variablise(result, VARIABLES)
      @info = result
    else
      raise ArgumentError.new("You need a hash or short_url in order to get info")
    end
    return @info
  else
    @info
  end
end

#jmp_urlObject



98
99
100
# File 'lib/bitly/url.rb', line 98

def jmp_url
  @short_url.nil? ? nil : @short_url.gsub(/bit\.ly/,'j.mp')
end

#shorten(opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bitly/url.rb', line 21

def shorten(opts = {})
  return @short_url if defined? @short_url
  if defined? @long_url
    request = create_url("shorten", :longUrl => @long_url, :history => (opts[:history] ? 1 : nil))
    result = get_result(request)[@long_url.gsub(/\/$/,'')]
    if result['statusCode'] == "ERROR"
      raise BitlyError.new(result['errorMessage'],result['errorCode'])
    else
      instance_variablise(result,VARIABLES)
      return @short_url
    end
  else
    raise ArgumentError.new("You need a long_url in order to shorten it")
  end
end

#statsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bitly/url.rb', line 78

def stats
  if @stats.nil?
    if defined? @hash
      request = create_url "stats", :hash => @hash
    elsif defined? @short_url
      @hash = create_hash_from_url(@short_url)
      request = create_url "stats", :hash => @hash
    else
      raise ArgumentError.new("You need a hash or short_url in order to get stats")
    end
    @stats = get_result(request)
  else
    @stats
  end
end