rest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ Rest Wrapper
2
+ -------------
3
+
4
+ Getting Started
5
+ ==============
6
+
7
+ Install the gem:
8
+
9
+ gem install rest
10
+
11
+ Create an IronMQ client object:
12
+
13
+ @rest = Rest::Client.new()
14
+
15
+ Then use it.
16
+
17
+ GET
18
+ =========
19
+
20
+ @rest.get(full_url, options...)
21
+
22
+ options:
23
+
24
+ - params - query params for url
25
+ - headers
26
+
27
+ POST
28
+ ======
29
+
30
+ @rest.post(full_url, options...)
31
+
32
+ options:
33
+
34
+ - headers
35
+ - body
36
+
37
+
38
+
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler2'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rest"
8
+ gem.summary = "Rest client wrapper that chooses best installed client."
9
+ gem.description = "Rest client wrapper that chooses best installed client."
10
+ gem.email = "travis@iron.io"
11
+ gem.homepage = "http://www.iron.io"
12
+ gem.authors = ["Travis Reeder"]
13
+ gem.add_dependency 'rest-client'
14
+ #gem.add_dependency 'typhoeus'
15
+ gem.required_ruby_version = '>= 1.9'
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler2"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ task :default => :test
30
+
31
+ require 'rdoc/task'
32
+ Rake::RDocTask.new do |rdoc|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
34
+
35
+ rdoc.rdoc_dir = 'doc'
36
+ rdoc.title = "iron_mq #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
5
+ :build:
@@ -0,0 +1 @@
1
+ require_relative 'rest/client'
@@ -0,0 +1,130 @@
1
+ require 'json'
2
+ require 'logger'
3
+
4
+ # This is a simple wrapper that can use different http clients depending on what's installed.
5
+ # The purpose of this is so that users who can't install binaries easily (like windoze users) can have fallbacks that work
6
+
7
+ module Rest
8
+ def self.gem=(g)
9
+ @gem = g
10
+ end
11
+
12
+ def self.gem
13
+ @gem
14
+ end
15
+
16
+ begin
17
+ require 'typhoeus'
18
+ Rest.gem = :typhoeus
19
+ rescue LoadError => ex
20
+ puts "Could not load typhoeus. #{ex.class.name}: #{ex.message}. Falling back to rest-client. Please install 'typhoeus' gem for best performance."
21
+ require 'rest_client'
22
+ Rest.gem = :rest_client
23
+ end
24
+
25
+ class RestClientResponseWrapper
26
+ def initialize(response)
27
+ @response = response
28
+ end
29
+
30
+ def code
31
+ @response.code
32
+ end
33
+
34
+ def body
35
+ @response.body
36
+ end
37
+
38
+ end
39
+
40
+ class ClientError < StandardError
41
+
42
+ end
43
+
44
+ class RestClientExceptionWrapper < ClientError
45
+ def initialize(ex)
46
+ super(ex.message)
47
+ @ex = ex
48
+ end
49
+ end
50
+
51
+ class TimeoutError < ClientError
52
+ def initialize(msg=nil)
53
+ msg ||= "HTTP Request Timed out."
54
+ super(msg)
55
+ end
56
+ end
57
+
58
+ class TyphoeusTimeoutError < TimeoutError
59
+ def initialize(response)
60
+ msg ||= "HTTP Request Timed out. Curl code: #{response.curl_return_code}. Curl error msg: #{response.curl_error_message}."
61
+ super(msg)
62
+ end
63
+ end
64
+
65
+ class Client
66
+
67
+ def initialize(options={})
68
+ @logger = Logger.new(STDOUT)
69
+ @logger.level=Logger::INFO
70
+
71
+ end
72
+
73
+ def default_typhoeus_options
74
+ req_hash = {}
75
+ req_hash[:connect_timeout] = 5000
76
+ req_hash[:timeout] ||= 10000
77
+ req_hash[:follow_location] = true
78
+ req_hash[:max_redirects] = 2
79
+ req_hash
80
+ end
81
+
82
+ def get(url, req_hash={})
83
+ if Rest.gem == :typhoeus
84
+ req_hash = default_typhoeus_options.merge(req_hash)
85
+
86
+ # puts "REQ_HASH=" + req_hash.inspect
87
+ response = Typhoeus::Request.get(url, req_hash)
88
+ #p response
89
+ if response.timed_out?
90
+ raise TyphoeusTimeoutError.new(response)
91
+ end
92
+ else
93
+ begin
94
+ headers = req_hash[:headers] || {}
95
+ r2 = RestClient.get(url, req_hash.merge(headers))
96
+ response = RestClientResponseWrapper.new(r2)
97
+ # todo: make generic exception
98
+ rescue RestClient::Exception => ex
99
+ raise RestClientExceptionWrapper.new(ex)
100
+ end
101
+ end
102
+ response
103
+ end
104
+
105
+ def post(url, req_hash={})
106
+ if Rest.gem == :typhoeus
107
+ # todo: should change this timeout to longer if it's for posting file
108
+ req_hash[:connect_timeout] = 5000
109
+ req_hash[:timeout] ||= 10000
110
+ # puts "REQ_HASH=" + req_hash.inspect
111
+ response = Typhoeus::Request.post(url, req_hash)
112
+ #p response
113
+ if response.timed_out?
114
+ raise TyphoeusTimeoutError.new(response)
115
+ end
116
+ else
117
+ begin
118
+ headers = req_hash[:headers] || {}
119
+ r2 = RestClient.post(url, req_hash[:body], headers)
120
+ response = RestClientResponseWrapper.new(r2)
121
+ # todo: make generic exception
122
+ rescue RestClient::Exception => ex
123
+ raise RestClientExceptionWrapper.new(ex)
124
+ end
125
+ end
126
+ response
127
+ end
128
+
129
+ end
130
+ end
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rest"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Travis Reeder"]
12
+ s.date = "2011-12-16"
13
+ s.description = "Rest client wrapper that chooses best installed client."
14
+ s.email = "travis@iron.io"
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "lib/rest.rb",
23
+ "lib/rest/client.rb",
24
+ "rest.gemspec",
25
+ "test/test_base.rb",
26
+ "test/test_rest.rb"
27
+ ]
28
+ s.homepage = "http://www.iron.io"
29
+ s.require_paths = ["lib"]
30
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9")
31
+ s.rubygems_version = "1.8.11"
32
+ s.summary = "Rest client wrapper that chooses best installed client."
33
+
34
+ if s.respond_to? :specification_version then
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
39
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
40
+ else
41
+ s.add_dependency(%q<rest-client>, [">= 0"])
42
+ s.add_dependency(%q<rest-client>, [">= 0"])
43
+ end
44
+ else
45
+ s.add_dependency(%q<rest-client>, [">= 0"])
46
+ s.add_dependency(%q<rest-client>, [">= 0"])
47
+ end
48
+ end
49
+
@@ -0,0 +1,20 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ begin
5
+ require File.join(File.dirname(__FILE__), '../lib/rest')
6
+ rescue Exception => ex
7
+ puts "Could NOT load gem: " + ex.message
8
+ raise ex
9
+ end
10
+
11
+
12
+ class TestBase < Test::Unit::TestCase
13
+ def setup
14
+ puts 'setup'
15
+ #@config = YAML::load_file(File.expand_path(File.join("~", "Dropbox", "configs", "rest", "test", "config.yml")))
16
+ @rest = Rest::Client.new
17
+ #@client.logger.level = Logger::DEBUG
18
+
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ # Put config.yml file in ~/Dropbox/configs/ironmq_gem/test/config.yml
2
+
3
+ gem 'test-unit'
4
+ require 'test/unit'
5
+ require 'yaml'
6
+ require_relative 'test_base'
7
+
8
+ class TestTests < TestBase
9
+ def setup
10
+ super
11
+
12
+
13
+ end
14
+
15
+ def test_basics
16
+ response = @rest.get("http://www.github.com")
17
+ p response.body
18
+
19
+ end
20
+
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Reeder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &19547060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19547060
25
+ - !ruby/object:Gem::Dependency
26
+ name: rest-client
27
+ requirement: &19546560 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19546560
36
+ description: Rest client wrapper that chooses best installed client.
37
+ email: travis@iron.io
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - README.markdown
42
+ files:
43
+ - README.markdown
44
+ - Rakefile
45
+ - VERSION.yml
46
+ - lib/rest.rb
47
+ - lib/rest/client.rb
48
+ - rest.gemspec
49
+ - test/test_base.rb
50
+ - test/test_rest.rb
51
+ homepage: http://www.iron.io
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '1.9'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.11
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Rest client wrapper that chooses best installed client.
75
+ test_files: []