Created
March 16, 2012 19:02
-
-
Save cadwallion/2051865 to your computer and use it in GitHub Desktop.
Nokogiri::XML to Object properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'ostruct' | |
| require 'nokogiri' | |
| module Brewscribe | |
| class Recipe | |
| attr_reader :raw_data, :hash | |
| def initialize raw_data | |
| @raw_data = raw_data | |
| parse_raw_data | |
| end | |
| def parse_raw_data | |
| @xml = Nokogiri::XML(@raw_data).xpath('/Selections/Data/Recipe') | |
| @hash = xml_node_to_hash(@xml.first) | |
| @hash.keys.each do |key| | |
| property = clean_key key | |
| self.class.module_eval do | |
| attr_accessor property.to_sym | |
| end | |
| self.send "#{property}=", @hash[key] | |
| end | |
| end | |
| def clean_key key | |
| key.to_s.match(/(F_(\w{1,2}_)?)?(_MOD_|.+)/)[3].downcase | |
| end | |
| def xml_node_to_hash node | |
| if node.element? | |
| if node.children.size > 0 | |
| result_hash = {} | |
| node.children.each do |child| | |
| result = xml_node_to_hash child | |
| key = child.name.to_sym | |
| if child.name == 'text' | |
| return result if !child.next && !child.previous | |
| elsif result_hash[key] | |
| if result_hash[key].is_a? Array | |
| result_hash[key] << result | |
| else | |
| result_hash[key] = [result_hash[key]] << result | |
| end | |
| else | |
| result_hash[key] = result | |
| end | |
| end | |
| return result_hash | |
| else | |
| return nil | |
| end | |
| else | |
| return node.content.to_s | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment