class Scrapework::Object
Base class for web data type rubocop:disable Metrics/ClassLength
Public Class Methods
belongs_to(object, options = {})
click to toggle source
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
# File lib/scrapework/object.rb, line 22 def self.belongs_to(object, options = {}) ivar = "@#{object}" mapped_method = "_mapped_#{object}" reflection_class = options.fetch(:class) { object.to_s.classify }.to_s attr_writer object define_method(object) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) attributes = __send__(mapped_method) return if attributes.nil? instance = reflection_class.constantize.new(attributes) instance_variable_set(ivar, instance) end reflections[object] = { type: 'belongs_to', class: reflection_class } end
has_many(objects, options = {})
click to toggle source
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
# File lib/scrapework/object.rb, line 47 def self.has_many(objects, options = {}) ivar = "@#{objects}" reflection_class = options.fetch(:class) do objects.to_s.singularize.classify end.to_s inverse_reflection = self.class.name.underscore attr_writer objects define_method(objects) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) instances = __send__("_mapped_#{objects}").map do |attributes| next if attributes.nil? instance = reflection_class.constantize.new(attributes) if instance.class.reflections.include?(inverse_reflection) instance.public_send("#{inverse_reflection}=", self) end instance end instance_variable_set(ivar, instances.compact) end reflections[objects] = { type: 'has_many', class: reflection_class } end
has_one(object, options = {})
click to toggle source
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
# File lib/scrapework/object.rb, line 80 def self.has_one(object, options = {}) ivar = "@#{object}" mapped_method = "_mapped_#{object}" reflection_class = options.fetch(:class) { object.to_s.classify }.to_s inverse_reflection = self.class.name.underscore attr_writer object define_method(object) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) attributes = __send__(mapped_method) return if attributes.nil? instance = reflection_class.constantize.new(attributes) if instance.class.reflections.include?(inverse_reflection) instance.public_send("#{inverse_reflection}=", self) end instance_variable_set(ivar, instance) end reflections[object] = { type: 'has_one', class: reflection_class } end
load(url)
click to toggle source
# File lib/scrapework/object.rb, line 144 def self.load(url) instance = new(url: url) instance.load instance end
map(name, &block)
click to toggle source
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
# File lib/scrapework/object.rb, line 129 def self.map(name, &block) mapped_method = :"_mapped_#{name}" define_method(mapped_method) do value = begin instance_exec(_document, &block) rescue StandardError => e raise MappingError, "failed to scrape `#{name}`: #{e.message}" end public_send("#{name}=", value) end private mapped_method end
paginate(&block)
click to toggle source
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
# File lib/scrapework/object.rb, line 109 def self.paginate(&block) mapped_method = :_pagination define_method(mapped_method) do @_pagination ||= instance_exec(_document, &block) end define_method(:prev_page) do pages = __send__(mapped_method) self.class.new(pages[0]) if pages[0] end define_method(:next_page) do pages = __send__(mapped_method) self.class.new(pages[1]) if pages[1] end end
reflections()
click to toggle source
# File lib/scrapework/object.rb, line 16 def self.reflections @reflections ||= {} end
Public Instance Methods
_document()
click to toggle source
# File lib/scrapework/object.rb, line 150 def _document @_document ||= Nokogiri::HTML(html) end
html()
click to toggle source
# File lib/scrapework/object.rb, line 154 def html uri = URI.parse(url) uri.read end
load()
click to toggle source
# File lib/scrapework/object.rb, line 159 def load attributes.except('url').each do |attribute, value| __send__("_mapped_#{attribute}") if value.nil? end end