spec/happymapper_spec.rb in happymapper-0.2.0 vs spec/happymapper_spec.rb in happymapper-0.2.1

- old
+ new

@@ -1,7 +1,8 @@ require File.dirname(__FILE__) + '/spec_helper.rb' require 'pp' +require 'uri' class Feature include HappyMapper element :name, String, :tag => '.|.//text()' end @@ -36,11 +37,11 @@ end class FamilyTree include HappyMapper - tag 'familytree', :root => true + tag 'familytree' attribute :version, String attribute :status_message, String, :tag => 'statusMessage' attribute :status_code, String, :tag => 'statusCode' has_one :persons, Persons end @@ -49,10 +50,11 @@ module FedEx class Address include HappyMapper tag 'Address' + namespace 'v2' element :city, String, :tag => 'City' element :state, String, :tag => 'StateOrProvinceCode' element :zip, String, :tag => 'PostalCode' element :countrycode, String, :tag => 'CountryCode' element :residential, Boolean, :tag => 'Residential' @@ -60,28 +62,31 @@ class Event include HappyMapper tag 'Events' + namespace 'v2' element :timestamp, String, :tag => 'Timestamp' element :eventtype, String, :tag => 'EventType' element :eventdescription, String, :tag => 'EventDescription' has_one :address, Address end class PackageWeight include HappyMapper tag 'PackageWeight' + namespace 'v2' element :units, String, :tag => 'Units' element :value, Integer, :tag => 'Value' end class TrackDetails include HappyMapper tag 'TrackDetails' + namespace 'v2' element :tracking_number, String, :tag => 'TrackingNumber' element :status_code, String, :tag => 'StatusCode' element :status_desc, String, :tag => 'StatusDescription' element :carrier_code, String, :tag => 'CarrierCode' element :service_info, String, :tag => 'ServiceInfo' @@ -92,10 +97,11 @@ class Notification include HappyMapper tag 'Notifications' + namespace 'v2' element :severity, String, :tag => 'Severity' element :source, String, :tag => 'Source' element :code, Integer, :tag => 'Code' element :message, String, :tag => 'Message' element :localized_message, String, :tag => 'LocalizedMessage' @@ -103,17 +109,19 @@ class TransactionDetail include HappyMapper tag 'TransactionDetail' + namespace 'v2' element :cust_tran_id, String, :tag => 'CustomerTransactionId' end class TrackReply include HappyMapper - tag 'TrackReply', :root => true + tag 'TrackReply' + namespace 'v2' element :highest_severity, String, :tag => 'HighestSeverity' element :more_data, Boolean, :tag => 'MoreData' has_many :notifications, Notification, :tag => 'Notifications' has_many :trackdetails, TrackDetails, :tag => 'TrackDetails' has_one :tran_detail, TransactionDetail, :tab => 'TransactionDetail' @@ -165,41 +173,48 @@ element :source, String element :truncated, Boolean element :in_reply_to_status_id, Integer element :in_reply_to_user_id, Integer element :favorited, Boolean + element :non_existent, String, :tag => 'dummy', :namespace => 'fake' has_one :user, User end class CurrentWeather include HappyMapper tag 'ob' + namespace 'aws' element :temperature, Integer, :tag => 'temp' element :feels_like, Integer, :tag => 'feels-like' element :current_condition, String, :tag => 'current-condition', :attributes => {:icon => String} end class Address include HappyMapper - tag 'address', :root => true + tag 'address' element :street, String element :postcode, String element :housenumber, String element :city, String element :country, String end +# for type coercion +class ProductGroup < String; end + module PITA class Item include HappyMapper tag 'Item' # if you put class in module you need tag element :asin, String, :tag => 'ASIN' - element :detail_page_url, String, :tag => 'DetailPageURL' + element :detail_page_url, URI, :tag => 'DetailPageURL', :parser => :parse element :manufacturer, String, :tag => 'Manufacturer', :deep => true + element :point, String, :tag => 'point', :namespace => 'georss' + element :product_group, ProductGroup, :tag => 'ProductGroup', :deep => true, :parser => :new, :raw => true end class Items include HappyMapper @@ -212,11 +227,11 @@ module GitHub class Commit include HappyMapper - tag "commit", :root => true + tag "commit" element :url, String element :tree, String element :message, String element :id, String element :'committed-date', Date @@ -288,11 +303,11 @@ element = Foo.elements.first element.name.should == 'users' element.type.should == User element.options[:single] = false end - + it "should default tag name to lowercase class" do Foo.get_tag_name.should == 'foo' end it "should default tag name of class in modules to the last constant lowercase" do @@ -303,10 +318,15 @@ it "should allow setting tag name" do Foo.tag('FooBar') Foo.get_tag_name.should == 'FooBar' end + it "should allow setting a namespace" do + Foo.namespace(namespace = "foo") + Foo.namespace.should == namespace + end + it "should provide #parse" do Foo.should respond_to(:parse) end end @@ -318,11 +338,11 @@ end describe "#elements" do it "should only return elements for the current class" do Post.elements.size.should == 0 - Status.elements.size.should == 9 + Status.elements.size.should == 10 end end it "should parse xml attributes into ruby objects" do posts = Post.parse(fixture_file('posts.xml')) @@ -374,11 +394,14 @@ items.total_results.should == 22 items.total_pages.should == 3 first = items.items[0] second = items.items[1] first.asin.should == '0321480791' - first.detail_page_url.should == 'http://www.amazon.com/gp/redirect.html%3FASIN=0321480791%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0321480791%253FSubscriptionId=dontbeaswoosh' + first.point.should == '38.5351715088 -121.7948684692' + first.detail_page_url.should be_a_kind_of(URI) + first.detail_page_url.to_s.should == 'http://www.amazon.com/gp/redirect.html%3FASIN=0321480791%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0321480791%253FSubscriptionId=dontbeaswoosh' first.manufacturer.should == 'Addison-Wesley Professional' + first.product_group.should == '<ProductGroup>Book</ProductGroup>' second.asin.should == '047022388X' second.manufacturer.should == 'Wrox' end it "should parse xml that has attributes of elements" do