httparty 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of httparty might be problematic. Click here for more details.

@@ -1,3 +1,10 @@
1
+ == 0.1.3 2008-08-22
2
+
3
+ * 3 major enhancements:
4
+ * Added http_proxy key for setting proxy server and port (francxk@gmail.com)
5
+ * Now raises exception when http error occurs (francxk@gmail.com)
6
+ * Changed auto format detection from file extension to response content type (Jay Pignata)
7
+
1
8
  == 0.1.2 2008-08-09
2
9
 
3
10
  * 1 major enhancement:
data/README.txt CHANGED
@@ -9,7 +9,7 @@ Makes http fun again!
9
9
  * Easy get, post, put, delete requests
10
10
  * Basic http authentication
11
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
12
+ * Automatic parsing of JSON and XML into ruby hashes based on response content-type
13
13
 
14
14
  == SYNOPSIS:
15
15
 
@@ -8,7 +8,6 @@ module AAWS
8
8
  include HTTParty
9
9
  base_uri 'http://ecs.amazonaws.com'
10
10
  default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
11
- format :xml
12
11
 
13
12
  def initialize(key)
14
13
  self.class.default_params :AWSAccessKeyId => key
@@ -6,7 +6,6 @@ config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
6
6
  class Delicious
7
7
  include HTTParty
8
8
  base_uri 'https://api.del.icio.us/v1'
9
- format :xml
10
9
 
11
10
  def initialize(u, p)
12
11
  @auth = {:username => u, :password => p}
@@ -4,7 +4,6 @@ require 'pp'
4
4
 
5
5
  class Rep
6
6
  include HTTParty
7
- format :xml
8
7
  end
9
8
 
10
9
  puts Rep.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544').inspect
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{httparty}
3
- s.version = "0.1.2"
3
+ s.version = "0.1.3"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["John Nunemaker"]
7
- s.date = %q{2008-08-09}
7
+ s.date = %q{2008-08-22}
8
8
  s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
9
9
  s.email = ["nunemaker@gmail.com"]
10
10
  s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt"]
@@ -12,15 +12,27 @@ dir = File.expand_path(File.join(File.dirname(__FILE__), 'httparty'))
12
12
  require dir + '/core_ext'
13
13
 
14
14
  module HTTParty
15
+ class UnsupportedFormat < StandardError; end
16
+
15
17
  def self.included(base)
16
18
  base.extend ClassMethods
17
19
  end
18
20
 
19
- class UnsupportedFormat < StandardError; end
20
-
21
- AllowedFormats = %w[xml json]
21
+ AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
22
22
 
23
23
  module ClassMethods
24
+ #
25
+ # Set an http proxy
26
+ #
27
+ # class Twitter
28
+ # include HTTParty
29
+ # http_proxy http://myProxy, 1080
30
+ # ....
31
+ def http_proxy(addr=nil, port = nil)
32
+ @http_proxyaddr = addr
33
+ @http_proxyport = port
34
+ end
35
+
24
36
  def base_uri(base_uri=nil)
25
37
  return @base_uri unless base_uri
26
38
  # don't want this to ever end with /
@@ -52,8 +64,7 @@ module HTTParty
52
64
  end
53
65
 
54
66
  def format(f)
55
- f = f.to_s
56
- raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f)
67
+ raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f)
57
68
  @format = f
58
69
  end
59
70
 
@@ -80,7 +91,7 @@ module HTTParty
80
91
  private
81
92
  def http(uri) #:nodoc:
82
93
  if @http.blank?
83
- @http = Net::HTTP.new(uri.host, uri.port)
94
+ @http = Net::HTTP.new(uri.host, uri.port, @http_proxyaddr, @http_proxyport)
84
95
  @http.use_ssl = (uri.port == 443)
85
96
  # so we can avoid ssl warnings
86
97
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -94,13 +105,11 @@ module HTTParty
94
105
  # body => hash of keys/values or a query string (foo=bar&baz=poo)
95
106
  # headers => hash of headers to send request with
96
107
  # basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
108
+ # Raises exception Net::XXX (http error code) if an http error occured
97
109
  def send_request(method, path, options={}) #:nodoc:
98
110
  raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
99
111
  raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
100
112
  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
113
  uri = URI.parse("#{base_uri}#{path}")
105
114
  existing_query = uri.query ? "#{uri.query}&" : ''
106
115
  uri.query = if options[:query].blank?
@@ -116,14 +125,24 @@ module HTTParty
116
125
  # note to self: self, do not put basic auth above headers because it removes basic auth
117
126
  request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
118
127
  response = http(uri).request(request)
119
- parse_response(response.body)
128
+ @format ||= format_from_mimetype(response['content-type'])
129
+
130
+ case response
131
+ when Net::HTTPSuccess
132
+ parse_response(response.body)
133
+ else
134
+ response.instance_eval { class << self; attr_accessor :body_parsed; end }
135
+ begin; response.body_parsed = parse_response(response.body); rescue; end
136
+ response.error! # raises exception corresponding to http error Net::XXX
137
+ end
138
+
120
139
  end
121
140
 
122
141
  def parse_response(body) #:nodoc:
123
142
  case @format
124
- when 'xml'
143
+ when :xml
125
144
  Hash.from_xml(body)
126
- when 'json'
145
+ when :json
127
146
  ActiveSupport::JSON.decode(body)
128
147
  else
129
148
  # just return the response if no format
@@ -136,13 +155,10 @@ module HTTParty
136
155
  str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
137
156
  end
138
157
 
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) #:nodoc:
144
- ext = File.extname(path)[1..-1]
145
- !ext.blank? && AllowedFormats.include?(ext) ? ext : nil
158
+ # Uses the HTTP Content-Type header to determine the format of the response
159
+ # It compares the MIME type returned to the types stored in the AllowedFormats hash
160
+ def format_from_mimetype(mimetype) #:nodoc:
161
+ AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
146
162
  end
147
163
  end
148
164
  end
@@ -2,7 +2,7 @@ module HTTParty
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-09 00:00:00 -04:00
12
+ date: 2008-08-22 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency