Module: Phonehome

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

Defined Under Namespace

Modules: Gem

Constant Summary collapse

@@phonehomeURL =
'phonehome.io'

Class Method Summary collapse

Class Method Details

.call(secret_key, id) ⇒ Object

id - from job, Event



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

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){yield}
    user_events = get_user_events(secret_key).split
    status = user_events.include?(id.to_s) ? true : false
    if status == false
      p 'You try to run not your event!'                 #  Write error to console
      yield
      exit
    end
    start(secret_key, id){yield}  # id - from job, Event
    yield
    stop(@id)    # @id - from API, CurrentEvent
  rescue => e
    if e.class == RuntimeError && e.to_s =~ /All events are completed/
      return false
    else
      send_error(@id, e)  # @id - from API, CurrentEvent
    end
  end
end

.check_connectionObject



87
88
89
90
91
92
93
94
95
# File 'lib/phonehome.rb', line 87

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)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/phonehome.rb', line 97

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



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

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!'
    yield
    exit
  end
end

.get_app_name(id) ⇒ Object



67
68
69
70
# File 'lib/phonehome.rb', line 67

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



111
112
113
114
115
116
117
# File 'lib/phonehome.rb', line 111

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

.get_user_events(secret_key) ⇒ Object



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

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



63
64
65
# File 'lib/phonehome.rb', line 63

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



8
9
10
# File 'lib/phonehome.rb', line 8

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

.start(secret_key, id) ⇒ Object

id - from job, Event



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/phonehome.rb', line 39

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"
    yield
    raise "#{id}: All events are completed"
  end
  @id = JSON.parse(response.body)['id']
  @status = JSON.parse(response.body)['status']

end

.stop(id) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/phonehome.rb', line 53

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