Class: Datahen::Client::ScraperJob

Inherits:
Base
  • Object
show all
Defined in:
lib/datahen/client/scraper_job.rb

Constant Summary

Constants inherited from Base

Base::CHECK_EMPTY_BODY, Base::CHECK_NIL, Base::DEFAULT_RETRY_LIMIT

Instance Method Summary collapse

Methods inherited from Base

#auth_token, #auth_token=, #default_retry_limit, #env_api_url, env_auth_token, env_ignore_ssl, #ignore_ssl, #initialize, #left_merge, random_delay, #retry

Constructor Details

This class inherits a constructor from Datahen::Client::Base

Instance Method Details

#all(scraper_name, opts = {}) ⇒ Object



4
5
6
7
# File 'lib/datahen/client/scraper_job.rb', line 4

def all(scraper_name, opts={})
  params = @options.merge(opts)
  self.class.get("/scrapers/#{scraper_name}/jobs", params)
end

#cancel(scraper_name, opts = {}) ⇒ Object



63
64
65
66
# File 'lib/datahen/client/scraper_job.rb', line 63

def cancel(scraper_name, opts={})
  opts[:status] = 'cancelled'
  update(scraper_name, opts)
end

#create(scraper_name, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/datahen/client/scraper_job.rb', line 9

def create(scraper_name, opts={})
  body = {}
  body[:parser_worker_count] = opts[:parsers] if opts[:parsers]
  body[:fetcher_worker_count] = opts[:fetchers] if opts[:fetchers]
  body[:browser_worker_count] = opts[:browsers] if opts[:browsers]
  body[:proxy_type] = opts[:proxy_type] if opts[:proxy_type]
  body[:max_page_size] = opts[:max_page_size] if opts[:max_page_size]
  body[:enable_global_cache] = opts[:enable_global_cache] if opts.has_key?("enable_global_cache") || opts.has_key?(:enable_global_cache)
  body[:retry_interval] = opts[:retry_interval] if opts[:retry_interval]
  body[:soft_fetching_try_limit] = opts[:soft_fetching_try_limit] if opts[:soft_fetching_try_limit]
  body[:soft_refetch_limit] = opts[:soft_refetch_limit] if opts[:soft_refetch_limit]
  body[:parsing_try_limit] = opts[:parsing_try_limit] if opts[:parsing_try_limit]
  body[:prevent_kb_autoscaler] = opts[:prevent_kb_autoscaler] if opts.has_key?("prevent_kb_autoscaler") || opts.has_key?(:prevent_kb_autoscaler)
  body[:deletion_protected] = opts[:deletion_protected] if opts.has_key?("deletion_protected") || opts.has_key?(:deletion_protected)
  if opts[:vars]
    if opts[:vars].is_a?(Array)
      body[:vars] = opts[:vars]
    elsif opts[:vars].is_a?(String)
      body[:vars] = JSON.parse(opts[:vars])
    end
  end
  params = @options.merge({body: body.to_json})
  self.class.post("/scrapers/#{scraper_name}/jobs", params)
end

#delete(scraper_name, opts = {}) ⇒ Object



84
85
86
87
# File 'lib/datahen/client/scraper_job.rb', line 84

def delete(scraper_name, opts={})
  params = @options.merge(opts)
  self.class.delete("/scrapers/#{scraper_name}/current_job", params)
end

#find(scraper_name, opts = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/datahen/client/scraper_job.rb', line 34

def find(scraper_name, opts={})
  if opts[:live]
    self.class.get("/scrapers/#{scraper_name}/current_job", @options)
  else
    self.class.get("/cached/scrapers/#{scraper_name}/current_job", @options)
  end
end

#pause(scraper_name, opts = {}) ⇒ Object



73
74
75
76
# File 'lib/datahen/client/scraper_job.rb', line 73

def pause(scraper_name, opts={})
  opts[:status] = 'paused'
  update(scraper_name, opts)
end

#profile(scraper_name, opts = {}) ⇒ Object



78
79
80
81
82
# File 'lib/datahen/client/scraper_job.rb', line 78

def profile(scraper_name, opts={})
  params = @options.merge(opts)

  self.class.get("/scrapers/#{scraper_name}/current_job/profile", params)
end

#resume(scraper_name, opts = {}) ⇒ Object



68
69
70
71
# File 'lib/datahen/client/scraper_job.rb', line 68

def resume(scraper_name, opts={})
  opts[:status] = 'active'
  update(scraper_name, opts)
end

#sync_schema(scraper_name, opts = {}) ⇒ Object



89
90
91
92
# File 'lib/datahen/client/scraper_job.rb', line 89

def sync_schema(scraper_name, opts={})
  params = @options.merge(opts)
  self.class.put("/scrapers/#{scraper_name}/current_job/sync/schema", params)
end

#update(scraper_name, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datahen/client/scraper_job.rb', line 42

def update(scraper_name, opts={})
  body = {}
  body[:status] = opts[:status] if opts[:status]
  body[:parser_worker_count] = opts[:parsers] if opts[:parsers]
  body[:fetcher_worker_count] = opts[:fetchers] if opts[:fetchers]
  body[:browser_worker_count] = opts[:browsers] if opts[:browsers]
  body[:proxy_type] = opts[:proxy_type] if opts[:proxy_type]
  body[:profile] = opts[:profile] if opts[:profile]
  body[:max_page_size] = opts[:max_page_size] if opts[:max_page_size]
  body[:enable_global_cache] = opts[:enable_global_cache] if opts.has_key?("enable_global_cache") || opts.has_key?(:enable_global_cache)
  body[:retry_interval] = opts[:retry_interval] if opts[:retry_interval]
  body[:soft_fetching_try_limit] = opts[:soft_fetching_try_limit] if opts[:soft_fetching_try_limit]
  body[:soft_refetch_limit] = opts[:soft_refetch_limit] if opts[:soft_refetch_limit]
  body[:parsing_try_limit] = opts[:parsing_try_limit] if opts[:parsing_try_limit]
  body[:prevent_kb_autoscaler] = opts[:prevent_kb_autoscaler] if opts.has_key?("prevent_kb_autoscaler") || opts.has_key?(:prevent_kb_autoscaler)
  body[:deletion_protected] = opts[:deletion_protected] if opts.has_key?("deletion_protected") || opts.has_key?(:deletion_protected)
  params = @options.merge({body: body.to_json})

  self.class.put("/scrapers/#{scraper_name}/current_job", params)
end