Class: AMEE::Data::DrillDown
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 inherited from Object
#expire_cache, get_and_parse
#load_xml_doc, #node_value, #xmlpathpreamble
Constructor Details
#initialize(data = {}) ⇒ DrillDown
Returns a new instance of DrillDown.
10
11
12
13
14
15
16
|
# File 'lib/amee/drill_down.rb', line 10
def initialize(data = {})
@choices = data ? data[:choices] : []
@choice_name = data[:choice_name]
@selections = data ? data[:selections] : []
raise AMEE::ArgumentError.new('No choice_name specified') if @choice_name.nil?
super
end
|
Instance Attribute Details
#choice_name ⇒ Object
Returns the value of attribute choice_name.
19
20
21
|
# File 'lib/amee/drill_down.rb', line 19
def choice_name
@choice_name
end
|
Returns the value of attribute choices.
18
19
20
|
# File 'lib/amee/drill_down.rb', line 18
def choices
@choices
end
|
#selections ⇒ Object
Returns the value of attribute selections.
20
21
22
|
# File 'lib/amee/drill_down.rb', line 20
def selections
@selections
end
|
Class Method Details
.from_json(json) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/amee/drill_down.rb', line 41
def self.from_json(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
DrillDown.new(data)
rescue Exception
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/amee/drill_down.rb', line 66
def self.from_xml(xml)
@doc = load_xml_doc(xml)
data = {}
data[:choice_name] = x('Choices/Name') || x('Choices/name')
data[:choices] = [x('Choices/Choices/Choice/Value') || x('Choices/Choice/value')].flatten
names = x('Selections/Choice/Name') || x('Selections/Choice/name')
values = x('Selections/Choice/Value') || x('Selections/Choice/value')
data[:selections] = names && values ? Hash[*([names].flatten.zip([values].flatten)).flatten] : {}
DrillDown.new(data)
rescue Exception
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)
response = connection.get(path).body
if response.is_json?
drill = DrillDown.from_json(response)
else
drill = DrillDown.from_xml(response)
end
drill.path = path.split('?')[0].gsub(/^\/data/, '')
drill.connection = connection
return drill
rescue Exception
raise AMEE::BadData.new("Couldn't load DrillDown resource. Check that your URL is correct (#{path}).\n#{response}")
end
|
.xmlpathpreamble ⇒ Object
62
63
64
|
# File 'lib/amee/drill_down.rb', line 62
def self.xmlpathpreamble
'/Resources/DrillDownResource/'
end
|
Instance Method Details
#choose(choice) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/amee/drill_down.rb', line 31
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_uid ⇒ Object
26
27
28
29
|
# File 'lib/amee/drill_down.rb', line 26
def data_item_uid
return nil if @choice_name != 'uid' || @choices.size != 1
@choices[0]
end
|
#path=(path) ⇒ Object
22
23
24
|
# File 'lib/amee/drill_down.rb', line 22
def path=(path)
@path = path
end
|