Module: Phonehome

Defined in:
lib/phonehome.rb,
lib/phonehome/gem/version.rb

Defined Under Namespace

Modules: Gem

Constant Summary collapse

@@phonehomeURL =

class Main

'phonehome.io'

Class Method Summary collapse

Class Method Details

.call(secret_key, id) ⇒ Object

id - from job, Event



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
# File 'lib/phonehome.rb', line 15

def self.call(secret_key, id)   # id - from job, Event
  begin
    result = check_connection?{yield}
    if result == false
      p 'Something goes wrong. Server not responding'
      return false
    end
    check_secret_key(secret_key)
    user_events = get_user_events(secret_key).split
    status = 0
    user_events.each do |val|
      if id.to_i != val.to_i
        status += 1   # make +1 for status var if event IT not him
      else
        status = 0    # if find correct id break this
        break
      end
    end
    if status > 0
      p 'You try to run not your event!'                 #  Write error to console
      raise 'It is not your event id!'                   #
    end
    start(secret_key, id)  # id - from job, Event
    yield
    stop(@id)    # @id - from API, CurrentEvent
  rescue => e
    send_error(@id, e)  # @id - from API, CurrentEvent
  end
end

.check_connectionObject



93
94
95
96
97
98
99
100
101
# File 'lib/phonehome.rb', line 93

def self.check_connection
  begin
    response = Excon.get("http://#{@@phonehomeURL}")
  rescue Excon::Errors::SocketError => error
    p "#{error} - Server not responding"
    sleep(5)
    retry
  end
end

.check_connection?Boolean

check connection with server

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/phonehome.rb', line 103

def self.check_connection?                                   # check connection with server
  begin
    response = Excon.get("http://#{@@phonehomeURL}")         # if all ok, return true
    return true                                              # and continue run code
  rescue Excon::Errors::SocketError => error                 # if server is offline
    begin                                                    # don't know why, but here got exception
      yield                                                  # and fix it by rescue
    rescue
      return false
    end
    return false                                             # return false and stop running code
  end
end

.check_secret_key(secret_key) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/phonehome.rb', line 85

def self.check_secret_key(secret_key)
  response = Excon.post("http://#{@@phonehomeURL}/check_secret_key/#{secret_key}")
  if response.data[:body] != 'true'
    p 'Your secret key is invalid!'
    raise 'Invalid secret key'
  end
end

.get_app_name(id) ⇒ Object



74
75
76
77
# File 'lib/phonehome.rb', line 74

def self.get_app_name(id)
  response = Excon.post("http://#{@@phonehomeURL}/get_name/#{id}", :headers => { "Content-Type" => "application/json" })
  return response.data[:body]
end

.get_status(type) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/phonehome.rb', line 117

def self.get_status(type)
  if type == 1
    return 'working'
  else
    return 'completed'
  end
end

.get_user_events(secret_key) ⇒ Object



79
80
81
82
83
# File 'lib/phonehome.rb', line 79

def self.get_user_events(secret_key)
  response = Excon.post("http://#{@@phonehomeURL}/get_user_event/#{secret_key}")
  response.data[:body]
  return response.data[:body]
end

.send_error(id, e) ⇒ Object



70
71
72
# File 'lib/phonehome.rb', line 70

def self.send_error(id, e)  # @id - from API, CurrentEvent
  Excon.post("http://#{@@phonehomeURL}/query_update/#{id}/failed/#{URI.encode_www_form('error' => e)}")
end

.set_url(url) ⇒ Object



11
12
13
# File 'lib/phonehome.rb', line 11

def self.set_url(url)
  @@phonehomeURL = url
end

.start(secret_key, id) ⇒ Object

id - from job, Event



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/phonehome.rb', line 45

def self.start(secret_key, id)  # id - from job, Event
  @begin = Time.now
  app_name = get_app_name(id)
  response = Excon.post("http://#{@@phonehomeURL}/query/#{secret_key}/#{get_status(1)}/#{id}", :headers => { "Content-Type" => "application/json" })
  if JSON.parse(response.body).status == 'All events are completed'
    p "#{id}: All events are completed"
    raise JSON.parse(response.body).status
  end
  @id = JSON.parse(response.body).id
  @status = JSON.parse(response.body).status

end

.stop(id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/phonehome.rb', line 58

def self.stop(id)   # @id - from API, CurrentEvent
  finish = Time.now
  status = 'completed'
  if @status == 'late'
    status = 'late_completed'
  end


  text = "#{finish.to_i - @begin.to_i} seconds"
  Excon.post("http://#{@@phonehomeURL}/query_update/#{id}/#{status}/#{URI.encode_www_form('result' => text)}")
end