philnash / bitly Goto Github PK
View Code? Open in Web Editor NEW🗜 A Ruby wrapper for the bit.ly API
Home Page: https://rubygems.org/gems/bitly
License: MIT License
🗜 A Ruby wrapper for the bit.ly API
Home Page: https://rubygems.org/gems/bitly
License: MIT License
With the bitly snafu today, is there anyway to put in short timeout ( 2-3 seconds max ) or make it configurable for this library?
Some of my pages do on-the-fly Bitly link conversions for creating tweets and Bitly was taking over 30 seconds to return a timeout error which tied up processes on heroku. To a user, it seemed as the app was down because all processes were tied up.
I use bitly version 3.
when i call #short_url on bitly it work perfectly.
but call #jmp_url not work. An error occurred.
bitly.shorten("http://google.com/123/456").jmp_url
NoMethodError: undefined method `jmp_url' for #Bitly::V3::Url:0x108e9b908
from (irb):2
How can I do it??
Looks like Bitly have added some endpoints since this was last released. Here are the new ones I think are missing:
I came across this problem because when gem 'version'
was included after gem 'bitly'
, it would cause an error when I tried to run a rake task.
undefined method 'to_version' for "1.2.8":String
When I included gem 'version'
first, it would run without problems.
So, I think what is happening is that the line $:.unshift File.dirname(__FILE__)
is including all of the /lib/bitly
files in the load path and causing naming conflicts. Is there a reason why that line is in there at all? I changed the file to look like this and it fixed the problem.
# /lib/bitly/v3.rb
require 'httparty'
require 'cgi'
require 'oauth2'
require 'bitly/v3/bitly'
require 'bitly/v3/client'
require 'bitly/v3/url'
require 'bitly/v3/referrer'
require 'bitly/v3/day'
require 'bitly/v3/country'
require 'bitly/v3/missing_url'
require 'bitly/v3/realtime_link'
require 'bitly/v3/oauth'
require 'bitly/v3/user'
Problem with
Gem version: httparty 0.15.1
While setting up the bitly gem I noticed that the API URL is set to:
API_URL = 'http://api.bit.ly/'
However when reviewing api.bitly.com, in their best practices I see their example of a valid API call to shorten the link uses:
https://api-ssl.bitly.com/
Is there any plan on updating this gem to use the https link so api keys aren't transmitted in cleartext?
Hello there,
I've been scratching my head for the last 2 hours and I can't find what I'm doing wrong.
Rails 4.2
gem 'bitly', '~> 0.10.4'
My initializer uses oAuth:
Bitly.use_api_version_3
Bitly.configure do |config|
config.api_version = 3
config.access_token = "0a9e6c23bb42efd17feffa3266dbd4aa7ec8d045"
end
Now when I'm trying to get in the view the short_url, I have in my controller show
@url = Bitly.client.shorten('http://www.google.com')
@short_url = @url.short_url
Now this is where it gets confusing.
Why is that?
Where is my mistake?
Thanks!
I'm thinking that to_s on the url objects should return the short url.
maybe bump the version nr, or mention it's not available in 0.8.1
I can't figure out how to create a custom bitlink / back half with the gem.
The docs say the endpoint is supported, but i cant make it work.
any help is appreciated.
Update oauth to >= 1.0.0
Hey, I might be doing something wrong, but I'm not able to configure a Bitly.client
through an initializer.
I have config/initializers/secrets.rb
with the following:
if Rails.env == "development"
ENV['BITLY_USERNAME'] = user_name
ENV['BITLY_API_KEY'] = api_key
end
I'm getting both from https://bitly.com/a/your_api_key.
And I have config/initializers/bitly.rb
with this:
Bitly.configure do |config|
config.api_version = 3
config.login = ENV['BITLY_USERNAME']
config.api_key = ENV['BITLY_API_KEY']
end
When I launch a Rails console, the env variables are correctly set, but initializing a new client with Bitly.client
doesn't seem to be using the login credentials. It returns:
<Bitly::V3::Client:0x007ff960af3230 @default_query_opts={:login=>nil, :apiKey=>nil}>
client = Bitly.client
client.shorten("http://google.com").jmp_url
I'm getting a no method error.
NoMethodError: undefined method 'shorten_url' for #<Bitly::V3::Url:0x007fe7e9a1dd20>
client.shorten("http://google.com").short_url
works as expected and generates a bit.ly
url.
Hi, just installed the gem and was going to try it out when I got this error message when requiring the gem:
/Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1064:in `escape': can't convert Pathname to String (TypeError)
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1064:in `block in loaded_path?'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1063:in `each'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1063:in `find'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1063:in `loaded_path?'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from /Users/Johan/.rvm/gems/ruby-1.9.2-p0/gems/httparty-0.7.4/lib/httparty.rb:10:in `<top (required)>'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require'
from /Users/Johan/.rvm/gems/ruby-1.9.2-p0/gems/bitly-0.6.1/lib/bitly/v3.rb:3:in `<top (required)>'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require'
from /Users/Johan/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require'
from /Users/Johan/.rvm/gems/ruby-1.9.2-p0/gems/bitly-0.6.1/lib/bitly.rb:9:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:33:in `require'
from <internal:lib/rubygems/custom_require>:33:in `rescue in require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from bitly.rb:1:in `<main>'
Apparently it's something weird going on in the gem business – which bitly
doesn't show me anything, and after a double-check in my gem dir (/Users/Johan/.rvm/gems/ruby-1.9.2-p0/bin
) the bitly gem is nowhere to be found.
Would be really awesome if this could be fixed. I'm running Ruby 1.9.2 and RubyGems 1.6.2.
After upgrading the rails to 5 and ruby to 2.5 I started to receive this error,
/config/initializers/bitly.rb:1
:in <top (required)>: undefined method "use_api_version_3" for Bitly:Module (NoMethodError)
any idea how to fix this issue ?
this is how the initializer file config/initializers/bitly.rb
looks like
Bitly.use_api_version_3
Bitly.configure do |config|
config.api_version = 3
config.access_token = Rails.application.secrets["bitly"]["access_token"]
end
After installing the gem I am getting errors.
jdms-mm:dev User$ ruby script/server
=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
no such file to load -- oauth2
/Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in gem_original_require' /Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in
require'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in
new_constants_in'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/bitly-0.6.2/lib/bitly/v3.rb:5 /Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
gem_original_require'
/Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in
require'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in new_constants_in' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in
require'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/bitly-0.6.2/lib/bitly.rb:9
/Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in gem_original_require' /Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
require'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in
new_constants_in'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/rails/gem_dependency.rb:215:in
load'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:307:in load_gems' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:307:in
each'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:307:in load_gems' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:169:in
process'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:113:in send' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/initializer.rb:113:in
run'
/Users/jdm/Documents/rails/obviam/dev/config/environment.rb:9
/Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in gem_original_require' /Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
require'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in
new_constants_in'
/Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/jdm/.rvm/gems/ruby-1.8.7-p352@r238/gems/rails-2.3.8/lib/commands/server.rb:84 /Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
gem_original_require'
/Users/jdm/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `require'
script/server:3
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-09-05 02:29:43] INFO WEBrick 1.3.1
[2012-09-05 02:29:43] INFO ruby 1.8.7 (2011-06-30) [i686-darwin11.2.0]
[2012-09-05 02:29:43] INFO WEBrick::HTTPServer#start: pid=57637 port=3000
Here's my gems:
jdms-mm:~ User$ gem list
*** LOCAL GEMS ***
actionmailer (2.3.8)
actionpack (2.3.8)
activemodel (3.2.8)
activerecord (2.3.8)
activeresource (2.3.8)
activesupport (3.2.8, 2.3.8)
addressable (2.3.2, 2.2.8)
arel (3.0.2)
Ascii85 (1.0.1)
bitly (0.6.2)
builder (3.0.0)
bundler (1.0.21)
cancan (1.5.1)
chunky_png (1.2.6)
crack (0.3.1)
daemons (1.0.10)
delayed_job (2.0.4)
faraday (0.8.4, 0.7.6, 0.7.4)
faraday_middleware (0.7.0)
haml (3.1.7)
hashery (2.0.1)
hashie (1.2.0)
httparty (0.8.3)
httpauth (0.1)
i18n (0.4.2)
json (1.7.5)
json_pure (1.7.5)
jwt (0.1.5)
mime-types (1.19)
multi_json (1.3.6, 1.0.4)
multi_xml (0.5.1)
multipart-post (1.1.5)
mysql (2.8.1)
oauth (0.4.7, 0.4.5)
oauth2 (0.8.0, 0.5.1, 0.5.0)
omniauth (1.1.1)
omniauth-linkedin (0.0.8)
omniauth-oauth (1.0.1)
rack (1.4.1, 1.1.3)
rails (2.3.8)
rake (0.8.7)
rest-client (1.6.7)
ruby-rc4 (0.1.5)
rvm (1.11.3.5)
ttfunk (1.0.3)
tzinfo (0.3.33)
jdms-mm:~ User$
The oauth2 gem is (at the time of writing) at version 0.9.1, and version 0.9.0 was the first to officially support Ruby 2. Is there a known reason to limit the oauth2 dependency to < 0.9 now? The limit in the gemspec was set before oauth2 0.9.0 came out.
I am using bitly gem to access the bitly api. While fetching the referrers using Bitly::V3::User i am getting only today's data..How can i get the last 30 day's referrers. My code is here.
o = Bitly::V3::OAuth.new(configatron.bitly.client_id, configatron.bitly.client_secret)
@access_token = o.get_access_token_from_code(params[:code], configatron.bitly.redirect_url)
u=Bitly::V3::User.new(o.access_token)
@a = u.link_history
@r = u.referrers
In particular, if the input to these methods is an array, the array will be empty upon return.
Example code:
Bitly.use_api_version_3
MyBitly = Bitly.new("myuser", "myaccesstoken")
urls = [some, array, of bitlys]
clicks = MyBitly.clicks(urls)
puts "#{urls.inspect}\n"
Expected output: the array of bitlys
Actual output: an array the same size full of nils
Hi.
The bitly documents say that
API V3 will be deactivated on March 1, 2020.
Are there any plan to support API V4 in this gem?
http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks_by_day shows that clicks_by_day can take an optional days parameter between 1 and 30. Currently the v3 client does not accept this parameter and defaults to 7 days.
Following the README when I try to client.expand
I get a 404.
This is my code:
require 'bitly'
token = '«myTokenHere»'
client = Bitly::API::Client.new(token: token)
bitlink = client.expand(bitlink: "https://bit.ly/«myShortCode»")
p bitlink.long_url
This is my env:
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
$ gem list | grep bitly
bitly (2.0.1)
This is the pry-ed output ( :
$ ruby bitly.rb
From: /home/luilver/.rvm/gems/ruby-2.7.1/gems/bitly-2.0.1/lib/bitly/api/client.rb:58 Bitly::API::Client#request:
52: def request(path:, method: 'GET', params: {}, headers: {})
53: params = params.select { |k,v| !v.nil? }
54: headers = default_headers.merge(headers)
55: uri = Bitly::API::BASE_URL.dup
56: uri.path += path
57: request = Bitly::HTTP::Request.new(uri: uri, method: method, params: params, headers: headers)
=> 58: binding.pry
59: @http.request(request)
60: end
[1] pry(#<Bitly::API::Client>)> @http.request(request)
Bitly::Error: [404] NOT_FOUND
from /home/luilver/.rvm/gems/ruby-2.7.1/gems/bitly-2.0.1/lib/bitly/http/client.rb:28:in `request'
[2] pry(#<Bitly::API::Client>)> uri
=> #<URI::HTTPS https://api-ssl.bitly.com/v4/expand>
[3] pry(#<Bitly::API::Client>)>
From bitly.rb:
$:.unshift File.dirname(__FILE__)
You should not be messing with other people's $LOAD_PATH
(e.g., $:
). This line ends up causing require 'utils'
, require 'client'
, require 'url'
, require 'version'
, and require 'v3'
to all preferentially load the copy from the bitly gem, instead of whatever they should actually point to.
Any chance for a bump there?
Hey! would love to know, how can I pass the title for the long URL, so that in future I can search based on the title & get short link
Thanks
/v3/user/link_history
OAuth 2 endpoint that provides a given user’s link shortening history, in reverse chronological order (most recent to least recent).
Parameters
access_token is the OAuth access token for a bitly user.
format (optional) indicates the requested response format. supported formats: json (default), xml.
limit (optional, default: 50) indicated the number of results to return.
offset (optional) indicates the number of results to skip (ie, offset=1 skips the most recent link, offset=10 skips the 10 most recent link, offset=50 would paginate the default result set).
created_before (optional) unix timestamp indicating only to return links created at or before the specified time.
created_after (optional) unix timestamp indicating only to return links created at or after the specified time.
Note
This endpoint is only available on https://api-ssl.bitly.com/
This endpoint returns a maximum of 100 results.
Output
long_url is the long link that was shortened.
link is this particular user's bitly link for the specified long_url.
aggregate_link is the global bitly link for the specified long_url which can be used to track aggregate stats across all matching bitly links.
created_at is a unix timestamp representing the time at which the long_url was first shortened by this user.
title is the page title of the long_url.
This went away a long time ago!
Installing ri documentation for bitly-0.8.1...
unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for lib/bitly/v3/user.rb, skipping
I can create a pull request for this if wanted.
How can I make api requests using code received from bitly oauth api?
Also, I tried to use oauth class form bitly gem, but I have got an error:
undefined method `web_server' for #<OAuth2::Client:0x10f6ad280>
@bitly = Bitly::V3::OAuth.new client_id, client_secret
@bitly.authorize_url(some_url)
Sorry, I must be doing something wrong, as I'm following the example but when trying to shorten a URL I keep getting an error. I do this:
Bitly.use_api_version_3
Bitly.configure do |config|
config.api_version = 3
config.login = 'mylogin'
config.api_key = 'myGenericAccessToken'
end
bitly_client = Bitly.client
#<Bitly::V3::Client:0x007fc87589bdc8 @default_query_opts={:login=>"mylogin", :apiKey=>"myGenericAccessToken"}>
u = bitly_client.shorten("http://www.example.org")
That gets me:
BitlyError: INVALID_APIKEY - '500'
from /Users/phil/.rvm/gems/ruby-1.9.3-p392/gems/bitly-0.9.0/lib/bitly/v3/client.rb:141:in `get'
from /Users/phil/.rvm/gems/ruby-1.9.3-p392/gems/bitly-0.9.0/lib/bitly/v3/client.rb:41:in `shorten'
from (irb):17
from /Users/phil/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Anything obvious I've done wrong?
Let's pass these tests.
And clean up old v2 code.
Hi am using bitly v3 API . but some how I am getting depreciation messages for bitly. is there anything I am missing.
According to https://dev.bitly.com/link_metrics.html#v3_link_clicks,
we can send multiple parameters like link, unit, etc.
i am actually looking for Clicks by Day
i tried Bitly.client.clicks("http://bit.ly/2tAL7YS").clicks_by_day
, works fine
but i need to change Time Zone value
i tried this Bitly.client.info({"link" =>"http://bit.ly/2tAL7YS", "timezone" => -6}).clicks_by_day
and i am getting error. Please help
Bitly client is using oauth >= 0.1.1, but the latest OAuth2 (0.5.1) has moved off pretty far.
For example, line 30 of lib/bitly/v3/oauth.rb
@access_token ||= ::OAuth2::AccessToken.new(client, token, nil, nil, params)
is not compatible with new OAuth2 AccessToken constructor
def initialize(client, token, opts={})
We are depending on oauth2 via two different routes and Bitly client's version is too old to be usable. Can you look into this?
We need this fix, and would prefer to not build our own gem:
http://github.com/philnash/bitly/commit/a9509e8ac566f09c5a8b973570f8c6480f38de52
Thanks...
https://github.com/philnash/bitly/blob/master/test/bitly/test_client.rb#L23
assert_equal login, login
should be...
assert_equal login, b.login
This gem was written such a long time ago. It's more than time for an update. And I don't just mean deprecate the (surely by now) long gone v2 API.
I am adding this issue as a commitment that I am going to rewrite this gem from scratch. It's going to be more consistent, more up to date and just generally better all round. All you Bitly API calling dreams will come true.
Getting this with * bitly (0.8.1)
BitlyError:
JSON Parse Error(Bit.ly messed up) - '69'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.