Class: SuperspeedCli::App
- Inherits:
-
Object
- Object
- SuperspeedCli::App
- 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.("~/.superspeed-cookies")
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #authenticated? ⇒ Boolean
- #companies ⇒ Object
- #create_log(log) ⇒ Object
- #days ⇒ Object
- #days_info ⇒ Object
-
#initialize(agent) ⇒ App
constructor
A new instance of App.
- #load_credentials ⇒ Object
- #load_month(date = Date.today) ⇒ Object
- #login(username, password) ⇒ Object
- #login_form_visible? ⇒ Boolean
- #save_credentials ⇒ Object
- #script ⇒ Object
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
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
3 4 5 |
# File 'lib/superspeed-cli/app.rb', line 3 def agent @agent end |
#page ⇒ Object (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
68 69 70 |
# File 'lib/superspeed-cli/app.rb', line 68 def authenticated? @agent..any? { |c| c.name == 'PHPSESSID' } end |
#companies ⇒ Object
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 |
#days ⇒ Object
33 34 35 36 37 |
# File 'lib/superspeed-cli/app.rb', line 33 def days authenticate! ExecJS.compile(script).exec('return days') end |
#days_info ⇒ Object
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_credentials ⇒ Object
76 77 78 |
# File 'lib/superspeed-cli/app.rb', line 76 def load_credentials @agent..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 login(username, password) @page = @agent.get(LOGIN_URL) login_form = @page.form login_form._username = username login_form._password = password @page = @agent.submit(login_form, login_form..first) raise "Login failed #{@page.search('//div[contains(@class, "alert")]').text}" if login_form_visible? end |
#login_form_visible? ⇒ Boolean
72 73 74 |
# File 'lib/superspeed-cli/app.rb', line 72 def login_form_visible? @page.content.match(/login_check/) end |
#save_credentials ⇒ Object
27 28 29 30 31 |
# File 'lib/superspeed-cli/app.rb', line 27 def save_credentials authenticate! @agent..save_as CREDENTIALS_FILE, session: true, format: :yaml end |
#script ⇒ Object
51 52 53 |
# File 'lib/superspeed-cli/app.rb', line 51 def script @page.search('//script')[6].content end |