Class: Abebooks4r::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/abebooks4r.rb

Overview

Internal wrapper class to provide convenient method to access Hpricot element value.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Pass Hpricot::Elements object



149
150
151
# File 'lib/abebooks4r.rb', line 149

def initialize(element)
  @element = element
end

Class Method Details

.get(element, path = '') ⇒ Object

Similar to #get, except an element object must be passed-in.



196
197
198
199
200
201
# File 'lib/abebooks4r.rb', line 196

def self.get(element, path='')
  return unless element
  result = element.at(path)
  result = result.inner_html if result
  result
end

.get_array(element, path = '') ⇒ Object

Similar to #get_array, except an element object must be passed-in.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/abebooks4r.rb', line 210

def self.get_array(element, path='')
      return unless element
      
      result = element/path
      if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
        parsed_result = []
        result.each {|item|
          parsed_result << Element.get(item)
        }
        parsed_result
      else
        [Element.get(result)]
      end
end

.get_hash(element, path = '') ⇒ Object

Similar to #get_hash, except an element object must be passed-in.



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/abebooks4r.rb', line 226

def self.get_hash(element, path='')
  return unless element
    
  result = element.at(path)
  if result
    hash = {}
    result = result.children
    result.each do |item|
      hash[item.name.to_sym] = item.inner_html
    end 
    hash
  end
end

.get_unescaped(element, path = '') ⇒ Object

Similar to #get_unescaped, except an element object must be passed-in.



204
205
206
207
# File 'lib/abebooks4r.rb', line 204

def self.get_unescaped(element, path='')
  result = get(element, path)
  CGI::unescapeHTML(result) if result
end

Instance Method Details

#/(path) ⇒ Object

Find Hpricot::Elements matching the given path. Example: element/“author”.



159
160
161
162
163
# File 'lib/abebooks4r.rb', line 159

def /(path)
  elements = @element/path
  return nil if elements.size == 0
  elements
end

#elemObject

Returns Hpricot::Elments object



154
155
156
# File 'lib/abebooks4r.rb', line 154

def elem
  @element
end

#get(path = '') ⇒ Object

Get the text value of the given path, leave empty to retrieve current element value.



176
177
178
# File 'lib/abebooks4r.rb', line 176

def get(path='')
  Element.get(@element, path)
end

#get_array(path = '') ⇒ Object

Get the array values of the given path.



186
187
188
# File 'lib/abebooks4r.rb', line 186

def get_array(path='')
  Element.get_array(@element, path)
end

#get_hash(path = '') ⇒ Object

Get the children element text values in hash format with the element names as the hash keys.



191
192
193
# File 'lib/abebooks4r.rb', line 191

def get_hash(path='')
  Element.get_hash(@element, path)
end

#get_unescaped(path = '') ⇒ Object

Get the unescaped HTML text of the given path.



181
182
183
# File 'lib/abebooks4r.rb', line 181

def get_unescaped(path='')
  Element.get_unescaped(@element, path)
end

#search_and_convert(path) ⇒ Object

Find Hpricot::Elements matching the given path, and convert to Abebooks4r::Element. Returns an array Abebooks4r::Elements if more than Hpricot::Elements size is greater than 1.



167
168
169
170
171
172
173
# File 'lib/abebooks4r.rb', line 167

def search_and_convert(path)
  elements = self./(path)
  return unless elements
  elements = elements.map{|element| Element.new(element)}
  return elements.first if elements.size == 1
  elements
end

#to_sObject



240
241
242
# File 'lib/abebooks4r.rb', line 240

def to_s
  elem.to_s if elem
end