leh-httparty 0.3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History +108 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +55 -0
- data/README +35 -0
- data/Rakefile +47 -0
- data/bin/httparty +98 -0
- data/cucumber.yml +1 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +11 -0
- data/examples/delicious.rb +37 -0
- data/examples/google.rb +16 -0
- data/examples/rubyurl.rb +14 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/handles_multiple_formats.feature +34 -0
- data/features/steps/env.rb +15 -0
- data/features/steps/httparty_response_steps.rb +26 -0
- data/features/steps/httparty_steps.rb +15 -0
- data/features/steps/mongrel_helper.rb +55 -0
- data/features/steps/remote_service_steps.rb +47 -0
- data/features/supports_redirection.feature +22 -0
- data/httparty.gemspec +37 -0
- data/lib/core_extensions.rb +230 -0
- data/lib/httparty.rb +201 -0
- data/lib/httparty/cookie_hash.rb +9 -0
- data/lib/httparty/exceptions.rb +7 -0
- data/lib/httparty/module_inheritable_attributes.rb +25 -0
- data/lib/httparty/parsers.rb +4 -0
- data/lib/httparty/parsers/json.rb +74 -0
- data/lib/httparty/parsers/xml.rb +209 -0
- data/lib/httparty/request.rb +139 -0
- data/lib/httparty/response.rb +17 -0
- data/lib/httparty/version.rb +3 -0
- data/setup.rb +1585 -0
- data/spec/fixtures/delicious.xml +23 -0
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/google.html +3 -0
- data/spec/fixtures/twitter.json +1 -0
- data/spec/fixtures/twitter.xml +403 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/hash_spec.rb +49 -0
- data/spec/httparty/cookie_hash_spec.rb +38 -0
- data/spec/httparty/parsers/json_spec.rb +42 -0
- data/spec/httparty/parsers/xml_spec.rb +445 -0
- data/spec/httparty/request_spec.rb +196 -0
- data/spec/httparty/response_spec.rb +53 -0
- data/spec/httparty_spec.rb +259 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/string_spec.rb +27 -0
- data/website/css/common.css +47 -0
- data/website/index.html +74 -0
- metadata +133 -0
data/examples/google.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Google
|
6
|
+
include HTTParty
|
7
|
+
format :html
|
8
|
+
end
|
9
|
+
|
10
|
+
# google.com redirects to www.google.com so this is live test for redirection
|
11
|
+
pp Google.get('http://google.com')
|
12
|
+
|
13
|
+
puts '', '*'*70, ''
|
14
|
+
|
15
|
+
# check that ssl is requesting right
|
16
|
+
pp Google.get('https://www.google.com')
|
data/examples/rubyurl.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Rubyurl
|
6
|
+
include HTTParty
|
7
|
+
base_uri 'rubyurl.com'
|
8
|
+
|
9
|
+
def self.shorten( website_url )
|
10
|
+
post( '/api/links.json', :query => { :link => { :website_url => website_url } } )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
pp Rubyurl.shorten( 'http://istwitterdown.com/')
|
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 of 0.2.0')
|
@@ -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
|
+
end
|
8
|
+
|
9
|
+
pp Rep.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544')
|
10
|
+
pp Rep.get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => 46544})
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Feature: Basic Authentication
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to use a service that requires Basic Authentication
|
5
|
+
Because that is not an uncommon requirement
|
6
|
+
|
7
|
+
Scenario: Passing no credentials to a page requiring Basic Authentication
|
8
|
+
Given a restricted page at '/protected.html'
|
9
|
+
When I call HTTParty#get with '/protected.html'
|
10
|
+
Then it should return a response with a 401 response code
|
11
|
+
|
12
|
+
Scenario: Passing proper credentials to a page requiring Basic Authentication
|
13
|
+
Given a remote service that returns 'Authenticated Page'
|
14
|
+
And that service is accessed at the path '/protected.html'
|
15
|
+
And that service is protected by Basic Authentication
|
16
|
+
And that service requires the username 'jcash' with the password 'maninblack'
|
17
|
+
When I call HTTParty#get with '/protected.html' and a basic_auth hash:
|
18
|
+
| username | password |
|
19
|
+
| jcash | maninblack |
|
20
|
+
Then the return value should match 'Authenticated Page'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: Deals with HTTP error codes
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be informed of non-successful responses
|
5
|
+
Because sometimes thing explode
|
6
|
+
And I should probably know what happened
|
7
|
+
|
8
|
+
Scenario: A response of '404 - Not Found'
|
9
|
+
Given a remote service that returns a 404 status code
|
10
|
+
And that service is accessed at the path '/service.html'
|
11
|
+
When I call HTTParty#get with '/service.html'
|
12
|
+
Then it should return a response with a 404 response code
|
13
|
+
|
14
|
+
Scenario: A response of '500 - Internal Server Error'
|
15
|
+
Given a remote service that returns a 500 status code
|
16
|
+
And that service is accessed at the path '/service.html'
|
17
|
+
When I call HTTParty#get with '/service.html'
|
18
|
+
Then it should return a response with a 500 response code
|
19
|
+
|
20
|
+
Scenario: A non-successful response where I need the body
|
21
|
+
Given a remote service that returns a 400 status code
|
22
|
+
And the response from the service has a body of 'Bad response'
|
23
|
+
And that service is accessed at the path '/service.html'
|
24
|
+
When I call HTTParty#get with '/service.html'
|
25
|
+
Then it should return a response with a 400 response code
|
26
|
+
And the return value should match 'Bad response'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Feature: Handles Multiple Formats
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to consume remote services of many different formats
|
5
|
+
And I want those formats to be automatically detected and handled
|
6
|
+
Because web services take many forms
|
7
|
+
And I don't want to have to do any extra work
|
8
|
+
|
9
|
+
Scenario: An HTML service
|
10
|
+
Given a remote service that returns '<h1>Some HTML</h1>'
|
11
|
+
And that service is accessed at the path '/service.html'
|
12
|
+
And the response from the service has a Content-Type of 'text/html'
|
13
|
+
When I call HTTParty#get with '/service.html'
|
14
|
+
Then it should return a String
|
15
|
+
And the return value should match '<h1>Some HTML</h1>'
|
16
|
+
|
17
|
+
Scenario: A JSON service
|
18
|
+
Given a remote service that returns '{ "jennings": "waylon", "cash": "johnny" }'
|
19
|
+
And that service is accessed at the path '/service.json'
|
20
|
+
And the response from the service has a Content-Type of 'application/json'
|
21
|
+
When I call HTTParty#get with '/service.json'
|
22
|
+
Then it should return a Hash equaling:
|
23
|
+
| key | value |
|
24
|
+
| jennings | waylon |
|
25
|
+
| cash | johnny |
|
26
|
+
|
27
|
+
Scenario: An XML Service
|
28
|
+
Given a remote service that returns '<singer>waylon jennings</singer>'
|
29
|
+
And that service is accessed at the path '/service.xml'
|
30
|
+
And the response from the service has a Content-Type of 'text/xml'
|
31
|
+
When I call HTTParty#get with '/service.xml'
|
32
|
+
Then it should return a Hash equaling:
|
33
|
+
| key | value |
|
34
|
+
| singer | waylon jennings |
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mongrel'
|
2
|
+
require 'activesupport'
|
3
|
+
require 'lib/httparty'
|
4
|
+
require 'spec/expectations'
|
5
|
+
|
6
|
+
Before do
|
7
|
+
port = ENV["HTTPARTY_PORT"] || 31981
|
8
|
+
@host_and_port = "0.0.0.0:#{port}"
|
9
|
+
@server = Mongrel::HttpServer.new("0.0.0.0", port)
|
10
|
+
@server.run
|
11
|
+
end
|
12
|
+
|
13
|
+
After do
|
14
|
+
@server.stop
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Then /it should return an? (\w+)$/ do |class_string|
|
2
|
+
@response_from_httparty.should be_an_instance_of(class_string.constantize)
|
3
|
+
end
|
4
|
+
|
5
|
+
Then /the return value should match '(.*)'/ do |expected_text|
|
6
|
+
@response_from_httparty.should eql(expected_text)
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /it should return a Hash equaling:/ do |hash_table|
|
10
|
+
@response_from_httparty.should be_an_instance_of(Hash)
|
11
|
+
@response_from_httparty.keys.length.should eql(hash_table.rows.length)
|
12
|
+
hash_table.hashes.each do |pair|
|
13
|
+
key, value = pair["key"], pair["value"]
|
14
|
+
@response_from_httparty.keys.should include(key)
|
15
|
+
@response_from_httparty[key].should eql(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Then /it should return a response with a (\d+) response code/ do |code|
|
20
|
+
@response_from_httparty.code.should eql(code)
|
21
|
+
end
|
22
|
+
|
23
|
+
Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
|
24
|
+
@exception_from_httparty.should_not be_nil
|
25
|
+
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep)
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
When /I call HTTParty#get with '(.*)'$/ do |url|
|
2
|
+
begin
|
3
|
+
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}")
|
4
|
+
rescue HTTParty::RedirectionTooDeep => e
|
5
|
+
@exception_from_httparty = e
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
When /I call HTTParty#get with '(.*)' and a basic_auth hash:/ do |url, auth_table|
|
10
|
+
h = auth_table.hashes.first
|
11
|
+
@response_from_httparty = HTTParty.get(
|
12
|
+
"http://#{@host_and_port}#{url}",
|
13
|
+
:basic_auth => { :username => h["username"], :password => h["password"] }
|
14
|
+
)
|
15
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
def basic_mongrel_handler
|
2
|
+
Class.new(Mongrel::HttpHandler) do
|
3
|
+
attr_writer :content_type, :response_body, :response_code
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@content_type = "text/html"
|
7
|
+
@response_body = ""
|
8
|
+
@response_code = 200
|
9
|
+
@custom_headers = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def process(request, response)
|
13
|
+
reply_with(response, @response_code, @response_body)
|
14
|
+
end
|
15
|
+
|
16
|
+
def reply_with(response, code, response_body)
|
17
|
+
response.start(code) do |head, body|
|
18
|
+
head["Content-Type"] = @content_type
|
19
|
+
@custom_headers.each { |k,v| head[k] = v }
|
20
|
+
body.write(response_body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_mongrel_handler
|
27
|
+
basic_mongrel_handler.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_basic_authentication_to(handler)
|
31
|
+
m = Module.new do
|
32
|
+
attr_writer :username, :password
|
33
|
+
|
34
|
+
def self.extended(base)
|
35
|
+
base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' }
|
36
|
+
base.class_eval { alias_method_chain :process, :basic_authentication }
|
37
|
+
end
|
38
|
+
|
39
|
+
def process_with_basic_authentication(request, response)
|
40
|
+
if authorized?(request) then process_without_basic_authentication(request, response)
|
41
|
+
else reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def authorized?(request)
|
46
|
+
request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
|
47
|
+
end
|
48
|
+
end
|
49
|
+
handler.extend(m)
|
50
|
+
end
|
51
|
+
|
52
|
+
def new_mongrel_redirector(target_url, relative_path = false)
|
53
|
+
target_url = "http://#{@host_and_port}#{target_url}" unless relative_path
|
54
|
+
Mongrel::RedirectHandler.new(target_url)
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
Given /a remote service that returns '(.*)'/ do |response_body|
|
2
|
+
@handler = new_mongrel_handler
|
3
|
+
Given "the response from the service has a body of '#{response_body}'"
|
4
|
+
end
|
5
|
+
|
6
|
+
Given /a remote service that returns a (\d+) status code/ do |code|
|
7
|
+
@handler = new_mongrel_handler
|
8
|
+
@handler.response_code = code
|
9
|
+
end
|
10
|
+
|
11
|
+
Given /that service is accessed at the path '(.*)'/ do |path|
|
12
|
+
@server.register(path, @handler)
|
13
|
+
end
|
14
|
+
|
15
|
+
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
|
16
|
+
@handler.content_type = content_type
|
17
|
+
end
|
18
|
+
|
19
|
+
Given /the response from the service has a body of '(.*)'/ do |response_body|
|
20
|
+
@handler.response_body = response_body
|
21
|
+
end
|
22
|
+
|
23
|
+
Given /the url '(.*)' redirects to '(.*)'/ do |redirection_url, target_url|
|
24
|
+
@server.register redirection_url, new_mongrel_redirector(target_url)
|
25
|
+
end
|
26
|
+
|
27
|
+
Given /that service is protected by Basic Authentication/ do
|
28
|
+
add_basic_authentication_to @handler
|
29
|
+
end
|
30
|
+
|
31
|
+
Given /that service requires the username '(.*)' with the password '(.*)'/ do |username, password|
|
32
|
+
@handler.username = username
|
33
|
+
@handler.password = password
|
34
|
+
end
|
35
|
+
|
36
|
+
Given /a restricted page at '(.*)'/ do |url|
|
37
|
+
Given "a remote service that returns 'A response I will never see'"
|
38
|
+
And "that service is accessed at the path '#{url}'"
|
39
|
+
And "that service is protected by Basic Authentication"
|
40
|
+
And "that service requires the username 'something' with the password 'secret'"
|
41
|
+
end
|
42
|
+
|
43
|
+
# This joins the server thread, and halts cucumber, so you can actually hit the
|
44
|
+
# server with a browser. Runs until you kill it with Ctrl-c
|
45
|
+
Given /I want to hit this in a browser/ do
|
46
|
+
@server.acceptor.join
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Feature: Supports Redirection
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to work with services that may redirect me
|
5
|
+
And I want it to follow a reasonable number of redirects
|
6
|
+
Because sometimes web services do that
|
7
|
+
|
8
|
+
Scenario: A service that redirects once
|
9
|
+
Given a remote service that returns 'Service Response'
|
10
|
+
And that service is accessed at the path '/service.html'
|
11
|
+
And the url '/redirector.html' redirects to '/service.html'
|
12
|
+
When I call HTTParty#get with '/redirector.html'
|
13
|
+
Then the return value should match 'Service Response'
|
14
|
+
|
15
|
+
# TODO: Look in to why this actually fails...
|
16
|
+
Scenario: A service that redirects to a relative URL
|
17
|
+
|
18
|
+
Scenario: A service that redirects infinitely
|
19
|
+
Given the url '/first.html' redirects to '/second.html'
|
20
|
+
And the url '/second.html' redirects to '/first.html'
|
21
|
+
When I call HTTParty#get with '/first.html'
|
22
|
+
Then it should raise an HTTParty::RedirectionTooDeep exception
|
data/httparty.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{httparty}
|
5
|
+
s.version = "0.3.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["John Nunemaker"]
|
9
|
+
s.date = %q{2009-02-10}
|
10
|
+
s.default_executable = %q{httparty}
|
11
|
+
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
12
|
+
s.email = %q{nunemaker@gmail.com}
|
13
|
+
s.executables = ["httparty"]
|
14
|
+
s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README"]
|
15
|
+
s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/parsers/json.rb", "lib/httparty/parsers/xml.rb", "lib/httparty/parsers.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/hash_spec.rb", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/parsers/json_spec.rb", "spec/httparty/parsers/xml_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty/response_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "website/css/common.css", "website/index.html"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://httparty.rubyforge.org}
|
18
|
+
s.post_install_message = %q{When you HTTParty, you must party hard!}
|
19
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Httparty", "--main", "README"]
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.rubyforge_project = %q{httparty}
|
22
|
+
s.rubygems_version = %q{1.3.1}
|
23
|
+
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
24
|
+
|
25
|
+
if s.respond_to? :specification_version then
|
26
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
+
s.specification_version = 2
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
# Copyright (c) 2008 Sam Smoot.
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
class Object #:nodoc:
|
25
|
+
# @return <TrueClass, FalseClass>
|
26
|
+
#
|
27
|
+
# @example [].blank? #=> true
|
28
|
+
# @example [1].blank? #=> false
|
29
|
+
# @example [nil].blank? #=> false
|
30
|
+
#
|
31
|
+
# Returns true if the object is nil or empty (if applicable)
|
32
|
+
def blank?
|
33
|
+
nil? || (respond_to?(:empty?) && empty?)
|
34
|
+
end unless method_defined?(:blank?)
|
35
|
+
end # class Object
|
36
|
+
|
37
|
+
class Numeric #:nodoc:
|
38
|
+
# @return <TrueClass, FalseClass>
|
39
|
+
#
|
40
|
+
# Numerics can't be blank
|
41
|
+
def blank?
|
42
|
+
false
|
43
|
+
end unless method_defined?(:blank?)
|
44
|
+
end # class Numeric
|
45
|
+
|
46
|
+
class NilClass #:nodoc:
|
47
|
+
# @return <TrueClass, FalseClass>
|
48
|
+
#
|
49
|
+
# Nils are always blank
|
50
|
+
def blank?
|
51
|
+
true
|
52
|
+
end unless method_defined?(:blank?)
|
53
|
+
end # class NilClass
|
54
|
+
|
55
|
+
class TrueClass #:nodoc:
|
56
|
+
# @return <TrueClass, FalseClass>
|
57
|
+
#
|
58
|
+
# True is not blank.
|
59
|
+
def blank?
|
60
|
+
false
|
61
|
+
end unless method_defined?(:blank?)
|
62
|
+
end # class TrueClass
|
63
|
+
|
64
|
+
class FalseClass #:nodoc:
|
65
|
+
# False is always blank.
|
66
|
+
def blank?
|
67
|
+
true
|
68
|
+
end unless method_defined?(:blank?)
|
69
|
+
end # class FalseClass
|
70
|
+
|
71
|
+
class String #:nodoc:
|
72
|
+
# @example "".blank? #=> true
|
73
|
+
# @example " ".blank? #=> true
|
74
|
+
# @example " hey ho ".blank? #=> false
|
75
|
+
#
|
76
|
+
# @return <TrueClass, FalseClass>
|
77
|
+
#
|
78
|
+
# Strips out whitespace then tests if the string is empty.
|
79
|
+
def blank?
|
80
|
+
strip.empty?
|
81
|
+
end unless method_defined?(:blank?)
|
82
|
+
|
83
|
+
def snake_case
|
84
|
+
return self.downcase if self =~ /^[A-Z]+$/
|
85
|
+
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
86
|
+
return $+.downcase
|
87
|
+
end unless method_defined?(:snake_case)
|
88
|
+
end # class String
|
89
|
+
|
90
|
+
class Hash #:nodoc:
|
91
|
+
# @return <String> This hash as a query string
|
92
|
+
#
|
93
|
+
# @example
|
94
|
+
# { :name => "Bob",
|
95
|
+
# :address => {
|
96
|
+
# :street => '111 Ruby Ave.',
|
97
|
+
# :city => 'Ruby Central',
|
98
|
+
# :phones => ['111-111-1111', '222-222-2222']
|
99
|
+
# }
|
100
|
+
# }.to_params
|
101
|
+
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
|
102
|
+
def to_params
|
103
|
+
params = self.map { |k,v| normalize_param(k,v) }.join
|
104
|
+
params.chop! # trailing &
|
105
|
+
params
|
106
|
+
end
|
107
|
+
|
108
|
+
# @param key<Object> The key for the param.
|
109
|
+
# @param value<Object> The value for the param.
|
110
|
+
#
|
111
|
+
# @return <String> This key value pair as a param
|
112
|
+
#
|
113
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
114
|
+
def normalize_param(key, value)
|
115
|
+
param = ''
|
116
|
+
stack = []
|
117
|
+
|
118
|
+
if value.is_a?(Array)
|
119
|
+
param << value.map { |element| normalize_param("#{key}[]", element) }.join
|
120
|
+
elsif value.is_a?(Hash)
|
121
|
+
stack << [key,value]
|
122
|
+
else
|
123
|
+
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
124
|
+
end
|
125
|
+
|
126
|
+
stack.each do |parent, hash|
|
127
|
+
hash.each do |key, value|
|
128
|
+
if value.is_a?(Hash)
|
129
|
+
stack << ["#{parent}[#{key}]", value]
|
130
|
+
else
|
131
|
+
param << normalize_param("#{parent}[#{key}]", value)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
param
|
137
|
+
end
|
138
|
+
|
139
|
+
# @return <String> The hash as attributes for an XML tag.
|
140
|
+
#
|
141
|
+
# @example
|
142
|
+
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
|
143
|
+
# #=> 'one="1" two="TWO"'
|
144
|
+
def to_xml_attributes
|
145
|
+
map do |k,v|
|
146
|
+
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
|
147
|
+
end.join(' ')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class BlankSlate #:nodoc:
|
152
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
|
153
|
+
end
|
154
|
+
|
155
|
+
# 1.8.6 has mistyping of transitive in if statement
|
156
|
+
require "rexml/document"
|
157
|
+
|
158
|
+
if (RUBY_VERSION == "1.8.6" || RUBY_VERSION == "1.8.7") && defined? REXML::Formatters::Transitive
|
159
|
+
class REXML::Document
|
160
|
+
remove_method :write rescue nil
|
161
|
+
def write( output=$stdout, indent=-1, trans=false, ie_hack=false )
|
162
|
+
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
163
|
+
output = Output.new( output, xml_decl.encoding )
|
164
|
+
end
|
165
|
+
formatter = if indent > -1
|
166
|
+
#if trans
|
167
|
+
REXML::Formatters::Transitive.new( indent )
|
168
|
+
#else
|
169
|
+
# REXML::Formatters::Pretty.new( indent, ie_hack )
|
170
|
+
#end
|
171
|
+
else
|
172
|
+
REXML::Formatters::Default.new( ie_hack )
|
173
|
+
end
|
174
|
+
formatter.write( self, output )
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
class REXML::Formatters::Transitive
|
179
|
+
remove_method :write_element rescue nil
|
180
|
+
def write_element( node, output )
|
181
|
+
output << "<#{node.expanded_name}"
|
182
|
+
|
183
|
+
node.attributes.each_attribute do |attr|
|
184
|
+
output << " "
|
185
|
+
attr.write( output )
|
186
|
+
end unless node.attributes.empty?
|
187
|
+
|
188
|
+
if node.children.empty?
|
189
|
+
output << "/>"
|
190
|
+
else
|
191
|
+
output << ">"
|
192
|
+
# If compact and all children are text, and if the formatted output
|
193
|
+
# is less than the specified width, then try to print everything on
|
194
|
+
# one line
|
195
|
+
skip = false
|
196
|
+
@level += @indentation
|
197
|
+
node.children.each { |child|
|
198
|
+
write( child, output )
|
199
|
+
}
|
200
|
+
@level -= @indentation
|
201
|
+
output << "</#{node.expanded_name}>"
|
202
|
+
end
|
203
|
+
output << "\n"
|
204
|
+
output << ' '*@level
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
#module REXML #:nodoc:
|
213
|
+
# class Document < Element #:nodoc:
|
214
|
+
# def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
|
215
|
+
# if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
216
|
+
# output = Output.new( output, xml_decl.encoding )
|
217
|
+
# end
|
218
|
+
# formatter = if indent > -1
|
219
|
+
# if transitive
|
220
|
+
# REXML::Formatters::Transitive.new( indent, ie_hack )
|
221
|
+
# else
|
222
|
+
# REXML::Formatters::Pretty.new( indent, ie_hack )
|
223
|
+
# end
|
224
|
+
# else
|
225
|
+
# REXML::Formatters::Default.new( ie_hack )
|
226
|
+
# end
|
227
|
+
# formatter.write( self, output )
|
228
|
+
# end
|
229
|
+
# end
|
230
|
+
#end
|