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 xml stuff into core extensions where it should be for now.
jnunemaker (author)
Sat Dec 06 19:51:18 -0800 2008
commit  7fab301eb592353311a064c76e93c2363f8b7658
tree    747b16ec92b450b775f1ea64aab98f8931b16a91
parent  8a70a8ef40531def06c950bfb4902abc774b5b31
...
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
0
@@ -78,3 +78,215 @@ class String
0
     strip.empty?
0
   end
0
 end # class String
0
+
0
+require 'rexml/parsers/streamparser'
0
+require 'rexml/parsers/baseparser'
0
+require 'rexml/light/node'
0
+
0
+# This is a slighly modified version of the XMLUtilityNode from
0
+# http://merb.devjavu.com/projects/merb/ticket/95 (has.sox@gmail.com)
0
+# It's mainly just adding vowels, as I ht cd wth n vwls :)
0
+# This represents the hard part of the work, all I did was change the
0
+# underlying parser.
0
+class REXMLUtilityNode
0
+  attr_accessor :name, :attributes, :children, :type
0
+  
0
+  def self.typecasts
0
+    @@typecasts
0
+  end
0
+  
0
+  def self.typecasts=(obj)
0
+    @@typecasts = obj
0
+  end
0
+  
0
+  def self.available_typecasts
0
+    @@typecasts
0
+  end
0
+  
0
+  def self.available_typecasts=(obj)
0
+    @@typecasts = obj
0
+  end
0
+
0
+  self.typecasts = {}
0
+  self.typecasts["integer"]       = lambda{|v| v.nil? ? nil : v.to_i}
0
+  self.typecasts["boolean"]       = lambda{|v| v.nil? ? nil : (v.strip != "false")}
0
+  self.typecasts["datetime"]      = lambda{|v| v.nil? ? nil : Time.parse(v).utc}
0
+  self.typecasts["date"]          = lambda{|v| v.nil? ? nil : Date.parse(v)}
0
+  self.typecasts["dateTime"]      = lambda{|v| v.nil? ? nil : Time.parse(v).utc}
0
+  self.typecasts["decimal"]       = lambda{|v| BigDecimal(v)}
0
+  self.typecasts["double"]        = lambda{|v| v.nil? ? nil : v.to_f}
0
+  self.typecasts["float"]         = lambda{|v| v.nil? ? nil : v.to_f}
0
+  self.typecasts["symbol"]        = lambda{|v| v.to_sym}
0
+  self.typecasts["string"]        = lambda{|v| v.to_s}
0
+  self.typecasts["yaml"]          = lambda{|v| v.nil? ? nil : YAML.load(v)}
0
+  self.typecasts["base64Binary"]  = lambda{|v| v.unpack('m').first }
0
+
0
+  self.available_typecasts = self.typecasts.keys
0
+
0
+  def initialize(name, attributes = {})
0
+    @name         = name.tr("-", "_")
0
+    # leave the type alone if we don't know what it is
0
+    @type         = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"]
0
+
0
+    @nil_element  = attributes.delete("nil") == "true"
0
+    @attributes   = undasherize_keys(attributes)
0
+    @children     = []
0
+    @text         = false
0
+  end
0
+
0
+  def add_node(node)
0
+    @text = true if node.is_a? String
0
+    @children << node
0
+  end
0
+
0
+  def to_hash
0
+    if @type == "file"
0
+      f = StringIO.new((@children.first || '').unpack('m').first)
0
+      class << f
0
+        attr_accessor :original_filename, :content_type
0
+      end
0
+      f.original_filename = attributes['name'] || 'untitled'
0
+      f.content_type = attributes['content_type'] || 'application/octet-stream'
0
+      return {name => f}
0
+    end
0
+
0
+    if @text
0
+      return { name => typecast_value( translate_xml_entities( inner_html ) ) }
0
+    else
0
+      #change repeating groups into an array
0
+      groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s }
0
+
0
+      out = nil
0
+      if @type == "array"
0
+        out = []
0
+        groups.each do |k, v|
0
+          if v.size == 1
0
+            out << v.first.to_hash.entries.first.last
0
+          else
0
+            out << v.map{|e| e.to_hash[k]}
0
+          end
0
+        end
0
+        out = out.flatten
0
+
0
+      else # If Hash
0
+        out = {}
0
+        groups.each do |k,v|
0
+          if v.size == 1
0
+            out.merge!(v.first)
0
+          else
0
+            out.merge!( k => v.map{|e| e.to_hash[k]})
0
+          end
0
+        end
0
+        out.merge! attributes unless attributes.empty?
0
+        out = out.empty? ? nil : out
0
+      end
0
+
0
+      if @type && out.nil?
0
+        { name => typecast_value(out) }
0
+      else
0
+        { name => out }
0
+      end
0
+    end
0
+  end
0
+
0
+  # Typecasts a value based upon its type. For instance, if
0
+  # +node+ has #type == "integer",
0
+  # {{[node.typecast_value("12") #=> 12]}}
0
+  #
0
+  # @param value<String> The value that is being typecast.
0
+  #
0
+  # @details [:type options]
0
+  #   "integer"::
0
+  #     converts +value+ to an integer with #to_i
0
+  #   "boolean"::
0
+  #     checks whether +value+, after removing spaces, is the literal
0
+  #     "true"
0
+  #   "datetime"::
0
+  #     Parses +value+ using Time.parse, and returns a UTC Time
0
+  #   "date"::
0
+  #     Parses +value+ using Date.parse
0
+  #
0
+  # @return <Integer, TrueClass, FalseClass, Time, Date, Object>
0
+  #   The result of typecasting +value+.
0
+  #
0
+  # @note
0
+  #   If +self+ does not have a "type" key, or if it's not one of the
0
+  #   options specified above, the raw +value+ will be returned.
0
+  def typecast_value(value)
0
+    return value unless @type
0
+    proc = self.class.typecasts[@type]
0
+    proc.nil? ? value : proc.call(value)
0
+  end
0
+
0
+  # Convert basic XML entities into their literal values.
0
+  #
0
+  # @param value<#gsub> An XML fragment.
0
+  #
0
+  # @return <#gsub> The XML fragment after converting entities.
0
+  def translate_xml_entities(value)
0
+    value.gsub(/&lt;/,   "<").
0
+          gsub(/&gt;/,   ">").
0
+          gsub(/&quot;/, '"').
0
+          gsub(/&apos;/, "'").
0
+          gsub(/&amp;/,  "&")
0
+  end
0
+
0
+  # Take keys of the form foo-bar and convert them to foo_bar
0
+  def undasherize_keys(params)
0
+    params.keys.each do |key, value|
0
+      params[key.tr("-", "_")] = params.delete(key)
0
+    end
0
+    params
0
+  end
0
+
0
+  # Get the inner_html of the REXML node.
0
+  def inner_html
0
+    @children.join
0
+  end
0
+
0
+  # Converts the node into a readable HTML node.
0
+  #
0
+  # @return <String> The HTML node in text form.
0
+  def to_html
0
+    attributes.merge!(:type => @type ) if @type
0
+    "<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>"
0
+  end
0
+
0
+  # @alias #to_html #to_s
0
+  def to_s
0
+    to_html
0
+  end
0
+end
0
+
0
+class ToHashParser
0
+  def self.from_xml(xml)
0
+    stack = []
0
+    parser = REXML::Parsers::BaseParser.new(xml)
0
+
0
+    while true
0
+      event = parser.pull
0
+      case event[0]
0
+      when :end_document
0
+        break
0
+      when :end_doctype, :start_doctype
0
+        # do nothing
0
+      when :start_element
0
+        stack.push REXMLUtilityNode.new(event[1], event[2])
0
+      when :end_element
0
+        if stack.size > 1
0
+          temp = stack.pop
0
+          stack.last.add_node(temp)
0
+        end
0
+      when :text, :cdata
0
+        stack.last.add_node(event[1]) unless event[1].strip.length == 0
0
+      end
0
+    end
0
+    stack.pop.to_hash
0
+  end
0
+end
0
+
0
+class Hash
0
+  def self.from_xml(xml)
0
+    ToHashParser.from_xml(xml)
0
+  end
0
+end
0
\ No newline at end of file
...
5
6
7
 
8
9
10
...
98
99
100
101
102
103
 
104
...
5
6
7
8
9
10
11
...
99
100
101
 
 
102
103
104
0
@@ -5,6 +5,7 @@ require 'net/https'
0
 require 'rubygems'
0
 gem 'json', '>= 1.1.3'
0
 require 'json'
0
+
0
 require 'module_level_inheritable_attributes'
0
 require 'core_extensions'
0
 
0
@@ -98,5 +99,4 @@ module HTTParty
0
 end
0
 
0
 require 'httparty/exceptions'
0
-require 'httparty/request'
0
-require 'httparty/xml'
0
\ No newline at end of file
0
+require 'httparty/request'
0
\ No newline at end of file
...
107
108
109
110
 
111
112
113
...
107
108
109
 
110
111
112
113
0
@@ -107,7 +107,7 @@ module HTTParty
0
         return nil if body.nil? or body.empty?
0
         case format
0
         when :xml
0
-          ToHashParser.from_xml(body)
0
+          Hash.from_xml(body)
0
         when :json
0
           JSON.parse(body)
0
         else

Comments