Skip to content

small updates #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Rake::TestTask.new(:test) do |test|
test.verbose = true
end

require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = BitlyOAuth::VERSION

Expand Down
20 changes: 8 additions & 12 deletions lib/bitly-oauth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def lookup(input)

private

def get_single_method(method, input)
raise ArgumentError.new("This method only takes a hash or url input") unless input.is_a? String

response = v3(method, key_for(input) => input.to_a)
BitlyOAuth::Url.new(self, response)
end

def get_method(method, input, options={})
options = ParamsHash[ options ]
options.symbolize_keys!
Expand All @@ -101,17 +108,6 @@ def get_method(method, input, options={})
results.length > 1 ? results : results[0]
end

def get_single_method(method, input)
raise ArgumentError.new("This method only takes a hash or url input") unless input.is_a? String

response = v3(method, key_for(input) => input.to_a)
BitlyOAuth::Url.new(self, response)
end

def access_token
@access_token
end

def key_for(input)
input.match(/^http:\/\//) ? :shortUrl : :hash
end
Expand All @@ -132,7 +128,7 @@ def get(path, params = {})
end

def query(params)
{ :query => ParamsHash[ { :access_token => access_token }.merge(params) ].to_query }
{ :query => ParamsHash[ { :access_token => @access_token }.merge(params) ].to_query }
end
end
end
30 changes: 3 additions & 27 deletions lib/bitly-oauth/url.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
module BitlyOAuth

class Url
attr_reader :short_url, :long_url, :user_hash, :global_hash, :referrers, :countries

# Initialize with a bitly client and optional hash to fill in the details for the url.
def initialize(client, options = {})
@client = client
@short_url = options['url'] || options['short_url'] || "http://bit.ly/#{@user_hash}"
@user_hash = options['hash'] || options['user_hash']
@title = options['title']
@new_hash = options['new_hash'] == 1
@long_url = options['long_url']
@created_by = options['created_by']
@global_hash = options['global_hash']
@user_clicks = options['user_clicks']
@global_clicks = options['global_clicks']
@title = options['title']
@user_hash = options['hash'] || options['user_hash']
@short_url = options['url'] || options['short_url'] || "http://bit.ly/#{@user_hash}"

@referrers = options['referrers'].map do |referrer|
BitlyOAuth::Referrer.new(referrer)
Expand All @@ -33,46 +31,30 @@ def initialize(client, options = {})
end
end

# Returns true if the user hash was created first for this call
def new_hash?
@new_hash
end

# If the url already has click statistics, returns the user clicks.
# IF there are no click statistics or <tt>:force => true</tt> is passed,
# updates the stats and returns the user clicks
def user_clicks(options={})
update_clicks_data if @user_clicks.nil? || options[:force]
@user_clicks
end

# If the url already has click statistics, returns the global clicks.
# IF there are no click statistics or <tt>:force => true</tt> is passed,
# updates the stats and returns the global clicks
def global_clicks(options={})
update_clicks_data if @global_clicks.nil? || options[:force]
@global_clicks
end

# If the url already has the title, return it.
# IF there is no title or <tt>:force => true</tt> is passed,
# updates the info and returns the title
def title(options={})
update_info if @title.nil? || options[:force]
@title
end

# If the url already has the creator, return it.
# IF there is no creator or <tt>:force => true</tt> is passed,
# updates the info and returns the creator
def created_by(options={})
update_info if @created_by.nil? || options[:force]
@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(options={})
if @referrers.nil? || options[:force]
full_url = @client.referrers(@user_hash || @short_url)
Expand All @@ -81,19 +63,13 @@ def referrers(options={})
@referrers
end

# If the url already has referring_domains data, return it.
# IF there is no referring_domains or <tt>:force => true</tt> is passed,
# updates the referring_domains and returns them
def referring_domains(options={})
if @referring_domains.nil? || options[:force]
@referring_domains = @client.referring_domains(@short_url)
end
@referring_domains
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(options={})
if @countries.nil? || options[:force]
full_url = @client.countries(@user_hash || @short_url)
Expand Down
4 changes: 0 additions & 4 deletions lib/bitly-oauth/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module BitlyOAuth

class User
include HTTParty
base_uri 'https://api-ssl.bit.ly/v3/'

def initialize(access_token)
@access_token = access_token
end
Expand Down
2 changes: 0 additions & 2 deletions lib/bitly_oauth.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/9uX1TEinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"status_code": 200, "data": {"info": [{"global_hash": "18H1ET", "hash": "9uX1TE", "user_hash": "9uX1TE", "created_by": "philnash", "title": "A title"}]}, "status_txt": "OK"}
{"status_code": 200, "data": {"info": [{"global_hash": "18H1ET", "hash": "9uX1TE", "user_hash": "9uX1TE", "created_by": "philnash", "title": "A title"}]}, "status_txt": "OK"}
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'bitly_oauth'
require 'bitly-oauth'

FakeWeb.allow_net_connect = false

Expand Down