mwunsch / prism

A Ruby microformat parser and HTML toolkit powered by Nokogiri
MIT License
63 stars 9 forks source link

Prism::Microformat::HCard returns anonymous class objects which can't be converted to YAML #2

Closed taazza closed 14 years ago

taazza commented 14 years ago

From an irb session :-

vcard = Prism.find(open('<some_linkedin_profile>'), :hcard)
vcard.second   # Returns Prism::Microformat::HCard object -> {:title=>"Title_string", :org=>{:organization_name=>"The Organization Name"}}
current = vcard.second[:org] # This object's type is an anonymous class
current.to_yaml
TypeError: can't dump anonymous class Class
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:41:in `node_export'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:41:in `add'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:41:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:40:in `each'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:40:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:39:in `map'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:39:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `call'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `emit'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `quick_emit'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:38:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:18:in `node_export'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:18:in `add'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:18:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:17:in `each'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:17:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:16:in `map'
        from /usr/lib64/ruby/1.8/yaml/rubytypes.rb:16:in `to_yaml'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `call'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `emit'
        from /usr/lib64/ruby/1.8/yaml.rb:391:in `quick_emit'

We are working around this issue by doing an inspect on current and then serializing it. We wanted to know whether this issue is something that can be fixed at your end.

mwunsch commented 14 years ago

Yes, sub-properties of POSH formats are of an anonymous class which itself inherits from POSH. It's possible to create a #to_yaml method of POSH objects to help this along. Can you post (in a gist, or in this issue) how you've been converting your objects to YAML. In all likelihood I will take a similar approach.

Thanks.

(sorry for the redundancy)

taazza commented 14 years ago

We have been using 'current.inspect.to_yaml' to do the conversion. From posh/base.rb, I see that calling 'inspect' internally calls the 'to_h' method to generate a Hash and returns its inspect. I tried this snippet in the irb and it seems to work.

class Prism::POSH::Base
  def to_yaml
    to_h.to_yaml
  end
end

Thanks

mwunsch commented 14 years ago

This technique works, but when the element increases in complexity, I get this. Is there a URL you've been testing on that I can test against? Not sure what's going on here.

ArgumentError: wrong number of arguments (1 for 0)
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:110:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:110:in `node_export'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:110:in `add'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:110:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:109:in `each'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:109:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:108:in `seq'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:108:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `call'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `emit'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `quick_emit'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:107:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:41:in `node_export'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:41:in `add'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:41:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:40:in `each'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:40:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:39:in `map'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:39:in `to_yaml'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `call'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `emit'
/usr/local/lib/ruby/1.8/yaml.rb:391:in `quick_emit'
/usr/local/lib/ruby/1.8/yaml/rubytypes.rb:38:in `to_yaml'
./../../lib/prism/posh.rb:183:in `to_yaml'
mwunsch commented 14 years ago

very bizarre: http://gist.github.com/351862

mwunsch commented 14 years ago

Conversion to YAML and recursive parsing to_h. to_yaml dumps to_h representation. Closed by 5709d199142533e378c9475cf877b2723c7fcf96