Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit b6660e34562c938db38b9537336d69e97abacc4a @treeder treeder committed Dec 16, 2011
Showing with 256 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +38 −0 README.markdown
  3. +39 −0 Rakefile
  4. +5 −0 VERSION.yml
  5. +1 −0 lib/rest.rb
  6. +130 −0 lib/rest/client.rb
  7. +20 −0 test/test_base.rb
  8. +22 −0 test/test_rest.rb
@@ -0,0 +1 @@
+.idea
@@ -0,0 +1,38 @@
+Rest Wrapper
+-------------
+
+Getting Started
+==============
+
+Install the gem:
+
+ gem install rest
+
+Create an IronMQ client object:
+
+ @rest = Rest::Client.new()
+
+Then use it.
+
+GET
+=========
+
+ @rest.get(full_url, options...)
+
+options:
+
+- params - query params for url
+- headers
+
+POST
+======
+
+ @rest.post(full_url, options...)
+
+options:
+
+- headers
+- body
+
+
+
@@ -0,0 +1,39 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler2'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "rest"
+ gem.summary = "Rest client wrapper that chooses best installed client."
+ gem.description = "Rest client wrapper that chooses best installed client."
+ gem.email = "travis@iron.io"
+ gem.homepage = "http://www.iron.io"
+ gem.authors = ["Travis Reeder"]
+ gem.add_dependency 'rest-client'
+ #gem.add_dependency 'typhoeus'
+ gem.required_ruby_version = '>= 1.9'
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler2"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+task :default => :test
+
+require 'rdoc/task'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'doc'
+ rdoc.title = "iron_mq #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,5 @@
+---
+:major: 0
+:minor: 0
+:patch: 1
+:build:
@@ -0,0 +1 @@
+require_relative 'rest/client'
@@ -0,0 +1,130 @@
+require 'json'
+require 'logger'
+
+# This is a simple wrapper that can use different http clients depending on what's installed.
+# The purpose of this is so that users who can't install binaries easily (like windoze users) can have fallbacks that work
+
+module Rest
+ def self.gem=(g)
+ @gem = g
+ end
+
+ def self.gem
+ @gem
+ end
+
+ begin
+ require 'typhoeus'
+ Rest.gem = :typhoeus
+ rescue LoadError => ex
+ puts "Could not load typhoeus. #{ex.class.name}: #{ex.message}. Falling back to rest-client. Please install 'typhoeus' gem for best performance."
+ require 'rest_client'
+ Rest.gem = :rest_client
+ end
+
+ class RestClientResponseWrapper
+ def initialize(response)
+ @response = response
+ end
+
+ def code
+ @response.code
+ end
+
+ def body
+ @response.body
+ end
+
+ end
+
+ class ClientError < StandardError
+
+ end
+
+ class RestClientExceptionWrapper < ClientError
+ def initialize(ex)
+ super(ex.message)
+ @ex = ex
+ end
+ end
+
+ class TimeoutError < ClientError
+ def initialize(msg=nil)
+ msg ||= "HTTP Request Timed out."
+ super(msg)
+ end
+ end
+
+ class TyphoeusTimeoutError < TimeoutError
+ def initialize(response)
+ msg ||= "HTTP Request Timed out. Curl code: #{response.curl_return_code}. Curl error msg: #{response.curl_error_message}."
+ super(msg)
+ end
+ end
+
+ class Client
+
+ def initialize(options={})
+ @logger = Logger.new(STDOUT)
+ @logger.level=Logger::INFO
+
+ end
+
+ def default_typhoeus_options
+ req_hash = {}
+ req_hash[:connect_timeout] = 5000
+ req_hash[:timeout] ||= 10000
+ req_hash[:follow_location] = true
+ req_hash[:max_redirects] = 2
+ req_hash
+ end
+
+ def get(url, req_hash={})
+ if Rest.gem == :typhoeus
+ req_hash = default_typhoeus_options.merge(req_hash)
+
+ # puts "REQ_HASH=" + req_hash.inspect
+ response = Typhoeus::Request.get(url, req_hash)
+ #p response
+ if response.timed_out?
+ raise TyphoeusTimeoutError.new(response)
+ end
+ else
+ begin
+ headers = req_hash[:headers] || {}
+ r2 = RestClient.get(url, req_hash.merge(headers))
+ response = RestClientResponseWrapper.new(r2)
+ # todo: make generic exception
+ rescue RestClient::Exception => ex
+ raise RestClientExceptionWrapper.new(ex)
+ end
+ end
+ response
+ end
+
+ def post(url, req_hash={})
+ if Rest.gem == :typhoeus
+ # todo: should change this timeout to longer if it's for posting file
+ req_hash[:connect_timeout] = 5000
+ req_hash[:timeout] ||= 10000
+ # puts "REQ_HASH=" + req_hash.inspect
+ response = Typhoeus::Request.post(url, req_hash)
+ #p response
+ if response.timed_out?
+ raise TyphoeusTimeoutError.new(response)
+ end
+ else
+ begin
+ headers = req_hash[:headers] || {}
+ r2 = RestClient.post(url, req_hash[:body], headers)
+ response = RestClientResponseWrapper.new(r2)
+ # todo: make generic exception
+ rescue RestClient::Exception => ex
+ raise RestClientExceptionWrapper.new(ex)
+ end
+ end
+ response
+ end
+
+ end
+end
@@ -0,0 +1,20 @@
+gem 'test-unit'
+require 'test/unit'
+require 'yaml'
+begin
+ require File.join(File.dirname(__FILE__), '../lib/rest')
+rescue Exception => ex
+ puts "Could NOT load gem: " + ex.message
+ raise ex
+end
+
+
+class TestBase < Test::Unit::TestCase
+ def setup
+ puts 'setup'
+ #@config = YAML::load_file(File.expand_path(File.join("~", "Dropbox", "configs", "rest", "test", "config.yml")))
+ @rest = Rest::Client.new
+ #@client.logger.level = Logger::DEBUG
+
+ end
+end
@@ -0,0 +1,22 @@
+# Put config.yml file in ~/Dropbox/configs/ironmq_gem/test/config.yml
+
+gem 'test-unit'
+require 'test/unit'
+require 'yaml'
+require_relative 'test_base'
+
+class TestTests < TestBase
+ def setup
+ super
+
+
+ end
+
+ def test_basics
+ response = @rest.get("http://www.github.com")
+ p response.body
+
+ end
+
+end
+

0 comments on commit b6660e3

Please sign in to comment.