Class: SuperspeedCli::App

Inherits:
Object
  • Object
show all
Defined in:
lib/superspeed-cli/app.rb

Constant Summary collapse

BASE_URL =
"http://intern.loki.pitechnologies.ro/app_dev.php"
LOGIN_URL =
"#{BASE_URL}/login"
MONTH_URL =
"#{BASE_URL}/superspeed/report/index/%{year}/%{month}"
CREATE_LOG_URL =
"#{BASE_URL}/superspeed/report/log"
CREDENTIALS_FILE =
File.expand_path("~/.superspeed-cookies")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ App

Returns a new instance of App.



11
12
13
# File 'lib/superspeed-cli/app.rb', line 11

def initialize(agent)
  @agent = agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



3
4
5
# File 'lib/superspeed-cli/app.rb', line 3

def agent
  @agent
end

#pageObject (readonly)

Returns the value of attribute page.



3
4
5
# File 'lib/superspeed-cli/app.rb', line 3

def page
  @page
end

Instance Method Details

#authenticate!Object



80
81
82
# File 'lib/superspeed-cli/app.rb', line 80

def authenticate!
  raise "You need to login first" unless authenticated?
end

#authenticated?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/superspeed-cli/app.rb', line 68

def authenticated?
  @agent.cookies.any? { |c| c.name == 'PHPSESSID' }
end

#companiesObject



39
40
41
42
43
# File 'lib/superspeed-cli/app.rb', line 39

def companies
  authenticate!

  ExecJS.compile(script).exec('return companies')
end

#create_log(log) ⇒ Object



62
63
64
65
66
# File 'lib/superspeed-cli/app.rb', line 62

def create_log(log)
  authenticate!

  @page = @agent.post CREATE_LOG_URL, log.to_json, {'accept' => 'application/json', 'Content-Type' => 'application/json'}
end

#daysObject



33
34
35
36
37
# File 'lib/superspeed-cli/app.rb', line 33

def days
  authenticate!

  ExecJS.compile(script).exec('return days')
end

#days_infoObject



45
46
47
48
49
# File 'lib/superspeed-cli/app.rb', line 45

def days_info
  authenticate!

  ExecJS.compile(script).exec('return daysInfo')
end

#load_credentialsObject



76
77
78
# File 'lib/superspeed-cli/app.rb', line 76

def load_credentials
  @agent.cookie_jar.load(CREDENTIALS_FILE)
end

#load_month(date = Date.today) ⇒ Object



55
56
57
58
59
60
# File 'lib/superspeed-cli/app.rb', line 55

def load_month(date = Date.today)
  authenticate!

  url = MONTH_URL % { year: date.year, month: date.month}
  @page = @agent.get(url)
end

#login(username, password) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/superspeed-cli/app.rb', line 15

def (username, password)
  @page = @agent.get(LOGIN_URL)

   = @page.form
  ._username = username
  ._password = password

  @page = @agent.submit(, .buttons.first)
  raise "Login failed #{@page.search('//div[contains(@class, "alert")]').text}" if 

end

#login_form_visible?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/superspeed-cli/app.rb', line 72

def 
  @page.content.match(/login_check/)
end

#save_credentialsObject



27
28
29
30
31
# File 'lib/superspeed-cli/app.rb', line 27

def save_credentials
  authenticate!

  @agent.cookie_jar.save_as CREDENTIALS_FILE, session: true, format: :yaml
end

#scriptObject



51
52
53
# File 'lib/superspeed-cli/app.rb', line 51

def script
  @page.search('//script')[6].content
end