jnunemaker-httparty 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/License.txt +20 -0
- data/Manifest.txt +31 -0
- data/PostInstall.txt +1 -0
- data/README.txt +51 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/examples/aaws.rb +30 -0
- data/examples/delicious.rb +38 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/httparty.gemspec +33 -0
- data/lib/httparty.rb +148 -0
- data/lib/httparty/core_ext.rb +2 -0
- data/lib/httparty/core_ext/hash.rb +21 -0
- data/lib/httparty/version.rb +9 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +82 -0
- data/setup.rb +1585 -0
- data/spec/hash_spec.rb +11 -0
- data/spec/httparty_spec.rb +145 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/website.rake +17 -0
- data/website/css/common.css +47 -0
- data/website/index.html +77 -0
- metadata +97 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 John Nunemaker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
config/hoe.rb
|
8
|
+
config/requirements.rb
|
9
|
+
examples/aaws.rb
|
10
|
+
examples/delicious.rb
|
11
|
+
examples/twitter.rb
|
12
|
+
examples/whoismyrep.rb
|
13
|
+
httparty.gemspec
|
14
|
+
lib/httparty.rb
|
15
|
+
lib/httparty/core_ext.rb
|
16
|
+
lib/httparty/core_ext/hash.rb
|
17
|
+
lib/httparty/version.rb
|
18
|
+
script/console
|
19
|
+
script/destroy
|
20
|
+
script/generate
|
21
|
+
script/txt2html
|
22
|
+
setup.rb
|
23
|
+
spec/hash_spec.rb
|
24
|
+
spec/httparty_spec.rb
|
25
|
+
spec/spec.opts
|
26
|
+
spec/spec_helper.rb
|
27
|
+
tasks/deployment.rake
|
28
|
+
tasks/environment.rake
|
29
|
+
tasks/website.rake
|
30
|
+
website/css/common.css
|
31
|
+
website/index.html
|
data/PostInstall.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
When you HTTParty, you must party hard!
|
data/README.txt
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= httparty
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Makes http fun again!
|
6
|
+
|
7
|
+
== FEATURES/PROBLEMS:
|
8
|
+
|
9
|
+
* Easy get, post, put, delete requests
|
10
|
+
* Basic http authentication
|
11
|
+
* Default request query string parameters (ie: for api keys that are needed on each request)
|
12
|
+
* Automatic parsing of JSON and XML into ruby hashes
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
The following is a simple example of wrapping Twitter's API for posting updates.
|
17
|
+
|
18
|
+
class Twitter
|
19
|
+
include HTTParty
|
20
|
+
base_uri 'twitter.com'
|
21
|
+
basic_auth 'username', 'password'
|
22
|
+
end
|
23
|
+
|
24
|
+
Twitter.post('/statuses/update.json', :query => {:status => "It's an HTTParty and everyone is invited!"})
|
25
|
+
|
26
|
+
That is really it! The object returned is a ruby hash that is decoded from Twitter's json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples).
|
27
|
+
|
28
|
+
That works and all but what if you don't want to embed your username and password in the class? Below is an example to fix that:
|
29
|
+
|
30
|
+
class Twitter
|
31
|
+
include HTTParty
|
32
|
+
base_uri 'twitter.com'
|
33
|
+
|
34
|
+
def initialize(user, pass)
|
35
|
+
self.class.basic_auth user, pass
|
36
|
+
end
|
37
|
+
|
38
|
+
def post(text)
|
39
|
+
self.class.post('/statuses/update.json', :query => {:status => text})
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Twitter.new('username', 'password').post("It's an HTTParty and everyone is invited!")
|
44
|
+
|
45
|
+
== REQUIREMENTS:
|
46
|
+
|
47
|
+
* Active Support >= 2.1
|
48
|
+
|
49
|
+
== INSTALL:
|
50
|
+
|
51
|
+
* sudo gem install httparty
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'httparty/version'
|
2
|
+
|
3
|
+
AUTHOR = 'John Nunemaker' # can also be an array of Authors
|
4
|
+
EMAIL = "nunemaker@gmail.com"
|
5
|
+
DESCRIPTION = "Makes http fun! Also, makes consuming restful web services dead easy."
|
6
|
+
GEM_NAME = 'httparty' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'httparty' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
['activesupport', '>= 2.1']
|
12
|
+
] # An array of rubygem dependencies [name, version]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "unknown"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = HTTParty::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'httparty documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
67
|
+
end
|
68
|
+
|
69
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
70
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
71
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
72
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
73
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
data/examples/aaws.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.aaws')))
|
5
|
+
|
6
|
+
module AAWS
|
7
|
+
class Book
|
8
|
+
include HTTParty
|
9
|
+
base_uri 'http://ecs.amazonaws.com'
|
10
|
+
default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
|
11
|
+
format :xml
|
12
|
+
|
13
|
+
def initialize(key)
|
14
|
+
self.class.default_params :AWSAccessKeyId => key
|
15
|
+
end
|
16
|
+
|
17
|
+
def search(options={})
|
18
|
+
raise ArgumentError, 'You must search for something' if options[:query].blank?
|
19
|
+
|
20
|
+
# amazon uses nasty camelized query params
|
21
|
+
options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
|
22
|
+
|
23
|
+
# make a request and return the items (NOTE: this doesn't handle errors at this point)
|
24
|
+
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
aaws = AAWS::Book.new(config[:access_key])
|
30
|
+
pp aaws.search(:query => {:title => 'Ruby On Rails'})
|
@@ -0,0 +1,38 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
|
5
|
+
|
6
|
+
class Delicious
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'https://api.del.icio.us/v1'
|
9
|
+
format :xml
|
10
|
+
|
11
|
+
def initialize(u, p)
|
12
|
+
@auth = {:username => u, :password => p}
|
13
|
+
end
|
14
|
+
|
15
|
+
# query params that filter the posts are:
|
16
|
+
# tag (optional). Filter by this tag.
|
17
|
+
# dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
|
18
|
+
# url (optional). Filter by this url.
|
19
|
+
# ie: posts(:query => {:tag => 'ruby'})
|
20
|
+
def posts(options={})
|
21
|
+
options.merge!({:basic_auth => @auth})
|
22
|
+
# get posts and convert to structs so we can do .key instead of ['key'] with results
|
23
|
+
self.class.get('/posts/get', options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# query params that filter the posts are:
|
27
|
+
# tag (optional). Filter by this tag.
|
28
|
+
# count (optional). Number of items to retrieve (Default:15, Maximum:100).
|
29
|
+
def recent(options={})
|
30
|
+
options.merge!({:basic_auth => @auth})
|
31
|
+
self.class.get('/posts/recent', options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
delicious = Delicious.new(config['username'], config['password'])
|
36
|
+
pp delicious.posts(:query => {:tag => 'ruby'})
|
37
|
+
pp delicious.recent
|
38
|
+
|
data/examples/twitter.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
|
5
|
+
|
6
|
+
class Twitter
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'twitter.com'
|
9
|
+
|
10
|
+
def initialize(u, p)
|
11
|
+
@auth = {:username => u, :password => p}
|
12
|
+
end
|
13
|
+
|
14
|
+
# which can be :friends, :user or :public
|
15
|
+
# options[:query] can be things like since, since_id, count, etc.
|
16
|
+
def timeline(which=:friends, options={})
|
17
|
+
options.merge!({:basic_auth => @auth})
|
18
|
+
self.class.get("/statuses/#{which}_timeline.json", options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def post(text)
|
22
|
+
options = { :query => {:status => text}, :basic_auth => @auth }
|
23
|
+
self.class.post('/statuses/update.json', options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
twitter = Twitter.new(config['email'], config['password'])
|
28
|
+
pp twitter.timeline
|
29
|
+
# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
|
30
|
+
# pp twitter.timeline(:friends, :query => 'since_id=868482746')
|
31
|
+
# pp twitter.post('this is a test')
|
@@ -0,0 +1,10 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Rep
|
6
|
+
include HTTParty
|
7
|
+
format :xml
|
8
|
+
end
|
9
|
+
|
10
|
+
puts Rep.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544').inspect
|
data/httparty.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{httparty}
|
3
|
+
s.version = "0.1.1"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["John Nunemaker"]
|
7
|
+
s.date = %q{2008-07-31}
|
8
|
+
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
9
|
+
s.email = ["nunemaker@gmail.com"]
|
10
|
+
s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt"]
|
11
|
+
s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "config/hoe.rb", "config/requirements.rb", "examples/aaws.rb", "examples/delicious.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "httparty.gemspec", "lib/httparty.rb", "lib/httparty/core_ext.rb", "lib/httparty/core_ext/hash.rb", "lib/httparty/version.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/hash_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/website.rake", "website/css/common.css", "website/index.html"]
|
12
|
+
s.has_rdoc = true
|
13
|
+
s.homepage = %q{http://httparty.rubyforge.org}
|
14
|
+
s.post_install_message = %q{When you HTTParty, you must party hard!}
|
15
|
+
s.rdoc_options = ["--main", "README.txt"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{httparty}
|
18
|
+
s.rubygems_version = %q{1.2.0}
|
19
|
+
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 2
|
24
|
+
|
25
|
+
if current_version >= 3 then
|
26
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.1"])
|
27
|
+
else
|
28
|
+
s.add_dependency(%q<activesupport>, [">= 2.1"])
|
29
|
+
end
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<activesupport>, [">= 2.1"])
|
32
|
+
end
|
33
|
+
end
|
data/lib/httparty.rb
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'active_support'
|
7
|
+
|
8
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
9
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
10
|
+
|
11
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), 'httparty'))
|
12
|
+
require dir + '/core_ext'
|
13
|
+
|
14
|
+
module HTTParty
|
15
|
+
def self.included(base)
|
16
|
+
base.extend ClassMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
class UnsupportedFormat < StandardError; end
|
20
|
+
|
21
|
+
AllowedFormats = %w[xml json]
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def base_uri(base_uri=nil)
|
25
|
+
return @base_uri unless base_uri
|
26
|
+
# don't want this to ever end with /
|
27
|
+
base_uri = base_uri.ends_with?('/') ? base_uri.chop : base_uri
|
28
|
+
@base_uri = normalize_base_uri(base_uri)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Warning: This is not thread safe most likely and
|
32
|
+
# only works if you use one set of credentials. I
|
33
|
+
# leave it because it is convenient on some occasions.
|
34
|
+
def basic_auth(u, p)
|
35
|
+
@auth = {:username => u, :password => p}
|
36
|
+
end
|
37
|
+
|
38
|
+
# Updates the default query string parameters
|
39
|
+
# that should be appended to each request.
|
40
|
+
def default_params(h={})
|
41
|
+
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
|
42
|
+
@default_params ||= {}
|
43
|
+
return @default_params if h.blank?
|
44
|
+
@default_params.merge!(h)
|
45
|
+
end
|
46
|
+
|
47
|
+
def headers(h={})
|
48
|
+
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
|
49
|
+
@headers ||= {}
|
50
|
+
return @headers if h.blank?
|
51
|
+
@headers.merge!(h)
|
52
|
+
end
|
53
|
+
|
54
|
+
def format(f)
|
55
|
+
f = f.to_s
|
56
|
+
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f)
|
57
|
+
@format = f
|
58
|
+
end
|
59
|
+
|
60
|
+
# TODO: spec out this
|
61
|
+
def get(path, options={})
|
62
|
+
send_request 'get', path, options
|
63
|
+
end
|
64
|
+
|
65
|
+
# TODO: spec out this
|
66
|
+
def post(path, options={})
|
67
|
+
send_request 'post', path, options
|
68
|
+
end
|
69
|
+
|
70
|
+
# TODO: spec out this
|
71
|
+
def put(path, options={})
|
72
|
+
send_request 'put', path, options
|
73
|
+
end
|
74
|
+
|
75
|
+
# TODO: spec out this
|
76
|
+
def delete(path, options={})
|
77
|
+
send_request 'delete', path, options
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
def http(uri)
|
82
|
+
if @http.blank?
|
83
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
84
|
+
@http.use_ssl = (uri.port == 443)
|
85
|
+
# so we can avoid ssl warnings
|
86
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
87
|
+
end
|
88
|
+
@http
|
89
|
+
end
|
90
|
+
|
91
|
+
# FIXME: this method is doing way to much and needs to be split up
|
92
|
+
# options can be any or all of:
|
93
|
+
# query => hash of keys/values or a query string (foo=bar&baz=poo)
|
94
|
+
# body => hash of keys/values or a query string (foo=bar&baz=poo)
|
95
|
+
# headers => hash of headers to send request with
|
96
|
+
# basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
|
97
|
+
def send_request(method, path, options={})
|
98
|
+
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
|
99
|
+
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
|
100
|
+
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
|
101
|
+
# we always want path that begins with /
|
102
|
+
path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
|
103
|
+
@format ||= format_from_path(path)
|
104
|
+
uri = URI.parse("#{base_uri}#{path}")
|
105
|
+
existing_query = uri.query ? "#{uri.query}&" : ''
|
106
|
+
uri.query = if options[:query].blank?
|
107
|
+
existing_query
|
108
|
+
else
|
109
|
+
existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
|
110
|
+
end
|
111
|
+
klass = Net::HTTP.const_get method.to_s.downcase.capitalize
|
112
|
+
request = klass.new(uri.request_uri)
|
113
|
+
request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
|
114
|
+
basic_auth = options.delete(:basic_auth) || @auth
|
115
|
+
request.initialize_http_header headers.merge(options[:headers] || {})
|
116
|
+
# note to self: self, do not put basic auth above headers because it removes basic auth
|
117
|
+
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
|
118
|
+
response = http(uri).request(request)
|
119
|
+
parse_response(response.body)
|
120
|
+
end
|
121
|
+
|
122
|
+
def parse_response(body)
|
123
|
+
case @format
|
124
|
+
when 'xml'
|
125
|
+
Hash.from_xml(body)
|
126
|
+
when 'json'
|
127
|
+
ActiveSupport::JSON.decode(body)
|
128
|
+
else
|
129
|
+
# just return the response if no format
|
130
|
+
body
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Makes it so uri is sure to parse stuff like google.com with the http
|
135
|
+
def normalize_base_uri(str)
|
136
|
+
str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
|
137
|
+
end
|
138
|
+
|
139
|
+
# Returns a format that we can handle from the path if possible.
|
140
|
+
# Just does simple pattern matching on file extention:
|
141
|
+
# /foobar.xml => 'xml'
|
142
|
+
# /foobar.json => 'json'
|
143
|
+
def format_from_path(path)
|
144
|
+
ext = File.extname(path)[1..-1]
|
145
|
+
!ext.blank? && AllowedFormats.include?(ext) ? ext : nil
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|