Class: Creepin::Resource

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Resource

Returns a new instance of Resource.



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
# File 'lib/creepin/resource.rb', line 5

def initialize(name, options={}, &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(options[:params]) if options.has_key?(:params)
  @loaded_resource = options[:loaded_resource] if options.has_key?(:loaded_resource)
  
  class_name = "#{name.camelize}ResourceCreeper"
  klass = Class.new ResourceCreeper
  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 => :parse_response do
        transitions :from => :crawling, :to => :finished
      end
      
    end
    
    dsl_methods.each do |sym|
      define_method(sym.to_s) {
        settings[sym]
      }
    end
    
  end
  
end

Instance Method Details

#base_url(string) ⇒ Object



52
53
54
# File 'lib/creepin/resource.rb', line 52

def base_url(string)
  @config[:base_url] = string
end

#default_params(hash) ⇒ Object



56
57
58
# File 'lib/creepin/resource.rb', line 56

def default_params(hash)
  @config[:default_params] = hash
end

#define_element_mapping(attr_name, &block) ⇒ Object



68
69
70
71
# File 'lib/creepin/resource.rb', line 68

def define_element_mapping(attr_name, &block)
  @config[:element_mappings] ||= {}
  @config[:element_mappings][attr_name] = block
end

#resource_class(string) ⇒ Object



73
74
75
# File 'lib/creepin/resource.rb', line 73

def resource_class(string)
  @config[:resource_class] = string
end

#resource_load_strategy(*options, &block) ⇒ Object



81
82
83
# File 'lib/creepin/resource.rb', line 81

def resource_load_strategy(*options, &block)
  @config[:resource_load_strategy] = Proc.new(*options, &block)
end

#resource_save_strategy(*options, &block) ⇒ Object



85
86
87
# File 'lib/creepin/resource.rb', line 85

def resource_save_strategy(*options, &block)
  @config[:resource_save_strategy] = Proc.new(*options, &block)
end

#selector(string) ⇒ Object



64
65
66
# File 'lib/creepin/resource.rb', line 64

def selector(string)
  @config[:selector] = string
end

#skip_resource_save(bool) ⇒ Object



77
78
79
# File 'lib/creepin/resource.rb', line 77

def skip_resource_save(bool)
  @config[:skip_resource_save] = bool.present? ? bool : false
end

#url_attribute(sym) ⇒ Object



60
61
62
# File 'lib/creepin/resource.rb', line 60

def url_attribute(sym)
  @config[:url_attribute] = sym.to_sym
end