Class: ProjectMonitorStat::Fetcher

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

Instance Method Summary collapse

Constructor Details

#initialize(config: raise) ⇒ Fetcher

Returns a new instance of Fetcher.



6
7
8
# File 'lib/project_monitor_stat/fetcher.rb', line 6

def initialize(config: raise)
  @config = config
end

Instance Method Details

#fetchObject



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
# File 'lib/project_monitor_stat/fetcher.rb', line 10

def fetch
  json = Util.get(url: config.url, cookie: config.cookie)

  begin
    projects = JSON.parse(json)
  rescue JSON::ParserError
    return :error_invalid_json
  end

  if projects.empty?
    return :error_no_projects
  end

  begin
    sucesss_results = projects.map do |project|
      project.fetch('build').fetch('status') == 'success'
    end
    building_results = projects.map do |project|
      project.fetch('build').fetch('building')
    end
  rescue JSON::ParserError, KeyError, TypeError
    return :error_invalid_project_attributes
  end

  if sucesss_results.all?
    if building_results.any?
      :building
    else
      if config.idle_seconds
        recent_results = projects.map do |project|
          published_at = Time.parse(project.fetch('build').fetch('published_at'))
          published_at > (Time.now - config.idle_seconds)
        end

        if recent_results.any?
          :success
        else
          :idle
        end
      else
        :success
      end
    end
  else
    :fail
  end
end