1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Now using crack for xml and json parsing instead of bundling that in HTTParty. Hopefully panic will not ensue.

This commit is contained in:
John Nunemaker 2009-03-29 00:30:05 -04:00
parent 8115056d74
commit 690eeb81f5
10 changed files with 32 additions and 953 deletions

View file

@ -14,6 +14,7 @@ Echoe.new(ProjectName, HTTParty::Version) do |p|
p.url = "http://#{ProjectName}.rubyforge.org"
p.author = "John Nunemaker"
p.email = "nunemaker@gmail.com"
p.extra_deps = [['jnunemaker-crack', '>= 0.1.0']]
p.need_tar_gz = false
p.docs_host = WebsitePath
end

View file

@ -1,175 +0,0 @@
require 'time'
# Copyright (c) 2008 Sam Smoot.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class Object #:nodoc:
# @return <TrueClass, FalseClass>
#
# @example [].blank? #=> true
# @example [1].blank? #=> false
# @example [nil].blank? #=> false
#
# Returns true if the object is nil or empty (if applicable)
def blank?
nil? || (respond_to?(:empty?) && empty?)
end unless method_defined?(:blank?)
end # class Object
class Numeric #:nodoc:
# @return <TrueClass, FalseClass>
#
# Numerics can't be blank
def blank?
false
end unless method_defined?(:blank?)
end # class Numeric
class NilClass #:nodoc:
# @return <TrueClass, FalseClass>
#
# Nils are always blank
def blank?
true
end unless method_defined?(:blank?)
end # class NilClass
class TrueClass #:nodoc:
# @return <TrueClass, FalseClass>
#
# True is not blank.
def blank?
false
end unless method_defined?(:blank?)
end # class TrueClass
class FalseClass #:nodoc:
# False is always blank.
def blank?
true
end unless method_defined?(:blank?)
end # class FalseClass
class String #:nodoc:
# @example "".blank? #=> true
# @example " ".blank? #=> true
# @example " hey ho ".blank? #=> false
#
# @return <TrueClass, FalseClass>
#
# Strips out whitespace then tests if the string is empty.
def blank?
strip.empty?
end unless method_defined?(:blank?)
def snake_case
return self.downcase if self =~ /^[A-Z]+$/
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
return $+.downcase
end unless method_defined?(:snake_case)
end # class String
class Hash #:nodoc:
# @return <String> This hash as a query string
#
# @example
# { :name => "Bob",
# :address => {
# :street => '111 Ruby Ave.',
# :city => 'Ruby Central',
# :phones => ['111-111-1111', '222-222-2222']
# }
# }.to_params
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
def to_params
params = self.map { |k,v| normalize_param(k,v) }.join
params.chop! # trailing &
params
end
# @param key<Object> The key for the param.
# @param value<Object> The value for the param.
#
# @return <String> This key value pair as a param
#
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
def normalize_param(key, value)
param = ''
stack = []
if value.is_a?(Array)
param << value.map { |element| normalize_param("#{key}[]", element) }.join
elsif value.is_a?(Hash)
stack << [key,value]
else
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
end
stack.each do |parent, hash|
hash.each do |key, value|
if value.is_a?(Hash)
stack << ["#{parent}[#{key}]", value]
else
param << normalize_param("#{parent}[#{key}]", value)
end
end
end
param
end
# @return <String> The hash as attributes for an XML tag.
#
# @example
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
# #=> 'one="1" two="TWO"'
def to_xml_attributes
map do |k,v|
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
end.join(' ')
end
end
class BlankSlate #:nodoc:
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
end
# 1.8.6 has mistyping of transitive in if statement
require "rexml/document"
module REXML #:nodoc:
class Document < Element #:nodoc:
def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
output = Output.new( output, xml_decl.encoding )
end
formatter = if indent > -1
if transitive
REXML::Formatters::Transitive.new( indent, ie_hack )
else
REXML::Formatters::Pretty.new( indent, ie_hack )
end
else
REXML::Formatters::Default.new( ie_hack )
end
formatter.write( self, output )
end
end
end

View file

@ -2,8 +2,10 @@ $:.unshift(File.dirname(__FILE__))
require 'net/http'
require 'net/https'
require 'core_extensions'
require 'httparty/module_inheritable_attributes'
require 'rubygems'
gem 'crack'
require 'crack'
module HTTParty
@ -195,7 +197,7 @@ module HTTParty
end
require 'httparty/cookie_hash'
require 'httparty/core_extensions'
require 'httparty/exceptions'
require 'httparty/request'
require 'httparty/response'
require 'httparty/parsers'

