public
Rubygem
Description: Makes http fun! Also, makes consuming restful web services dead easy.
Homepage:
Clone URL: git://github.com/jnunemaker/httparty.git
Click here to lend your support to: httparty and make a donation at www.pledgie.com !
Moved module inheritable attributes to it's own file. Added specs for 
HTTParty.get.
jnunemaker (author)
Sat Dec 06 16:41:23 -0800 2008
commit  aa09eb4c656ea124f2ce75eaefc4ffdaea5fe74c
tree    577479da6e059bd02a00174327eb87f507b48dc1
parent  e72ec5a483538eb8e133fc7c0536051b90060e18
...
7
8
9
 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
53
54
55
56
57
58
59
60
61
62
63
64
65
...
132
133
134
135
136
...
7
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
15
...
28
29
30
 
 
 
 
 
 
 
31
32
33
...
100
101
102
 
103
0
@@ -7,34 +7,9 @@ require 'active_support'
0
 dir = File.dirname(__FILE__)
0
 $:.unshift(dir) unless $:.include?(dir) || $:.include?(File.expand_path(dir))
0
 
0
+require 'module_level_inheritable_attributes'
0
 require 'httparty/request'
0
 
0
-module ModuleLevelInheritableAttributes
0
-  def self.included(base)
0
-    base.extend(ClassMethods)
0
-  end
0
-  
0
-  module ClassMethods
0
-    def mattr_inheritable(*args)
0
-      @mattr_inheritable_attrs ||= [:mattr_inheritable_attrs]
0
-      @mattr_inheritable_attrs += args
0
-      args.each do |arg|
0
-        module_eval %(
0
-          class << self; attr_accessor :#{arg} end
0
-        )
0
-      end
0
-      @mattr_inheritable_attrs
0
-    end
0
-
0
-    def inherited(subclass)
0
-      @mattr_inheritable_attrs.each do |inheritable_attribute|
0
-        instance_var = "@#{inheritable_attribute}" 
0
-        subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
0
-      end
0
-    end
0
-  end
0
-end
0
-
0
 module HTTParty
0
   class UnsupportedFormat < StandardError; end
0
   class RedirectionTooDeep < StandardError; end
0
@@ -53,13 +28,6 @@ module HTTParty
0
       @default_options
0
     end
0
 
0
-    #
0
-    # Set an http proxy
0
-    #
0
-    #  class Twitter
0
-    #    include HTTParty
0
-    #    http_proxy 'http://myProxy', 1080
0
-    # ....
0
     def http_proxy(addr=nil, port = nil)
0
       default_options[:http_proxyaddr] = addr
0
       default_options[:http_proxyport] = port
0
@@ -132,5 +100,4 @@ module HTTParty
0
   def self.delete(*args)
0
     Basement.delete(*args)
0
   end
0
-
0
 end
...
32
33
34
 
 
 
 
35
36
37
...
47
48
49
50
 
 
 
 
 
51
52
53
 
 
 
 
 
54
55
56
...
88
89
90
91
 
92
93
94
...
32
33
34
35
36
37
38
39
40
41
...
51
52
53
 
54
55
56
57
58
59
60
 
61
62
63
64
65
66
67
68
...
100
101
102
 
103
104
105
106
0
@@ -32,6 +32,10 @@ module HTTParty
0
       uri
0
     end
0
     
0
+    def format
0
+      options[:format]
0
+    end
0
+    
0
     def perform
0
       validate!
0
       handle_response!(get_response(uri))
0
@@ -47,10 +51,18 @@ module HTTParty
0
       
0
       def get_response(uri) #:nodoc:
0
         request = http_method.new(uri.request_uri)   
0
-        request.set_form_data(options[:query]) if post? && options[:query]
0
+        
0
+        if post? && options[:query]
0
+          request.set_form_data(options[:query])
0
+        end
0
+        
0
         request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
0
         request.initialize_http_header options[:headers]
0
-        request.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password]) if options[:basic_auth]
0
+        
0
+        if options[:basic_auth]
0
+          request.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password])
0
+        end
0
+        
0
         response = http(uri).request(request)
0
         options[:format] ||= format_from_mimetype(response['content-type'])
0
         response
0
@@ -88,7 +100,7 @@ module HTTParty
0
       
0
       def parse_response(body) #:nodoc:
0
         return nil if body.nil? or body.empty?
0
-        case options[:format]
0
+        case format
0
         when :xml
0
           Hash.from_xml(body)
0
         when :json
...
116
117
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
0
@@ -116,4 +116,45 @@ describe HTTParty do
0
       GRest.default_options.should == {:base_uri => 'grest.com', :default_params => {:one => 'two'}}
0
     end
0
   end
0
+  
0
+  describe "#get" do
0
+    it "should be able to get html" do
0
+      stub_http_response_with('google.html')
0
+      HTTParty.get('http://www.google.com').should == file_fixture('google.html')
0
+    end
0
+    
0
+    it "should be able parse response type json automatically" do
0
+      stub_http_response_with('twitter.json')
0
+      tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
0
+      tweets.size.should == 20
0
+      tweets.first['user'].should == {
0
+        "name"              => "Pyk", 
0
+        "url"               => nil, 
0
+        "id"                => "7694602", 
0
+        "description"       => nil, 
0
+        "protected"         => false, 
0
+        "screen_name"       => "Pyk", 
0
+        "followers_count"   => 1, 
0
+        "location"          => "Opera Plaza, California", 
0
+        "profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
0
+      }
0
+    end
0
+    
0
+    it "should be able parse response type xml automatically" do
0
+      stub_http_response_with('twitter.xml')
0
+      tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
0
+      tweets['statuses'].size.should == 20
0
+      tweets['statuses'].first['user'].should == {
0
+        "name"              => "Magic 8 Bot", 
0
+        "url"               => nil, 
0
+        "id"                => "17656026", 
0
+        "description"       => "ask me a question", 
0
+        "protected"         => "false", 
0
+        "screen_name"       => "magic8bot", 
0
+        "followers_count"   => "90", 
0
+        "profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg", 
0
+        "location"          => nil
0
+      }
0
+    end
0
+  end
0
 end
...
6
7
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
...
6
7
8
 
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -6,4 +6,24 @@ rescue LoadError
0
   require 'spec'
0
 end
0
 
0
-require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
0
\ No newline at end of file
0
+require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
0
+
0
+def file_fixture(filename)
0
+  open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
0
+end
0
+
0
+def stub_http_response_with(fixture_name)
0
+  format = fixture_name.split('.').last.intern
0
+  data = file_fixture(fixture_name)
0
+  http = Net::HTTP.new('localhost', 80)
0
+  
0
+  response = Net::HTTPOK.new("1.1", 200, "Content for you")
0
+  response.stub!(:body).and_return(data)
0
+  http.stub!(:request).and_return(response)
0
+  
0
+  http_request = HTTParty::Request.new(Net::HTTP::Get, '')
0
+  http_request.stub!(:get_response).and_return(response)
0
+  http_request.stub!(:format).and_return(format)
0
+  
0
+  HTTParty::Request.should_receive(:new).and_return(http_request)
0
+end
0
\ No newline at end of file

Comments