Class: Bitly::Client

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

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) ⇒ Client

Returns a new instance of Client.



41
42
43
44
45
# File 'lib/bitly/client.rb', line 41

def initialize(,api_key)
  warn "[DEPRECATION] The bit.ly version 2 API has been superseded by version 3 and will be removed. See the README for details"
  @login = 
  @api_key = api_key
end

Instance Method Details

#expand(input) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bitly/client.rb', line 66

def expand(input)
  if input.is_a? String
    if input.include?('bit.ly/') || input.include?('j.mp/')
      hash = create_hash_from_url(input)
      request = create_url "expand", :hash => hash
      result = get_result(request)
      result = { :short_url => input, :hash => hash }.merge result[hash]
    else
      request = create_url "expand", :hash => input
      result = get_result(request)
      result = { :hash => input, :short_url => "http://bit.ly/#{input}" }.merge result[input]
    end
    Bitly::Url.new(@login,@api_key,result)
  elsif input.is_a? Array
    request = create_url "expand", :hash => input.join(',')
    result = get_result(request)
    input.map do |hsh|
      new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
      hsh = Bitly::Url.new(@login,@api_key,new_url)
    end
  else
    raise ArgumentError('Expand requires either a short url, a hash or an array of hashes')
  end
end

#info(input) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bitly/client.rb', line 91

def info(input)
  if input.is_a? String
    if input.include? "bit.ly/"
      hash = create_hash_from_url(input)
      request = create_url 'info', :hash => hash
      result = get_result(request)
      result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result[hash]
    else
      request = create_url 'info', :hash => input
      result = get_result(request)
      result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result[input]
    end
    Bitly::Url.new(@login,@api_key,result)
  elsif input.is_a? Array
    request = create_url "info", :hash => input.join(',')
    result = get_result(request)
    input.map do |hsh|
      new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
      hsh = Bitly::Url.new(@login,@api_key,:info => new_url)
    end
  else
    raise ArgumentError.new('Info requires either a short url, a hash or an array of hashes')
  end
end

#shorten(input, opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bitly/client.rb', line 47

def shorten(input, opts={})
  if input.is_a? String
    request = create_url("shorten", :longUrl => input, :history => (opts[:history] ? 1 : nil))
    result = get_result(request)
    result = {:long_url => input}.merge result[input]
    Bitly::Url.new(@login,@api_key,result)
  elsif input.is_a? Array
    request = create_url("shorten", :history => (opts[:history] ? 1 : nil))
    request.query << "&" + input.map { |long_url| "longUrl=#{CGI.escape(long_url)}" }.join("&") unless input.nil?
    result = get_result(request)
    input.map do |long_url|
      new_url = {:long_url => long_url}.merge result[long_url]
      long_url = Bitly::Url.new(@login,@api_key,new_url)
    end
  else
    raise ArgumentError.new("Shorten requires either a url or an array of urls")
  end
end

#stats(input) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bitly/client.rb', line 116

def stats(input)
  if input.is_a? String
    if input.include? "bit.ly/"
      hash = create_hash_from_url(input)
      request = create_url 'stats', :hash => hash
      result = get_result(request)
      result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result
    else
      request = create_url 'stats', :hash => input
      result = get_result(request)
      result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result
    end
    Bitly::Url.new(@login,@api_key,:stats => result)
  else
    raise ArgumentError.new("Stats requires either a short url or a hash")
  end
end