ddollar-httparty 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +6 -0
  2. data/History +141 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest +47 -0
  5. data/README.rdoc +54 -0
  6. data/Rakefile +71 -0
  7. data/VERSION +1 -0
  8. data/bin/httparty +98 -0
  9. data/cucumber.yml +1 -0
  10. data/examples/aaws.rb +32 -0
  11. data/examples/basic.rb +11 -0
  12. data/examples/delicious.rb +37 -0
  13. data/examples/google.rb +16 -0
  14. data/examples/rubyurl.rb +14 -0
  15. data/examples/twitter.rb +31 -0
  16. data/examples/whoismyrep.rb +10 -0
  17. data/features/basic_authentication.feature +20 -0
  18. data/features/command_line.feature +7 -0
  19. data/features/deals_with_http_error_codes.feature +26 -0
  20. data/features/handles_multiple_formats.feature +34 -0
  21. data/features/steps/env.rb +16 -0
  22. data/features/steps/httparty_response_steps.rb +26 -0
  23. data/features/steps/httparty_steps.rb +19 -0
  24. data/features/steps/mongrel_helper.rb +56 -0
  25. data/features/steps/remote_service_steps.rb +52 -0
  26. data/features/supports_redirection.feature +22 -0
  27. data/features/supports_timeout_option.feature +12 -0
  28. data/httparty.gemspec +110 -0
  29. data/lib/httparty.rb +216 -0
  30. data/lib/httparty/cookie_hash.rb +22 -0
  31. data/lib/httparty/core_extensions.rb +25 -0
  32. data/lib/httparty/exceptions.rb +7 -0
  33. data/lib/httparty/module_inheritable_attributes.rb +25 -0
  34. data/lib/httparty/parsers.rb +31 -0
  35. data/lib/httparty/request.rb +175 -0
  36. data/lib/httparty/response.rb +18 -0
  37. data/lib/httparty/version.rb +3 -0
  38. data/spec/fixtures/delicious.xml +23 -0
  39. data/spec/fixtures/empty.xml +0 -0
  40. data/spec/fixtures/google.html +3 -0
  41. data/spec/fixtures/twitter.json +1 -0
  42. data/spec/fixtures/twitter.xml +403 -0
  43. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  44. data/spec/httparty/cookie_hash_spec.rb +71 -0
  45. data/spec/httparty/request_spec.rb +244 -0
  46. data/spec/httparty/response_spec.rb +62 -0
  47. data/spec/httparty_spec.rb +328 -0
  48. data/spec/spec.opts +2 -0
  49. data/spec/spec_helper.rb +24 -0
  50. data/website/css/common.css +47 -0
  51. data/website/index.html +73 -0
  52. metadata +135 -0
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: features
data/examples/aaws.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'activesupport'
3
+
4
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require File.join(dir, 'httparty')
6
+ require 'pp'
7
+ config = YAML::load(File.read(File.join(ENV['HOME'], '.aaws')))
8
+
9
+ module AAWS
10
+ class Book
11
+ include HTTParty
12
+ base_uri 'http://ecs.amazonaws.com'
13
+ default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
14
+
15
+ def initialize(key)
16
+ self.class.default_params :AWSAccessKeyId => key
17
+ end
18
+
19
+ def search(options={})
20
+ raise ArgumentError, 'You must search for something' if options[:query].blank?
21
+
22
+ # amazon uses nasty camelized query params
23
+ options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
24
+
25
+ # make a request and return the items (NOTE: this doesn't handle errors at this point)
26
+ self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
27
+ end
28
+ end
29
+ end
30
+
31
+ aaws = AAWS::Book.new(config[:access_key])
32
+ pp aaws.search(:query => {:title => 'Ruby On Rails'})
data/examples/basic.rb ADDED
@@ -0,0 +1,11 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
5
+ # You can also use post, put, delete in the same fashion
6
+ response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
7
+ puts response.body, response.code, response.message, response.headers.inspect
8
+
9
+ response.each do |item|
10
+ puts item['user']['screen_name']
11
+ end
@@ -0,0 +1,37 @@
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
+
10
+ def initialize(u, p)
11
+ @auth = {:username => u, :password => p}
12
+ end
13
+
14
+ # query params that filter the posts are:
15
+ # tag (optional). Filter by this tag.
16
+ # dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
17
+ # url (optional). Filter by this url.
18
+ # ie: posts(:query => {:tag => 'ruby'})
19
+ def posts(options={})
20
+ options.merge!({:basic_auth => @auth})
21
+ self.class.get('/posts/get', options)
22
+ end
23
+
24
+ # query params that filter the posts are:
25
+ # tag (optional). Filter by this tag.
26
+ # count (optional). Number of items to retrieve (Default:15, Maximum:100).
27
+ def recent(options={})
28
+ options.merge!({:basic_auth => @auth})
29
+ self.class.get('/posts/recent', options)
30
+ end
31
+ end
32
+
33
+ delicious = Delicious.new(config['username'], config['password'])
34
+ pp delicious.posts(:query => {:tag => 'ruby'})
35
+ pp delicious.recent
36
+
37
+ delicious.recent['posts']['post'].each { |post| puts post['href'] }
@@ -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')
@@ -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/')
@@ -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,7 @@
1
+ Feature: Command Line
2
+
3
+ As a developer
4
+ I want to be able to harness the power of HTTParty from the command line
5
+ Because that would make quick testing and debugging easy
6
+ And 'easy' is my middle name
7
+ And I'm kidding it's actually 'Danger'!
@@ -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,16 @@
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
+ @request_options = {}
12
+ end
13
+
14
+ After do
15
+ @server.stop
16
+ 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.to_i)
21
+ end
22
+
23
+ Then /it should raise (?:an|a) ([\w:]+) exception/ do |exception|
24
+ @exception_from_httparty.should_not be_nil
25
+ @exception_from_httparty.class.name.should eql(exception)
26
+ end
@@ -0,0 +1,19 @@
1
+ When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
2
+ @request_options[:timeout] = timeout.to_i
3
+ end
4
+
5
+ When /I call HTTParty#get with '(.*)'$/ do |url|
6
+ begin
7
+ @response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
8
+ rescue HTTParty::RedirectionTooDeep, Timeout::Error => e
9
+ @exception_from_httparty = e
10
+ end
11
+ end
12
+
13
+ When /I call HTTParty#get with '(.*)' and a basic_auth hash:/ do |url, auth_table|
14
+ h = auth_table.hashes.first
15
+ @response_from_httparty = HTTParty.get(
16
+ "http://#{@host_and_port}#{url}",
17
+ :basic_auth => { :username => h["username"], :password => h["password"] }
18
+ )
19
+ end
@@ -0,0 +1,56 @@
1
+ def basic_mongrel_handler
2
+ Class.new(Mongrel::HttpHandler) do
3
+ attr_writer :content_type, :response_body, :response_code, :preprocessor
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
+ instance_eval &@preprocessor if @preprocessor
14
+ reply_with(response, @response_code, @response_body)
15
+ end
16
+
17
+ def reply_with(response, code, response_body)
18
+ response.start(code) do |head, body|
19
+ head["Content-Type"] = @content_type
20
+ @custom_headers.each { |k,v| head[k] = v }
21
+ body.write(response_body)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def new_mongrel_handler
28
+ basic_mongrel_handler.new
29
+ end
30
+
31
+ def add_basic_authentication_to(handler)
32
+ m = Module.new do
33
+ attr_writer :username, :password
34
+
35
+ def self.extended(base)
36
+ base.instance_eval { @custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"' }
37
+ base.class_eval { alias_method_chain :process, :basic_authentication }
38
+ end
39
+
40
+ def process_with_basic_authentication(request, response)
41
+ if authorized?(request) then process_without_basic_authentication(request, response)
42
+ else reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
43
+ end
44
+ end
45
+
46
+ def authorized?(request)
47
+ request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
48
+ end
49
+ end
50
+ handler.extend(m)
51
+ end
52
+
53
+ def new_mongrel_redirector(target_url, relative_path = false)
54
+ target_url = "http://#{@host_and_port}#{target_url}" unless relative_path
55
+ Mongrel::RedirectHandler.new(target_url)
56
+ end
@@ -0,0 +1,52 @@
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 /^that service takes (\d+) seconds to generate a response$/ do |time|
16
+ preprocessor = lambda { sleep time.to_i }
17
+ @handler.preprocessor = preprocessor
18
+ end
19
+
20
+ Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
21
+ @handler.content_type = content_type
22
+ end
23
+
24
+ Given /the response from the service has a body of '(.*)'/ do |response_body|
25
+ @handler.response_body = response_body
26
+ end
27
+
28
+ Given /the url '(.*)' redirects to '(.*)'/ do |redirection_url, target_url|
29
+ @server.register redirection_url, new_mongrel_redirector(target_url)
30
+ end
31
+
32
+ Given /that service is protected by Basic Authentication/ do
33
+ add_basic_authentication_to @handler
34
+ end
35
+
36
+ Given /that service requires the username '(.*)' with the password '(.*)'/ do |username, password|
37
+ @handler.username = username
38
+ @handler.password = password
39
+ end
40
+
41
+ Given /a restricted page at '(.*)'/ do |url|
42
+ Given "a remote service that returns 'A response I will never see'"
43
+ And "that service is accessed at the path '#{url}'"
44
+ And "that service is protected by Basic Authentication"
45
+ And "that service requires the username 'something' with the password 'secret'"
46
+ end
47
+
48
+ # This joins the server thread, and halts cucumber, so you can actually hit the
49
+ # server with a browser. Runs until you kill it with Ctrl-c
50
+ Given /I want to hit this in a browser/ do
51
+ @server.acceptor.join
52
+ 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