Class: AMEE::Data::DrillDown

Inherits:
Object show all
Defined in:
lib/amee/drill_down.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#path

Attributes inherited from Object

#connection, #created, #modified, #name, #path, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#full_path

Methods included from ParseHelper

#xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ DrillDown

Returns a new instance of DrillDown.



7
8
9
10
11
12
# File 'lib/amee/drill_down.rb', line 7

def initialize(data = {})
  @choices = data ? data[:choices] : []
  @choice_name = data[:choice_name]
  @selections = data ? data[:selections] : []
  super
end

Instance Attribute Details

#choice_nameObject (readonly)

Returns the value of attribute choice_name.



15
16
17
# File 'lib/amee/drill_down.rb', line 15

def choice_name
  @choice_name
end

#choicesObject (readonly)

Returns the value of attribute choices.



14
15
16
# File 'lib/amee/drill_down.rb', line 14

def choices
  @choices
end

#selectionsObject (readonly)

Returns the value of attribute selections.



16
17
18
# File 'lib/amee/drill_down.rb', line 16

def selections
  @selections
end

Class Method Details

.from_json(json) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/amee/drill_down.rb', line 37

def self.from_json(json)
  # Parse json
  doc = JSON.parse(json)
  data = {}
  data[:choice_name] = doc['choices']['name']
  choices = []
  doc['choices']['choices'].each do |c|
    choices << c['value']
  end
  data[:choices] = choices
  selections = {}
  doc['selections'].each do |c|
    selections[c['name']] = c['value']
  end
  data[:selections] = selections
  # Create object
  DrillDown.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load DrillDown resource from JSON data. Check that your URL is correct.\n#{json}")
end

.from_xml(xml) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/amee/drill_down.rb', line 58

def self.from_xml(xml)
  # Parse XML
  doc = REXML::Document.new(xml)
  data = {}
  data[:choice_name] = REXML::XPath.first(doc, "/Resources/DrillDownResource/Choices/?ame").text
  choices = []
  REXML::XPath.each(doc, "/Resources/DrillDownResource/Choices//Choice") do |c|
    choices << (c.elements['Value'] || c.elements['value']).text
  end
  data[:choices] = choices
  selections = {}
  REXML::XPath.each(doc, "/Resources/DrillDownResource/Selections/Choice") do |c|
    name = (c.elements['Name'] || c.elements['name']).text
    value = (c.elements['Value'] || c.elements['value']).text
    selections[name] = value
  end
  data[:selections] = selections
  # Create object
  DrillDown.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load DrillDown resource from XML data. Check that your URL is correct.\n#{xml}")
end

.get(connection, path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/amee/drill_down.rb', line 81

def self.get(connection, path)
  # Load data from path
  response = connection.get(path).body
  # Parse data from response
  if response.is_json?
    drill = DrillDown.from_json(response)
  else
    drill = DrillDown.from_xml(response)
  end
  # Store path in drill object - response does not include it
  drill.path = path.split('?')[0].gsub(/^\/data/, '')
  # Store connection in object for future use
  drill.connection = connection
  # Done
  return drill
rescue
  raise AMEE::BadData.new("Couldn't load DrillDown resource. Check that your URL is correct (#{path}).\n#{response}")
end

Instance Method Details

#choose(choice) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/amee/drill_down.rb', line 27

def choose(choice)
  options = []
  @selections.each_pair do |key, value|
    options << "#{CGI::escape(key)}=#{CGI::escape(value)}"
  end
  options << "#{CGI::escape(@choice_name)}=#{CGI::escape(choice)}"
  query_string = options.join "&"        
  DrillDown.get(connection, "#{full_path}?#{query_string}")
end

#data_item_uidObject



22
23
24
25
# File 'lib/amee/drill_down.rb', line 22

def data_item_uid
  return nil if @choice_name != 'uid' || @choices.size != 1
  @choices[0]
end

#path=(path) ⇒ Object



18
19
20
# File 'lib/amee/drill_down.rb', line 18

def path=(path)
  @path = path
end