| Class | FrgmntsController |
| In: |
app/controllers/frgmnts_controller.rb
|
| Parent: | ApplicationController |
Provides key functionality of the Workbench.
Before I save a new creation, fill-in some data
# File app/controllers/frgmnts_controller.rb, line 36
36: def before_create_save(record)
37: do_save_logic(record)
38: record.usr_id = session[:usr_id]
39: end
Before I save a new update, fill-in some data
# File app/controllers/frgmnts_controller.rb, line 42
42: def before_update_save(record)
43: do_save_logic(record)
44: if (record.usr_id != session[:usr_id])
45: # Force the call of record.errors.add_to_base "You can only update your records ..."
46: # Via record.validate
47: # which is defined here: app/models/frgmnt.rb
48: record.name = "record_usr_id_ne_session_usr_id"
49: end #if
50: end
Only show frgmnts owned by the usr in the current session
# File app/controllers/frgmnts_controller.rb, line 31
31: def conditions_for_collection
32: ['frgmnts.usr_id = ?', [session[:usr_id]]]
33: end
Before any kind of save, do this. The logic here can be visualized as a 2D grid. On x-Axis we have types of input used to build the fragment: Parent Fragement, Input URL. On y-Axis we have types of Hpricot expressions used to scrape the input:
.search(), .at(), display-enumerable(), etc
This logic is implemented via a large case-statement. Each branch is triggered by a pair of variables:
- type of input (Input URL, Parent Fragement.) - type of Hpricot expression (.search(), .at(), display-enumerable(), etc )
When I wrote Edgar411.com I used nested case-statements. That is more DRY but harder to maintain. So here, I use just one case-statement which branches on and-ed variable-pairs. Also seeing the variables paired up makes it easier for me to visualize this as a 2D grid. The names of the 2 variables are:
- record.inputurl - record.exprtype.name
# File app/controllers/frgmnts_controller.rb, line 68
68: def do_save_logic(record)
69: # shorten the variable names
70: urlin = record.inputurl
71: expname = record.exprtype.name
72: ptxt = record.parent.frgtxt unless record.parent.nil?
73: a1 = record.arg1
74: a2 = record.arg2
75: a3 = record.arg3
76: a4 = record.arg4
77: case
78: when record.valid? == false
79: record.frgtxt = "<b>The Data In This Fragment Is Invalid.</b>"
80: when ((urlin != nil) and (expname == ".search()"))
81: hpricot_object = get_my_hp_elem(urlin)
82: record.frgtxt = hpricot_object.search(a1).to_html
83: when ((urlin == nil) and (expname == ".search()"))
84: hpricot_object = Hpricot(ptxt)
85: record.frgtxt = hpricot_object.search(a1).to_html
86: when ((urlin != nil) and (expname == ".search().first"))
87: hpricot_object = get_my_hp_elem(urlin)
88: record.frgtxt = hpricot_object.search(a1).first.to_html
89: when ((urlin == nil) and (expname == ".search().first"))
90: hpricot_object = Hpricot(ptxt)
91: record.frgtxt = hpricot_object.search(a1).first.to_html
92: when ((urlin != nil) and (expname == ".search().last"))
93: hpricot_object = get_my_hp_elem(urlin)
94: record.frgtxt = hpricot_object.search(a1).last.to_html
95: when ((urlin == nil) and (expname == ".search().last"))
96: hpricot_object = Hpricot(ptxt)
97: record.frgtxt = hpricot_object.search(a1).last.to_html
98: when ((urlin != nil) and (expname == ".search().prepend()"))
99: hpricot_object = get_my_hp_elem(urlin)
100: hpricot_object.search(a1).prepend(a2)
101: record.frgtxt = hpricot_object.to_html
102: when ((urlin == nil) and (expname == ".search().prepend()"))
103: hpricot_object = Hpricot(ptxt)
104: hpricot_object.search(a1).prepend(a2)
105: record.frgtxt = hpricot_object.to_html
106: when ((urlin != nil) and (expname == ".search().append()"))
107: hpricot_object = get_my_hp_elem(urlin)
108: hpricot_object.search(a1).append(a2)
109: record.frgtxt = hpricot_object.to_html
110: when ((urlin == nil) and (expname == ".search().append()"))
111: hpricot_object = Hpricot(ptxt)
112: hpricot_object.search(a1).append(a2)
113: record.frgtxt = hpricot_object.to_html
114: when ((urlin != nil) and (expname == ".search().wrap()"))
115: hpricot_object = get_my_hp_elem(urlin)
116: hpricot_object.search(a1).wrap(a2)
117: record.frgtxt = hpricot_object.to_html
118: when ((urlin == nil) and (expname == ".search().wrap()"))
119: hpricot_object = Hpricot(ptxt)
120: hpricot_object.search(a1).wrap(a2)
121: record.frgtxt = hpricot_object.to_html
122: when ((urlin != nil) and (expname == ".search().remove"))
123: hpricot_object = get_my_hp_elem(urlin)
124: hpricot_object.search(a1).remove
125: record.frgtxt = hpricot_object.to_html
126: when ((urlin == nil) and (expname == ".search().remove"))
127: hpricot_object = Hpricot(ptxt)
128: hpricot_object.search(a1).remove
129: record.frgtxt = hpricot_object.to_html
130: when ((urlin != nil) and (expname == ".at()"))
131: hpricot_object = get_my_hp_elem(urlin)
132: record.frgtxt = hpricot_object.at(a1).to_html
133: when ((urlin == nil) and (expname == ".at()"))
134: hpricot_object = Hpricot(ptxt)
135: record.frgtxt = hpricot_object.at(a1).to_html
136: when ((urlin != nil) and (expname == ".at().inner_html"))
137: hpricot_object = get_my_hp_elem(urlin)
138: record.frgtxt = hpricot_object.at(a1).inner_html
139: when ((urlin == nil) and (expname == ".at().inner_html"))
140: hpricot_object = Hpricot(ptxt)
141: record.frgtxt = hpricot_object.at(a1).inner_html
142: when ((urlin != nil) and (expname == ".at().swap()"))
143: hpricot_object = get_my_hp_elem(urlin)
144: hpricot_object.at(a1).swap(a2)
145: record.frgtxt = hpricot_object.to_html
146: when ((urlin == nil) and (expname == ".at().swap()"))
147: hpricot_object = Hpricot(ptxt)
148: hpricot_object.at(a1).swap(a2)
149: record.frgtxt = hpricot_object.to_html
150: when ((urlin != nil) and (expname == "display-enumerable()"))
151: my_html = get_my_html_from_open_uri(urlin)
152: record.frgtxt = get_my_hp_enum(my_html,a1)
153: when ((urlin == nil) and (expname == "display-enumerable()"))
154: my_html = ptxt
155: record.frgtxt = get_my_hp_enum(my_html,a1)
156: when ((urlin != nil) and (expname == "remove-comments"))
157: hpricot_object = get_my_hp_elem(urlin)
158: hpricot_object.search("*").each {|e| Hpricot.orphan_node(e) if e.comment?}
159: record.frgtxt = hpricot_object.to_html
160: when ((urlin == nil) and (expname == "remove-comments"))
161: hpricot_object = Hpricot(ptxt)
162: hpricot_object.search("*").each {|e| Hpricot.orphan_node(e) if e.comment?}
163: record.frgtxt = hpricot_object.to_html
164: when ((urlin != nil) and (expname == "display-comments"))
165: hpricot_object = get_my_hp_elem(urlin)
166: record.frgtxt = hpricot_object.search("*").map {|e| "<hr />#{e}" if e.comment?}.to_s
167: when ((urlin == nil) and (expname == "display-comments"))
168: hpricot_object = Hpricot(ptxt)
169: record.frgtxt = hpricot_object.search("*").map {|e| "<hr />#{e}" if e.comment?}.to_s
170: when ((urlin != nil) and (expname == "gsub(/old/,'new')-textnodes"))
171: hpricot_object = get_my_hp_elem(urlin)
172: old = Regexp.new(a1)
173: new = a2
174: hpricot_object.search("*").each {|e| (e.content = e.content.gsub(old, new)) if e.text? }
175: record.frgtxt = hpricot_object.to_html
176: when ((urlin == nil) and (expname == "gsub(/old/,'new')-textnodes"))
177: hpricot_object = Hpricot(ptxt)
178: old = Regexp.new(a1)
179: new = a2
180: hpricot_object.search("*").each {|e| (e.content = e.content.gsub(old, new)) if e.text? }
181: record.frgtxt = hpricot_object.to_html
182: when ((urlin != nil) and (expname == ".search().remove_attr()"))
183: hpricot_object = get_my_hp_elem(urlin)
184: hpricot_object.search(a1).remove_attr(a2)
185: record.frgtxt = hpricot_object.to_html
186: when ((urlin == nil) and (expname == ".search().remove_attr()"))
187: hpricot_object = Hpricot(ptxt)
188: hpricot_object.search(a1).remove_attr(a2)
189: record.frgtxt = hpricot_object.to_html
190: else
191: record.frgtxt = "<b>nil</b>"
192: end # case
193: end
Returns Hpricot-enumerable from inputs: (HTML, Hpricot-Scrape-Expression)
# File app/controllers/frgmnts_controller.rb, line 205
205: def get_my_hp_enum(h,scrapeexpr)
206: # Match something like this: "body div.gb2>a[@href*='google.com'],[3,8]"
207: rgxp = /(.*)?(\,)(\[)(\d+)(,)(\d+)(\])$/
208: # Parse scrapeexpr into 3 pieces.
209: # 1. Hpricot expression
210: # 2. Enumerable starting index
211: # 3. Enumerable size
212: ma = rgxp.match(scrapeexpr).to_a
213: hp_expr = ma[1]
214: e0 = ma[4].to_i # starting index
215: esize = ma[6].to_i # size of the Enumerable object
216:
217: # Debug Hpricot here.
218: en_expr = "[#{ma[4]},#{ma[6]}]"
219: hp_str = "Hpricot(h).search(hp_expr)#{en_expr}"
220: # eval() not available in production
221: # hp_enum = eval(hp_str)
222: # Debug Hpricot here.
223:
224: # Now do it the hard way since I cant use eval()
225: hp_enum2 = []
226: Hpricot(h).search(hp_expr).each do |e|
227: # I count 2 indices as I fill hp_enum2
228: # e0 tells me when to start filling hp_enum2
229: # esize tells me when to stop filling hp_enum2
230: e0 = e0 - 1
231: esize = (esize - 1) if (e0 < 0)
232: hp_enum2 << e if ((e0 < 0) and (esize > -1))
233: end
234:
235: # Now I have the data I want.
236: # I pretty it up by adding <hr /> and numbering each element in the enumerable object.
237: frgtxt = ""
238: d = -1
239: hp_enum2.each {|e| (d = d + 1); frgtxt << "<hr />#{d.to_s}<p />#{e.to_s}" }
240: return frgtxt
241: end