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)


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

def initialize(,api_key,obj=nil)
  unless obj.nil?
    raise BitlyError.new(result['errorMessage'],result['errorCode']) if obj['statusCode'] == "ERROR"
    instance_variablise(obj, VARIABLES)
    @info = obj[:info] if obj[:info]
    @stats = obj[:stats] if obj[:stats]
  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

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
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

#statsObject (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
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

#expandObject



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

def expand
  return @long_url if @long_url
  unless !(@short_url || @hash)
    unless @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

#shorten(opts = {}) ⇒ Object



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

def shorten(opts = {})
  return @short_url if @short_url
  unless @long_url.nil?
    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