Class: WiteiWebApi::Base
- Inherits:
-
Object
show all
- Includes:
- ActiveModel::AttributeAssignment
- Defined in:
- lib/witei_web_api/base.rb
Defined Under Namespace
Classes: LoggedOutException, LoginFailedException
Class Method Summary
collapse
Class Method Details
.agent ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/witei_web_api/base.rb', line 9
def self.agent
Thread.current[:witei_web_api_agent] ||= begin
agent = WiteiWebApi::Agent.new
login(agent)
agent
end
end
|
.get(uri) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/witei_web_api/base.rb', line 40
def self.get(uri)
page = agent.get("https://witei.com#{uri}")
uri_path = URI.parse(uri).path
if page.uri.path != uri_path
page = login(agent, uri: uri)
if page.uri.path != uri_path
raise LoggedOutException.new("Cannot get #{uri}")
end
end
page
end
|
.has_one(name, &block) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/witei_web_api/base.rb', line 31
def self.has_one(name, &block)
define_method(name) do
id = send(:"#{name}_id")
if id && !id.empty?
block.call.find(id)
end
end
end
|
.login(agent, login: WiteiWebApi.login, password: WiteiWebApi.password, uri: nil) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/witei_web_api/base.rb', line 17
def self.login(agent, login: WiteiWebApi.login, password: WiteiWebApi.password, uri: nil)
uri ||= '/pro/agencies/dashboard/'
page = agent.get("https://witei.com/pro/accounts/login/?next=#{CGI.escape(uri)}")
form = page.form_with(class: 'form-horizontal')
form.login = login
form.password = password
new_page = form.submit
if new_page.uri.path != URI.parse(uri).path
raise(LoginFailedException.new)
end
new_page
end
|