/lib/bitly/v3/client.rb

https://github.com/macdiva/bitly · Ruby · 140 lines · 95 code · 14 blank · 31 comment · 10 complexity · e1450431eca93794b3b2bb0f5ce69973 MD5 · raw file

  1. module Bitly
  2. module V3
  3. # The client is the main part of this gem. You need to initialize the client with your
  4. # username and API key and then you will be able to use the client to perform
  5. # all the rest of the actions available through the API.
  6. class Client
  7. include HTTParty
  8. base_uri 'http://api.bit.ly/v3/'
  9. # Requires a login and api key. Get yours from your account page at http://bit.ly/a/account
  10. def initialize(login, api_key)
  11. @default_query_opts = { :login => login, :apiKey => api_key }
  12. end
  13. # Validates a login and api key
  14. def validate(x_login, x_api_key)
  15. response = get('/validate', :query => { :x_login => x_login, :x_apiKey => x_api_key })
  16. return response['data']['valid'] == 1
  17. end
  18. alias :valid? :validate
  19. # Checks whether a domain is a bitly.Pro domain
  20. def bitly_pro_domain(domain)
  21. response = get('/bitly_pro_domain', :query => { :domain => domain })
  22. return response['data']['bitly_pro_domain']
  23. end
  24. alias :pro? :bitly_pro_domain
  25. # Shortens a long url
  26. #
  27. # Options can be:
  28. #
  29. # [domain] choose bit.ly or j.mp (bit.ly is default)
  30. #
  31. # [x_login and x_apiKey] add this link to another user's history (both required)
  32. #
  33. def shorten(long_url, opts={})
  34. query = { :longUrl => long_url }.merge(opts)
  35. response = get('/shorten', :query => query)
  36. return Bitly::V3::Url.new(self, response['data'])
  37. end
  38. # Expands either a hash, short url or array of either.
  39. #
  40. # Returns the results in the order they were entered
  41. def expand(input)
  42. get_method(:expand, input)
  43. end
  44. # Expands either a hash, short url or array of either and gets click data too.
  45. #
  46. # Returns the results in the order they were entered
  47. def clicks(input)
  48. get_method(:clicks, input)
  49. end
  50. # Like expand, but gets the title of the page and who created it
  51. def info(input)
  52. get_method(:info, input)
  53. end
  54. # Looks up the short url and global hash of a url or array of urls
  55. #
  56. # Returns the results in the order they were entered
  57. def lookup(input)
  58. input = [input] if input.is_a?(String)
  59. query = input.inject([]) { |query, i| query << "url=#{CGI.escape(i)}" }
  60. query = "/lookup?" + query.join('&')
  61. response = get(query)
  62. results = response['data']['lookup'].inject([]) do |results, url|
  63. url['long_url'] = url['url']
  64. url['url'] = nil
  65. if url['error'].nil?
  66. # builds the results array in the same order as the input
  67. results[input.index(url['long_url'])] = Bitly::V3::Url.new(self, url)
  68. # remove the key from the original array, in case the same hash/url was entered twice
  69. input[input.index(url['long_url'])] = nil
  70. else
  71. results[input.index(url['long_url'])] = Bitly::V3::MissingUrl.new(url)
  72. input[input.index(url['long_url'])] = nil
  73. end
  74. results
  75. end
  76. return results.length > 1 ? results : results[0]
  77. end
  78. # Provides a list of countries from which clicks on a specified bit.ly short link have originated, and the number of clicks per country.
  79. #
  80. # Returns the results in the order they were entered
  81. def countries(input)
  82. get_method(:countries, input)
  83. end
  84. private
  85. def get(method, opts={})
  86. opts[:query] ||= {}
  87. opts[:query].merge!(@default_query_opts)
  88. response = self.class.get(method, opts)
  89. if response['status_code'] == 200
  90. return response
  91. else
  92. raise BitlyError.new(response['status_txt'], response['status_code'])
  93. end
  94. end
  95. def is_a_short_url?(input)
  96. input.match(/^http:\/\//)
  97. end
  98. def get_method(method, input)
  99. input = [input] if input.is_a? String
  100. query = input.inject([]) do |query,i|
  101. if is_a_short_url?(i)
  102. query << "shortUrl=#{CGI.escape(i)}"
  103. else
  104. query << "hash=#{CGI.escape(i)}"
  105. end
  106. end
  107. query = "/#{method}?" + query.join('&')
  108. response = get(query)
  109. results = response['data'][method.to_s].inject([]) do |results, url|
  110. result_index = input.index(url['short_url'] || url['hash']) || input.index(url['global_hash'])
  111. if url['error'].nil?
  112. # builds the results array in the same order as the input
  113. results[result_index] = Bitly::V3::Url.new(self, url)
  114. # remove the key from the original array, in case the same hash/url was entered twice
  115. input[result_index] = nil
  116. else
  117. results[result_index] = Bitly::V3::MissingUrl.new(url)
  118. input[result_index] = nil
  119. end
  120. results
  121. end
  122. return results.length > 1 ? results : results[0]
  123. end
  124. end
  125. end
  126. end