Skip to content

Commit

Permalink
Added support Bit.ly API v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoder committed May 13, 2010
1 parent 62d75df commit 0dce0e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Features:
* em-http-request: .get, etc are synchronous, while .aget, etc are async
* em-mysqlplus: .query is synchronous, while .aquery is async
* remcached: .get, etc, and .multi_* methods are synchronous
* bitly: synchronous api calls with EM::HttpRequest.
* bitly v2 and v3: synchronous api calls with EM::HttpRequest.

## Example with async em-http client:
require "em-synchrony/em-http"
Expand Down Expand Up @@ -80,7 +80,7 @@ Features:
EventMachine.stop
end

## Example with async Bitly client:
## Example with async Bitly client API v2:

require "em-synchrony/em-bitly"
EM.synchrony do
Expand All @@ -90,7 +90,24 @@ Features:

p "Short #{url} => #{short.jmp_url}"
end

## Example with async Bitly client API v3:

require "em-synchrony/em-bitly"

Bitly.use_api_version_3

EM.synchrony do
bitly = Bitly.new('[INSERT_LOGIN]', '[INSERT_API_KEY]')
url = 'http://github.com/igrigorik/em-synchrony'
domain='nyti.ms'
pro = bitly.bitly_pro_domain(domain)
p "Domain #{domain} pro=#{pro}"
end


# License

(The MIT License)
Expand Down
34 changes: 33 additions & 1 deletion lib/em-synchrony/em-bitly.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'cgi'

begin
require "em-synchrony/em-http"
require "bitly"
rescue LoadError => error
raise "Missing EM-Synchrony dependencies: gem install em-http-request; gem install bitly -v=0.4.0"
raise "Missing EM-Synchrony dependencies: gem install em-http-request; gem install bitly -v=0.5.0"
end

module Bitly
Expand All @@ -23,4 +25,34 @@ def get_result(request)
end
end
end

module V3
class Client
class << self
def get(method, query)
query_values=[]
query[:query].each do |key, value|
query_values << "#{key}=#{CGI::escape(value.to_s)}"
end
query_values=query_values.join('&')
request=(method[0]=='/' ? "#{base_uri}#{method}" : method)
request=(request.include?('?') ? "#{request}&#{query_values}" : "#{request}?#{query_values}")

http = EventMachine::HttpRequest.new(request).get(:timeout => 100)
response = if (http.response_header.status == 200)
Crack::JSON.parse(http.response)
else
{'errorMessage' => 'JSON Parse Error(Bit.ly messed up)', 'errorCode' => 69, 'statusCode' => 'ERROR'}
end

if response['status_code'] == 200
return response
else
raise BitlyError.new(response['status_txt'], response['status_code'])
end

end
end
end
end
end

0 comments on commit 0dce0e6

Please sign in to comment.