Class: Creepin::Collection
- Inherits:
-
Object
- Object
- Creepin::Collection
- Defined in:
- lib/creepin/collection.rb
Instance Method Summary collapse
- #after(event_name, &block) ⇒ Object
- #base_url(string) ⇒ Object
- #default_params(hash) ⇒ Object
- #define_element_mapping(attr_name, &block) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Collection
constructor
A new instance of Collection.
- #next_page_selector(string = '', &block) ⇒ Object
- #resource_class(string) ⇒ Object
- #resource_load_strategy(*options, &block) ⇒ Object
- #resource_save_strategy(*options, &block) ⇒ Object
- #selector(string) ⇒ Object
- #skip_resource_save(bool) ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Collection
Returns a new instance of Collection.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/creepin/collection.rb', line 4 def initialize(name, ={}, &block) @config = {} @request_params = {} instance_eval(&block) settings = @config.dup.merge(namespace: name) @request_params.merge(settings[:default_params]) if settings.has_key?(:default_params) @request_params.reverse_merge([:params]) if .has_key?(:params) class_name = "#{name.camelize}CollectionCreeper" puts class_name klass = Class.new CollectionCreeper dsl_methods = @config.keys @creeper_class = Object.const_set(class_name, klass) #seems to be a bug with AASM and inheritence, this should be in collection crawler file @creeper_class.class_eval do include AASM aasm do state :waiting, :initial => true state :crawling state :parsing state :finished state :finished_and_loaded event :crawl, :after => :transmit do transitions :from => :waiting, :to => :crawling end event :crawl_finished, :after => :run_after_crawl_finished_callbacks do transitions :from => :crawling, :to => :finished end event :crawl_next, :after => :crawl_next_page do transitions :from => :finished, :to => :waiting end event :collection_loaded, :after => :run_after_collection_loaded_callbacks do transitions :from => [:waiting, :finished], :to => :finished_and_loaded end end dsl_methods.each do |sym| define_method(sym.to_s) { settings[sym] } end end end |
Instance Method Details
#after(event_name, &block) ⇒ Object
97 98 99 100 |
# File 'lib/creepin/collection.rb', line 97 def after(event_name, &block) @config["after_#{event_name.to_s}_callbacks".to_sym] ||= [] @config["after_#{event_name.to_s}_callbacks".to_sym] << block end |
#base_url(string) ⇒ Object
59 60 61 |
# File 'lib/creepin/collection.rb', line 59 def base_url(string) @config[:base_url] = string end |
#default_params(hash) ⇒ Object
63 64 65 |
# File 'lib/creepin/collection.rb', line 63 def default_params(hash) @config[:default_params] = hash end |
#define_element_mapping(attr_name, &block) ⇒ Object
71 72 73 74 |
# File 'lib/creepin/collection.rb', line 71 def define_element_mapping(attr_name, &block) @config[:element_mappings] ||= {} @config[:element_mappings][attr_name] = block end |
#next_page_selector(string = '', &block) ⇒ Object
80 81 82 83 |
# File 'lib/creepin/collection.rb', line 80 def next_page_selector(string='', &block) @config[:next_page_selector] = string unless string.nil? @config[:next_page_selector] = block if block end |
#resource_class(string) ⇒ Object
76 77 78 |
# File 'lib/creepin/collection.rb', line 76 def resource_class(string) @config[:resource_class] = string end |
#resource_load_strategy(*options, &block) ⇒ Object
89 90 91 |
# File 'lib/creepin/collection.rb', line 89 def resource_load_strategy(*, &block) @config[:resource_load_strategy] = Proc.new(*, &block) end |
#resource_save_strategy(*options, &block) ⇒ Object
93 94 95 |
# File 'lib/creepin/collection.rb', line 93 def resource_save_strategy(*, &block) @config[:resource_save_strategy] = Proc.new(*, &block) end |
#selector(string) ⇒ Object
67 68 69 |
# File 'lib/creepin/collection.rb', line 67 def selector(string) @config[:selector] = string end |
#skip_resource_save(bool) ⇒ Object
85 86 87 |
# File 'lib/creepin/collection.rb', line 85 def skip_resource_save(bool) @config[:skip_resource_save] = bool.present? ? bool : false end |