Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
test shorten_url with faraday helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 3, 2011
1 parent 709301d commit c70fa42
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
54 changes: 48 additions & 6 deletions lib/service.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'faraday'

class Service
class << self
attr_reader :hook_name
Expand Down Expand Up @@ -26,20 +28,60 @@ def receive(event_type, data, payload)
attr_reader :data
attr_reader :payload

def initialize(event_type)
attr_writer :faraday

def initialize(event_type, data, payload)
@event_type = event_type
@data = data
@payload = payload
@faraday = nil
end

# Public
def shorten_url(url)
Service::Timeout.timeout(6, Service::TimeoutError) do
short = Net::HTTP.get("api.bit.ly", "/shorten?version=2.0.1&longUrl=#{url}&login=github&apiKey=R_261d14760f4938f0cda9bea984b212e4")
short = JSON.parse(short)
short["errorCode"].zero? ? short["results"][url]["shortUrl"] : url
res = http_get do |req|
req.url "http://api.bit.ly/shorten",
:version => '2.0.1',
:longUrl => url,
:login => 'github',
:apiKey => 'R_261d14760f4938f0cda9bea984b212e4'
end
rescue Service::TimeoutError

short = JSON.parse(res.body)
short["errorCode"].zero? ? short["results"][url]["shortUrl"] : url
rescue TimeoutError
url
end

# Public
def http_get(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
http_method(:get, url, nil, headers, &block)
end

# Public
def http_post(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
http_method(:post, url, body, headers, &block)
end

# Public
def http_method(method, url = nil, body = nil, headers = nil)
faraday.send(method) do |req|
req.url(url) if url
req.headers.update(headers) if headers
req.body = body if body
yield req if block_given?
end
end

def faraday(options = {})
@faraday ||= begin
options[:timeout] ||= 6
Faraday.new(options) { |b| b.adapter(:net_http) }
end
end

# Raised when an unexpected error occurs during service hook execution.
class Error < StandardError
attr_reader :original_exception
Expand Down
29 changes: 29 additions & 0 deletions test/service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test/unit'
require File.expand_path('../../config/load', __FILE__)

class ServiceTest < Test::Unit::TestCase
class TestService < Service
end

def setup
@stubs = Faraday::Adapter::Test::Stubs.new
@service = TestService.new(:push, 'data', 'payload')
@service.faraday = Faraday.new { |b| b.adapter(:test, @stubs) }
end

def test_url_shorten
url = "http://github.com"
bitly = "/shorten?apiKey=%s&login=%s&longUrl=%s&version=%s" % [
'R_261d14760f4938f0cda9bea984b212e4', 'github', 'http%3A%2F%2Fgithub.com', '2.0.1' ]
@stubs.get bitly do
[200, {}, {
'errorCode' => 0,
'results' => {
url => {'shortUrl' => 'short'}
}
}.to_json]
end

assert_equal 'short', @service.shorten_url(url)
end
end

0 comments on commit c70fa42

Please sign in to comment.