View file

@ -0,0 +1,25 @@
class BlankSlate #:nodoc:
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
end
# 1.8.6 has mistyping of transitive in if statement
require "rexml/document"
module REXML #:nodoc:
class Document < Element #:nodoc:
def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
output = Output.new( output, xml_decl.encoding )
end
formatter = if indent > -1
if transitive
REXML::Formatters::Transitive.new( indent, ie_hack )
else
REXML::Formatters::Pretty.new( indent, ie_hack )
end
else
REXML::Formatters::Default.new( ie_hack )
end
formatter.write( self, output )
end
end
end

View file

@ -1,4 +0,0 @@
Dir[File.dirname(__FILE__) + "/parsers/*.rb"].sort.each do |path|
filename = File.basename(path)
require "httparty/parsers/#{filename}"
end

View file

@ -1,74 +0,0 @@
# Copyright (c) 2004-2008 David Heinemeier Hansson
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'yaml'
require 'strscan'
module HTTParty
module Parsers #:nodoc:
module JSON #:nodoc:
class ParseError < StandardError #:nodoc:
end
def self.decode(json)
YAML.load(unescape(convert_json_to_yaml(json)))
rescue ArgumentError => e
raise ParseError, "Invalid JSON string"
end
protected
def self.unescape(str)
str.gsub(/\\u([0-9a-f]{4})/) {
[$1.hex].pack("U")
}
end
# matches YAML-formatted dates
DATE_REGEX = /^\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?$/
# Ensure that ":" and "," are always followed by a space
def self.convert_json_to_yaml(json) #:nodoc:
scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
case char = scanner[1]
when '"', "'"
if !quoting
quoting = char
pos = scanner.pos
elsif quoting == char
if json[pos..scanner.pos-2] =~ DATE_REGEX
# found a date, track the exact positions of the quotes so we can remove them later.
# oh, and increment them for each current mark, each one is an extra padded space that bumps
# the position in the final YAML output
total_marks = marks.size
times << pos+total_marks << scanner.pos+total_marks
end
quoting = false
end
when ":",","
marks << scanner.pos - 1 unless quoting
end
end
if marks.empty?
json.gsub(/\\\//, '/')
else
left_pos = [-1].push(*marks)
right_pos = marks << json.length
output = []
left_pos.each_with_index do |left, i|
output << json[left.succ..right_pos[i]]
end
output = output * " "
times.each { |i| output[i-1] = ' ' }
output.gsub!(/\\\//, '/')
output
end
end
end
end
end

View file

@ -1,209 +0,0 @@
require 'rexml/parsers/streamparser'
require 'rexml/parsers/baseparser'
require 'rexml/light/node'
# This is a slighly modified version of the XMLUtilityNode from
# http://merb.devjavu.com/projects/merb/ticket/95 (has.sox@gmail.com)
# It's mainly just adding vowels, as I ht cd wth n vwls :)
# This represents the hard part of the work, all I did was change the
# underlying parser.
class REXMLUtilityNode #:nodoc:
attr_accessor :name, :attributes, :children, :type
def self.typecasts
@@typecasts
end
def self.typecasts=(obj)
@@typecasts = obj
end
def self.available_typecasts
@@available_typecasts
end
def self.available_typecasts=(obj)
@@available_typecasts = obj
end
self.typecasts = {}
self.typecasts["integer"] = lambda{|v| v.nil? ? nil : v.to_i}
self.typecasts["boolean"] = lambda{|v| v.nil? ? nil : (v.strip != "false")}
self.typecasts["datetime"] = lambda{|v| v.nil? ? nil : Time.parse(v).utc}
self.typecasts["date"] = lambda{|v| v.nil? ? nil : Date.parse(v)}
self.typecasts["dateTime"] = lambda{|v| v.nil? ? nil : Time.parse(v).utc}
self.typecasts["decimal"] = lambda{|v| v.nil? ? nil : BigDecimal(v.to_s)}
self.typecasts["double"] = lambda{|v| v.nil? ? nil : v.to_f}
self.typecasts["float"] = lambda{|v| v.nil? ? nil : v.to_f}
self.typecasts["symbol"] = lambda{|v| v.nil? ? nil : v.to_sym}
self.typecasts["string"] = lambda{|v| v.to_s}
self.typecasts["yaml"] = lambda{|v| v.nil? ? nil : YAML.load(v)}
self.typecasts["base64Binary"] = lambda{|v| v.unpack('m').first }
self.available_typecasts = self.typecasts.keys
def initialize(name, attributes = {})
@name = name.tr("-", "_")
# leave the type alone if we don't know what it is
@type = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"]
@nil_element = attributes.delete("nil") == "true"
@attributes = undasherize_keys(attributes)
@children = []
@text = false
end
def add_node(node)
@text = true if node.is_a? String
@children << node
end
def to_hash
if @type == "file"
f = StringIO.new((@children.first || '').unpack('m').first)
class << f
attr_accessor :original_filename, :content_type
end
f.original_filename = attributes['name'] || 'untitled'
f.content_type = attributes['content_type'] || 'application/octet-stream'
return {name => f}
end
if @text
return { name => typecast_value( translate_xml_entities( inner_html ) ) }
else
#change repeating groups into an array
groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s }
out = nil
if @type == "array"
out = []
groups.each do |k, v|
if v.size == 1
out << v.first.to_hash.entries.first.last
else
out << v.map{|e| e.to_hash[k]}
end
end
out = out.flatten
else # If Hash
out = {}
groups.each do |k,v|
if v.size == 1
out.merge!(v.first)
else
out.merge!( k => v.map{|e| e.to_hash[k]})
end
end
out.merge! attributes unless attributes.empty?
out = out.empty? ? nil : out
end
if @type && out.nil?
{ name => typecast_value(out) }
else
{ name => out }
end
end
end
# Typecasts a value based upon its type. For instance, if
# +node+ has #type == "integer",
# {{[node.typecast_value("12") #=> 12]}}
#
# @param value<String> The value that is being typecast.
#
# @details [:type options]
# "integer"::
# converts +value+ to an integer with #to_i
# "boolean"::
# checks whether +value+, after removing spaces, is the literal
# "true"
# "datetime"::
# Parses +value+ using Time.parse, and returns a UTC Time
# "date"::
# Parses +value+ using Date.parse
#
# @return <Integer, TrueClass, FalseClass, Time, Date, Object>
# The result of typecasting +value+.
#
# @note
# If +self+ does not have a "type" key, or if it's not one of the
# options specified above, the raw +value+ will be returned.
def typecast_value(value)
return value unless @type
proc = self.class.typecasts[@type]
proc.nil? ? value : proc.call(value)
end
# Convert basic XML entities into their literal values.
#
# @param value<#gsub> An XML fragment.
#
# @return <#gsub> The XML fragment after converting entities.
def translate_xml_entities(value)
value.gsub(/&lt;/, "<").
gsub(/&gt;/, ">").
gsub(/&quot;/, '"').
gsub(/&apos;/, "'").
gsub(/&amp;/, "&")
end
# Take keys of the form foo-bar and convert them to foo_bar
def undasherize_keys(params)
params.keys.each do |key, value|
params[key.tr("-", "_")] = params.delete(key)
end
params
end
# Get the inner_html of the REXML node.
def inner_html
@children.join
end
# Converts the node into a readable HTML node.
#
# @return <String> The HTML node in text form.
def to_html
attributes.merge!(:type => @type ) if @type
"<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>"
end
# @alias #to_html #to_s
def to_s
to_html
end
end
module HTTParty
module Parsers #:nodoc:
module XML #:nodoc:
def self.parse(xml)
stack = []
parser = REXML::Parsers::BaseParser.new(xml)
while true
event = parser.pull
case event[0]
when :end_document
break
when :end_doctype, :start_doctype
# do nothing
when :start_element
stack.push REXMLUtilityNode.new(event[1], event[2])
when :end_element
if stack.size > 1
temp = stack.pop
stack.last.add_node(temp)
end
when :text, :cdata
stack.last.add_node(event[1]) unless event[1].strip.length == 0 || stack.empty?
end
end
stack.pop.to_hash
end
end
end
end

View file

@ -107,9 +107,9 @@ module HTTParty
return nil if body.nil? or body.empty?
case format
when :xml
HTTParty::Parsers::XML.parse(body)
Crack::XML.parse(body)
when :json
HTTParty::Parsers::JSON.decode(body)
Crack::JSON.parse(body)
when :yaml
YAML::load(body)
else

View file

@ -1,42 +0,0 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
describe HTTParty::Parsers::JSON do
TESTS = {
%q({"data": "G\u00fcnter"}) => {"data" => "Günter"},
%q({"returnTo":{"\/categories":"\/"}}) => {"returnTo" => {"/categories" => "/"}},
%q({returnTo:{"\/categories":"\/"}}) => {"returnTo" => {"/categories" => "/"}},
%q({"return\\"To\\":":{"\/categories":"\/"}}) => {"return\"To\":" => {"/categories" => "/"}},
%q({"returnTo":{"\/categories":1}}) => {"returnTo" => {"/categories" => 1}},
%({"returnTo":[1,"a"]}) => {"returnTo" => [1, "a"]},
%({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]},
%({a: "'", "b": "5,000"}) => {"a" => "'", "b" => "5,000"},
%({a: "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"},
%({a: "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({a: "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# no time zone
%({a: "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
%([]) => [],
%({}) => {},
%(1) => 1,
%("") => "",
%("\\"") => "\"",
%(null) => nil,
%(true) => true,
%(false) => false,
%q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1"
}
TESTS.each do |json, expected|
it "should decode json (#{json})" do
lambda {
HTTParty::Parsers::JSON.decode(json).should == expected
}.should_not raise_error
end
end
it "should raise error for failed decoding" do
lambda {
HTTParty::Parsers::JSON.decode(%({: 1}))
}.should raise_error(HTTParty::Parsers::JSON::ParseError)
end
end

View file

@ -1,445 +0,0 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
require "date"
require 'bigdecimal'
describe HTTParty::Parsers::XML, "#parse" do
it "should transform a simple tag with content" do
xml = "<tag>This is the contents</tag>"
HTTParty::Parsers::XML.parse(xml).should == { 'tag' => 'This is the contents' }
end
it "should work with cdata tags" do
xml = <<-END
<tag>
<![CDATA[
text inside cdata
]]>
</tag>
END
HTTParty::Parsers::XML.parse(xml)["tag"].strip.should == "text inside cdata"
end
it "should transform a simple tag with attributes" do
xml = "<tag attr1='1' attr2='2'></tag>"
hash = { 'tag' => { 'attr1' => '1', 'attr2' => '2' } }
HTTParty::Parsers::XML.parse(xml).should == hash
end
it "should transform repeating siblings into an array" do
xml =<<-XML
<opt>
<user login="grep" fullname="Gary R Epstein" />
<user login="stty" fullname="Simon T Tyson" />
</opt>
XML
HTTParty::Parsers::XML.parse(xml)['opt']['user'].should be_an_instance_of(Array)
hash = {
'opt' => {
'user' => [{
'login' => 'grep',
'fullname' => 'Gary R Epstein'
},{
'login' => 'stty',
'fullname' => 'Simon T Tyson'
}]
}
}
HTTParty::Parsers::XML.parse(xml).should == hash
end
it "should not transform non-repeating siblings into an array" do
xml =<<-XML
<opt>
<user login="grep" fullname="Gary R Epstein" />
</opt>
XML
HTTParty::Parsers::XML.parse(xml)['opt']['user'].should be_an_instance_of(Hash)
hash = {
'opt' => {
'user' => {
'login' => 'grep',
'fullname' => 'Gary R Epstein'
}
}
}
HTTParty::Parsers::XML.parse(xml).should == hash
end
it "should typecast an integer" do
xml = "<tag type='integer'>10</tag>"
HTTParty::Parsers::XML.parse(xml)['tag'].should == 10
end
it "should typecast a true boolean" do
xml = "<tag type='boolean'>true</tag>"
HTTParty::Parsers::XML.parse(xml)['tag'].should be_true
end
it "should typecast a false boolean" do
["false"].each do |w|
HTTParty::Parsers::XML.parse("<tag type='boolean'>#{w}</tag>")['tag'].should be_false
end
end
it "should typecast a datetime" do
xml = "<tag type='datetime'>2007-12-31 10:32</tag>"
HTTParty::Parsers::XML.parse(xml)['tag'].should == Time.parse( '2007-12-31 10:32' ).utc
end
it "should typecast a date" do
xml = "<tag type='date'>2007-12-31</tag>"
HTTParty::Parsers::XML.parse(xml)['tag'].should == Date.parse('2007-12-31')
end
it "should unescape html entities" do
values = {
"<" => "&lt;",
">" => "&gt;",
'"' => "&quot;",
"'" => "&apos;",
"&" => "&amp;"
}
values.each do |k,v|
xml = "<tag>Some content #{v}</tag>"
HTTParty::Parsers::XML.parse(xml)['tag'].should match(Regexp.new(k))
end
end
it "should undasherize keys as tags" do
xml = "<tag-1>Stuff</tag-1>"
HTTParty::Parsers::XML.parse(xml).keys.should include( 'tag_1' )
end
it "should undasherize keys as attributes" do
xml = "<tag1 attr-1='1'></tag1>"
HTTParty::Parsers::XML.parse(xml)['tag1'].keys.should include( 'attr_1')
end
it "should undasherize keys as tags and attributes" do
xml = "<tag-1 attr-1='1'></tag-1>"
HTTParty::Parsers::XML.parse(xml).keys.should include( 'tag_1' )
HTTParty::Parsers::XML.parse(xml)['tag_1'].keys.should include( 'attr_1')
end
it "should render nested content correctly" do
xml = "<root><tag1>Tag1 Content <em><strong>This is strong</strong></em></tag1></root>"
HTTParty::Parsers::XML.parse(xml)['root']['tag1'].should == "Tag1 Content <em><strong>This is strong</strong></em>"
end
it "should render nested content with split text nodes correctly" do
xml = "<root>Tag1 Content<em>Stuff</em> Hi There</root>"
HTTParty::Parsers::XML.parse(xml)['root'].should == "Tag1 Content<em>Stuff</em> Hi There"
end
it "should ignore attributes when a child is a text node" do
xml = "<root attr1='1'>Stuff</root>"
HTTParty::Parsers::XML.parse(xml).should == { "root" => "Stuff" }
end
it "should ignore attributes when any child is a text node" do
xml = "<root attr1='1'>Stuff <em>in italics</em></root>"
HTTParty::Parsers::XML.parse(xml).should == { "root" => "Stuff <em>in italics</em>" }
end
it "should correctly transform multiple children" do
xml = <<-XML
<user gender='m'>
<age type='integer'>35</age>
<name>Home Simpson</name>
<dob type='date'>1988-01-01</dob>
<joined-at type='datetime'>2000-04-28 23:01</joined-at>
<is-cool type='boolean'>true</is-cool>
</user>
XML
hash = {
"user" => {
"gender" => "m",
"age" => 35,
"name" => "Home Simpson",
"dob" => Date.parse('1988-01-01'),
"joined_at" => Time.parse("2000-04-28 23:01"),
"is_cool" => true
}
}
HTTParty::Parsers::XML.parse(xml).should == hash
end
it "should properly handle nil values (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<topic>
<title></title>
<id type="integer"></id>
<approved type="boolean"></approved>
<written-on type="date"></written-on>
<viewed-at type="datetime"></viewed-at>
<content type="yaml"></content>
<parent-id></parent-id>
</topic>
EOT
expected_topic_hash = {
'title' => nil,
'id' => nil,
'approved' => nil,
'written_on' => nil,
'viewed_at' => nil,
'content' => nil,
'parent_id' => nil
}
HTTParty::Parsers::XML.parse(topic_xml)["topic"].should == expected_topic_hash
end
it "should handle a single record from xml (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<topic>
<title>The First Topic</title>
<author-name>David</author-name>
<id type="integer">1</id>
<approved type="boolean"> true </approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
<ad-revenue type="decimal">1.5</ad-revenue>
<optimum-viewing-angle type="float">135</optimum-viewing-angle>
<resident type="symbol">yes</resident>
</topic>
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => true,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
# Changed this line where the key is :message. The yaml specifies this as a symbol, and who am I to change what you specify
# The line in ActiveSupport is
# 'content' => { 'message' => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
'content' => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil,
'ad_revenue' => BigDecimal("1.50"),
'optimum_viewing_angle' => 135.0,
'resident' => :yes
}
HTTParty::Parsers::XML.parse(topic_xml)["topic"].each do |k,v|
v.should == expected_topic_hash[k]
end
end
it "should handle multiple records (ActiveSupport Compatible)" do
topics_xml = <<-EOT
<topics type="array">
<topic>
<title>The First Topic</title>
<author-name>David</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id nil="true"></parent-id>
</topic>
<topic>
<title>The Second Topic</title>
<author-name>Jason</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
</topic>
</topics>
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => false,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
'content' => "Have a nice day",
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil
}
# puts HTTParty::Parsers::XML.parse(topics_xml)['topics'].first.inspect
HTTParty::Parsers::XML.parse(topics_xml)["topics"].first.each do |k,v|
v.should == expected_topic_hash[k]
end
end
it "should handle a single record from_xml with attributes other than type (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<rsp stat="ok">
<photos page="1" pages="1" perpage="100" total="16">
<photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/>
</photos>
</rsp>
EOT
expected_topic_hash = {
'id' => "175756086",
'owner' => "55569174@N00",
'secret' => "0279bf37a1",
'server' => "76",
'title' => "Colored Pencil PhotoBooth Fun",
'ispublic' => "1",
'isfriend' => "0",
'isfamily' => "0",
}
HTTParty::Parsers::XML.parse(topic_xml)["rsp"]["photos"]["photo"].each do |k,v|
v.should == expected_topic_hash[k]
end
end
it "should handle an emtpy array (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array"></posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => []}}
HTTParty::Parsers::XML.parse(blog_xml).should == expected_blog_hash
end
it "should handle empty array with whitespace from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => []}}
HTTParty::Parsers::XML.parse(blog_xml).should == expected_blog_hash
end
it "should handle array with one entry from_xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
<post>a post</post>
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => ["a post"]}}
HTTParty::Parsers::XML.parse(blog_xml).should == expected_blog_hash
end
it "should handle array with multiple entries from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
<post>a post</post>
<post>another post</post>
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}}
HTTParty::Parsers::XML.parse(blog_xml).should == expected_blog_hash
end
it "should handle file types (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<logo type="file" name="logo.png" content_type="image/png">
</logo>
</blog>
XML
hash = HTTParty::Parsers::XML.parse(blog_xml)
hash.should have_key('blog')
hash['blog'].should have_key('logo')
file = hash['blog']['logo']
file.original_filename.should == 'logo.png'
file.content_type.should == 'image/png'
end
it "should handle file from xml with defaults (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<logo type="file">
</logo>
</blog>
XML
file = HTTParty::Parsers::XML.parse(blog_xml)['blog']['logo']
file.original_filename.should == 'untitled'
file.content_type.should == 'application/octet-stream'
end
it "should handle xsd like types from xml (ActiveSupport Compatible)" do
bacon_xml = <<-EOT
<bacon>
<weight type="double">0.5</weight>
<price type="decimal">12.50</price>
<chunky type="boolean"> 1 </chunky>
<expires-at type="dateTime">2007-12-25T12:34:56+0000</expires-at>
<notes type="string"></notes>
<illustration type="base64Binary">YmFiZS5wbmc=</illustration>
</bacon>
EOT
expected_bacon_hash = {
'weight' => 0.5,
'chunky' => true,
'price' => BigDecimal("12.50"),
'expires_at' => Time.utc(2007,12,25,12,34,56),
'notes' => "",
'illustration' => "babe.png"
}
HTTParty::Parsers::XML.parse(bacon_xml)["bacon"].should == expected_bacon_hash
end
it "should let type trickle through when unknown (ActiveSupport Compatible)" do
product_xml = <<-EOT
<product>
<weight type="double">0.5</weight>
<image type="ProductImage"><filename>image.gif</filename></image>
</product>
EOT
expected_product_hash = {
'weight' => 0.5,
'image' => {'type' => 'ProductImage', 'filename' => 'image.gif' },
}
HTTParty::Parsers::XML.parse(product_xml)["product"].should == expected_product_hash
end
it "should handle unescaping from xml (ActiveResource Compatible)" do
xml_string = '<person><bare-string>First &amp; Last Name</bare-string><pre-escaped-string>First &amp;amp; Last Name</pre-escaped-string></person>'
expected_hash = {
'bare_string' => 'First & Last Name',
'pre_escaped_string' => 'First &amp; Last Name'
}
HTTParty::Parsers::XML.parse(xml_string)['person'].should == expected_hash
end
end