Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bump to 0.5.4. Adds referrer, countries, clicks_by_minute, clicks_by_…

…day to V3
  • Loading branch information...
commit a42ca5c93ac801cdb149f0252625822a57747fcd 1 parent 099bb8a
@philnash authored
View
6 History.txt
@@ -1,3 +1,9 @@
+=== 0.5.4 / 2010-12-01
+
+* 4 minor enhancements
+
+ * Added referrers, countries, clicks_by_minute and clicks_by_day calls to the version 3 API
+
=== 0.5.3 / 2010-07-30
* 1 minor enhancement
View
15 Manifest
@@ -1,18 +1,21 @@
-bitly.gemspec
History.txt
+Manifest
+README.txt
+Rakefile
+bitly.gemspec
+lib/bitly.rb
lib/bitly/client.rb
lib/bitly/url.rb
lib/bitly/utils.rb
+lib/bitly/v3.rb
lib/bitly/v3/bitly.rb
lib/bitly/v3/client.rb
+lib/bitly/v3/country.rb
+lib/bitly/v3/day.rb
lib/bitly/v3/missing_url.rb
+lib/bitly/v3/referrer.rb
lib/bitly/v3/url.rb
-lib/bitly/v3.rb
lib/bitly/version.rb
-lib/bitly.rb
-Manifest
-Rakefile
-README.txt
test/bitly/test_client.rb
test/bitly/test_url.rb
test/bitly/test_utils.rb
View
12 bitly.gemspec
@@ -2,20 +2,20 @@
Gem::Specification.new do |s|
s.name = %q{bitly}
- s.version = "0.5.2"
+ s.version = "0.5.4"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Phil Nash"]
- s.date = %q{2010-07-18}
+ s.date = %q{2010-12-01}
s.description = %q{Use the bit.ly API to shorten or expand URLs}
s.email = %q{philnash@gmail.com}
- s.extra_rdoc_files = ["lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3.rb", "lib/bitly/version.rb", "lib/bitly.rb", "README.txt"]
- s.files = ["bitly.gemspec", "History.txt", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3.rb", "lib/bitly/version.rb", "lib/bitly.rb", "Manifest", "Rakefile", "README.txt", "test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/fixtures/cnn.json", "test/fixtures/cnn_and_google.json", "test/fixtures/expand_cnn.json", "test/fixtures/expand_cnn_and_google.json", "test/fixtures/google_and_cnn_info.json", "test/fixtures/google_info.json", "test/fixtures/google_stats.json", "test/fixtures/shorten_error.json", "test/test_helper.rb"]
+ s.extra_rdoc_files = ["README.txt", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/version.rb"]
+ s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "bitly.gemspec", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/version.rb", "test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/fixtures/cnn.json", "test/fixtures/cnn_and_google.json", "test/fixtures/expand_cnn.json", "test/fixtures/expand_cnn_and_google.json", "test/fixtures/google_and_cnn_info.json", "test/fixtures/google_info.json", "test/fixtures/google_stats.json", "test/fixtures/shorten_error.json", "test/test_helper.rb"]
s.homepage = %q{http://github.com/philnash/bitly}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitly", "--main", "README.txt"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{bitly}
- s.rubygems_version = %q{1.3.6}
+ s.rubygems_version = %q{1.3.7}
s.summary = %q{Use the bit.ly API to shorten or expand URLs}
s.test_files = ["test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/test_helper.rb"]
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<crack>, [">= 0.1.4"])
s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
else
View
3  lib/bitly/v3.rb
@@ -6,4 +6,7 @@
require 'v3/bitly'
require 'v3/client'
require 'v3/url'
+require 'v3/referrer'
+require 'v3/day'
+require 'v3/country'
require 'v3/missing_url'
View
37 lib/bitly/v3/client.rb
@@ -83,6 +83,30 @@ def lookup(input)
end
return results.length > 1 ? results : results[0]
end
+
+ # Expands either a short link or hash and gets the referrer data for that link
+ #
+ # This method does not take an array as an input
+ def referrers(input)
+ get_single_method('referrers', input)
+ end
+
+ # Expands either a short link or hash and gets the country data for that link
+ #
+ # This method does not take an array as an input
+ def countries(input)
+ get_single_method('countries', input)
+ end
+
+ # Takes a short url, hash or array of either and gets the clicks by minute of each of the last hour
+ def clicks_by_minute(input)
+ get_method(:clicks_by_minute, input)
+ end
+
+ # Takes a short url, hash or array of either and gets the clicks by day
+ def clicks_by_day(input)
+ get_method(:clicks_by_day, input)
+ end
private
@@ -100,6 +124,18 @@ def get(method, opts={})
def is_a_short_url?(input)
input.match(/^http:\/\//)
end
+
+ def get_single_method(method, input)
+ raise ArgumentError.new("This method only takes a hash or url input") unless input.is_a? String
+ if is_a_short_url?(input)
+ query = "shortUrl=#{CGI.escape(input)}"
+ else
+ query = "hash=#{CGI.escape(input)}"
+ end
+ query = "/#{method}?" + query
+ response = get(query)
+ return Bitly::V3::Url.new(self,response['data'])
+ end
def get_method(method, input)
input = [input] if input.is_a? String
@@ -127,7 +163,6 @@ def get_method(method, input)
end
return results.length > 1 ? results : results[0]
end
-
end
end
end
View
13 lib/bitly/v3/country.rb
@@ -0,0 +1,13 @@
+module Bitly
+ module V3
+ class Country
+ attr_reader :clicks, :country
+
+ def initialize(opts)
+ ['clicks', 'country'].each do |attribute|
+ instance_variable_set("@#{attribute}".to_sym, opts[attribute])
+ end
+ end
+ end
+ end
+end
View
13 lib/bitly/v3/day.rb
@@ -0,0 +1,13 @@
+module Bitly
+ module V3
+ # Day objects are created by the clicks_by_day method of a url
+ class Day
+ attr_reader :clicks, :day_start
+
+ def initialize(opts)
+ @clicks = opts['clicks']
+ @day_start = Time.at(opts['day_start']) if opts['day_start']
+ end
+ end
+ end
+end
View
13 lib/bitly/v3/referrer.rb
@@ -0,0 +1,13 @@
+module Bitly
+ module V3
+ class Referrer
+ attr_reader :clicks, :referrer, :referrer_app, :url
+
+ def initialize(opts)
+ ['clicks', 'referrer', 'referrer_app', 'url'].each do |attribute|
+ instance_variable_set("@#{attribute}".to_sym, opts[attribute])
+ end
+ end
+ end
+ end
+end
View
55 lib/bitly/v3/url.rb
@@ -18,6 +18,19 @@ def initialize(client, opts={})
@global_clicks = opts['global_clicks']
@title = opts['title']
@created_by = opts['created_by']
+ @referrers = opts['referrers'].inject([]) do |results, referrer|
+ results << Bitly::V3::Referrer.new(referrer)
+ end if opts['referrers']
+ @countries = opts['countries'].inject([]) do |results, country|
+ results << Bitly::V3::Country.new(country)
+ end if opts['countries']
+ if opts['clicks'] && opts['clicks'][0].is_a?(Hash)
+ @clicks_by_day = opts['clicks'].inject([]) do |results, day|
+ results << Bitly::V3::Day.new(day)
+ end
+ else
+ @clicks_by_minute = opts['clicks']
+ end
end
@short_url = "http://bit.ly/#{@user_hash}" unless @short_url
end
@@ -59,6 +72,38 @@ def created_by(opts={})
@created_by
end
+ # If the url already has referrer data, return it.
+ # IF there is no referrer or <tt>:force => true</tt> is passed,
+ # updates the referrers and returns them
+ def referrers(opts={})
+ update_referrers if @referrers.nil? || opts[:force]
+ @referrers
+ end
+
+ # If the url already has country data, return it.
+ # IF there is no country or <tt>:force => true</tt> is passed,
+ # updates the countries and returns them
+ def countries(opts={})
+ update_countries if @countries.nil? || opts[:force]
+ @countries
+ end
+
+ def clicks_by_minute(opts={})
+ if @clicks_by_minute.nil? || opts[:force]
+ full_url = @client.clicks_by_minute(@user_hash || @short_url)
+ @clicks_by_minute = full_url.clicks_by_minute
+ end
+ @clicks_by_minute
+ end
+
+ def clicks_by_day(opts={})
+ if @clicks_by_day.nil? || opts[:force]
+ full_url = @client.clicks_by_day(@user_hash || @short_url)
+ @clicks_by_day = full_url.clicks_by_day
+ end
+ @clicks_by_day
+ end
+
private
def update_clicks_data
@@ -72,6 +117,16 @@ def update_info
@created_by = full_url.created_by
@title = full_url.title
end
+
+ def update_referrers
+ full_url = @client.referrers(@user_hash || @short_url)
+ @referrers = full_url.referrers
+ end
+
+ def update_countries
+ full_url = @client.countries(@user_hash || @short_url)
+ @countries = full_url.countries
+ end
end
end
end
View
2  lib/bitly/version.rb
@@ -1,3 +1,3 @@
module Bitly
- VERSION = '0.5.3'
+ VERSION = '0.5.4'
end
Please sign in to comment.
Something went wrong with that request. Please try again.