Class: Brewscribe::Document
- Inherits:
-
Object
- Object
- Brewscribe::Document
- Defined in:
- lib/brewscribe/document.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#recipes ⇒ Object
Returns the value of attribute recipes.
-
#styles ⇒ Object
Returns the value of attribute styles.
Instance Method Summary collapse
- #clean_key(key) ⇒ Object
-
#initialize(options = {}) ⇒ Document
constructor
A new instance of Document.
- #parse_data ⇒ Object
- #parse_recipes(recipes) ⇒ Object
- #parse_styles(styles) ⇒ Object
- #xml_node_to_hash(node) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Document
Returns a new instance of Document.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/brewscribe/document.rb', line 7 def initialize = {} @recipes = [] @styles = [] if [:file] @raw_data = File.read [:file] elsif [:data] @raw_data = [:data] end parse_data end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
4 5 6 |
# File 'lib/brewscribe/document.rb', line 4 def hash @hash end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
4 5 6 |
# File 'lib/brewscribe/document.rb', line 4 def raw_data @raw_data end |
#recipes ⇒ Object
Returns the value of attribute recipes.
5 6 7 |
# File 'lib/brewscribe/document.rb', line 5 def recipes @recipes end |
#styles ⇒ Object
Returns the value of attribute styles.
5 6 7 |
# File 'lib/brewscribe/document.rb', line 5 def styles @styles end |
Instance Method Details
#clean_key(key) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/brewscribe/document.rb', line 86 def clean_key key extracted = key.to_s.match(/(F_(\w{1,2}_)?)?(_MOD_|.+)/)[3] if extracted == '_MOD_' return 'last_modified' else extracted.downcase end end |
#parse_data ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/brewscribe/document.rb', line 20 def parse_data @raw_data = HTMLEntities.new.decode(@raw_data) @xml = Nokogiri::XML(@raw_data, nil, 'UTF-8').xpath('/Selections/Data') @hash = xml_node_to_hash @xml.first if @hash[:recipe].class == Hash parse_recipes [@hash[:recipe]] else parse_recipes Array @hash[:recipe] end if @hash[:style].class == Hash parse_styles[@hash[:style]] else parse_styles Array @hash[:style] end self end |
#parse_recipes(recipes) ⇒ Object
40 41 42 43 44 |
# File 'lib/brewscribe/document.rb', line 40 def parse_recipes recipes recipes.each do |recipe_hash| @recipes << Recipe.new(recipe_hash) end end |
#parse_styles(styles) ⇒ Object
46 47 48 49 50 |
# File 'lib/brewscribe/document.rb', line 46 def parse_styles styles styles.each do |style_hash| @styles << Style.from_data(style_hash) end end |
#xml_node_to_hash(node) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/brewscribe/document.rb', line 52 def xml_node_to_hash node if node if node.element? if node.children.size > 0 result_hash = {} node.children.each do |child| result = xml_node_to_hash child property = clean_key child.name key = property.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 |