Class: Sevendigital::Digestor

Inherits:
Object
  • Object
show all
Defined in:
lib/sevendigital/digestion_tract/digestor.rb

Overview

internal generic class used for digesting XML responses from the API

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Digestor

TODO TEST THIS CRAP



9
10
11
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 9

def initialize(api_client)
  @api_client = api_client
end

Instance Method Details

#content_present?(proxy_node) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 60

def content_present?(proxy_node)
  !proxy_node.nil?
end

#from_xml_string(xml, element_name = default_element_name) ⇒ Object



13
14
15
16
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 13

def from_xml_string(xml, element_name = default_element_name)
  xml_doc = Nokogiri::XML(xml)
  from_xml_doc(xml_doc.at_xpath("./#{element_name}"))
end

#get_optional_attribute(node, attribute_name) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 109

def get_optional_attribute(node, attribute_name)
  if node.at_xpath("@#{attribute_name}") then
    content = node.at_xpath("@#{attribute_name}").content
    return yield(content) if block_given?
    return content
  end
  nil
end

#get_optional_node(node, element_name) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 100

def get_optional_node(node, element_name)
  if node.at_xpath("./#{element_name}") then
    subnode = node.at_xpath("./#{element_name}")
    return yield(subnode) if block_given?
    return subnode
  end
  nil
end

#get_optional_value(node, element_name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 91

def get_optional_value(node, element_name)
  if node.at_xpath("./#{element_name}") then
    content = node.at_xpath("./#{element_name}").content
    return yield(content) if block_given?
    return content
  end
  nil
end

#get_required_attribute(node, attribute_name) ⇒ Object

Raises:



82
83
84
85
86
87
88
89
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 82

def get_required_attribute(node, attribute_name)
  if node.at_xpath("@#{attribute_name}") then
    content = node.at_xpath("@#{attribute_name}").content
    return yield(content) if block_given?
    return content
  end
  raise DigestiveProblem, "I need #{attribute_name} attribute to digest the response"
end

#get_required_node(node, element_name) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 73

def get_required_node(node, element_name)
  if node.at_xpath("./#{element_name}") then
    subnode = node.at_xpath("./#{element_name}")
    return yield(subnode) if block_given?
    return subnode
  end
  raise DigestiveProblem, "I need #{element_name} element to digest the response"
end

#get_required_value(node, element_name) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 64

def get_required_value(node, element_name)
  if node.at_xpath("./#{element_name}") then
    content = node.at_xpath("./#{element_name}").content
    return yield(content) if block_given?
    return content
  end
  raise DigestiveProblem, "I need #{element_name} element to digest the response"
end

#list_from_xml_doc(list_node) ⇒ Object



23
24
25
26
27
28
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 23

def list_from_xml_doc(list_node)
  make_sure_eating_nokogiri_node(list_node)
  list = []
  list_node.xpath("./#{default_element_name}").each { |node| list << from_xml_doc(node)}
  paginate_results(list_node, list)
end

#list_from_xml_string(xml, list_element_name = default_list_element_name) ⇒ Object



18
19
20
21
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 18

def list_from_xml_string(xml, list_element_name = default_list_element_name)
  xml_doc = Nokogiri::XML(xml)
  list_from_xml_doc(xml_doc.at_xpath("./#{list_element_name}"))
end

#make_sure_eating_nokogiri_node(xml) ⇒ Object

Raises:



51
52
53
54
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 51

def make_sure_eating_nokogiri_node(xml)
  raise DigestiveProblem, "There's nothing i can digest" unless xml
  raise DigestiveProblem, "I'm not eating this! It's not a Nokogiri XML node.'" unless xml.kind_of?(Nokogiri::XML::Node)
end

#nested_list_from_xml_doc(list_node, list_element_name = default_list_element_name, element_name = default_element_name) ⇒ Object



38
39
40
41
42
43
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 38

def nested_list_from_xml_doc(list_node, list_element_name = default_list_element_name, element_name = default_element_name)
  make_sure_eating_nokogiri_node(list_node)
  list = []
  list_node.xpath("./#{list_element_name}/#{element_name}").each { |node| list << from_xml_doc(node)}
  paginate_results(list_node, list)
end

#nested_list_from_xml_string(xml, container_element_name, list_element_name = default_list_element_name) ⇒ Object

nested parsing for api methods that return standard object inside containers with no additional (useful) information e.g. tagged_item.artist, recommendation.release, search_result.track, etc



33
34
35
36
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 33

def nested_list_from_xml_string(xml, container_element_name, list_element_name = default_list_element_name)
  xml_doc = Nokogiri::XML(xml)
  nested_list_from_xml_doc(xml_doc.at_xpath("./#{container_element_name}"), list_element_name)
end

#paginate_results(results_xml_node, list) ⇒ Object



45
46
47
48
49
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 45

def paginate_results(results_xml_node, list)
  pager = @api_client.pager_digestor.from_xml_doc(results_xml_node)
  return list if !pager
  pager.paginate_list(list)
end

#value_present?(proxy_node) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sevendigital/digestion_tract/digestor.rb', line 56

def value_present?(proxy_node)
  !proxy_node.nil? &&  !proxy_node.value.nil? && !proxy_node.value.empty?
end