Class: Sakura::MailAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/sakura/mail_address.rb

Constant Summary collapse

MAIL_URL =
BASE_URL + 'rs/mail'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, virus_scan, usage, quota, link, link_to_delete = nil) ⇒ MailAddress



48
49
50
51
52
53
54
55
# File 'lib/sakura/mail_address.rb', line 48

def initialize(address, virus_scan, usage, quota, link, link_to_delete=nil)
  @address        = address
  @virus_scan     = virus_scan == '○'
  @usage          = usage
  @quota          = quota
  @link           = link
  @link_to_delete = link_to_delete
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def address
  @address
end

Returns the value of attribute link.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def link
  @link
end

Returns the value of attribute link_to_delete.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def link_to_delete
  @link_to_delete
end

#quotaObject

Returns the value of attribute quota.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def quota
  @quota
end

#usageObject (readonly)

Returns the value of attribute usage.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def usage
  @usage
end

#virus_scanObject

Returns the value of attribute virus_scan.



7
8
9
# File 'lib/sakura/mail_address.rb', line 7

def virus_scan
  @virus_scan
end

Class Method Details

.allObject



21
22
23
24
25
26
27
28
# File 'lib/sakura/mail_address.rb', line 21

def all
  page = Client.current_session.get(MAIL_URL)

  page.all(:xpath, '//a[contains(@href, "mail?Username=")]/../..').map{|element|
    arguments = element.all('td').map(&:text)[0..-2] + element.all('a').map{|i| i[:href] }
    MailAddress.new(*arguments)
  }
end

.create(local_part, password) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/sakura/mail_address.rb', line 10

def create(local_part, password)
  Client.current_session.process(MAIL_URL) do
    fill_in 'NewUsername', with: local_part
    fill_in 'Password1',   with: password
    fill_in 'Password2',   with: password
    find('input[name="Submit_useradd"]').click
  end

  true
end

.find(local_part) ⇒ Object



30
31
32
# File 'lib/sakura/mail_address.rb', line 30

def find(local_part)
  all.find {|m| m.address == local_part }
end

.headerObject



34
35
36
37
# File 'lib/sakura/mail_address.rb', line 34

def header
  str = tabularize('address', 'virus_scan', 'usage', 'quota')
  "#{str}\n#{'-' * (str.size+1)}"
end

.tabularize(*args) ⇒ Object



39
40
41
42
43
44
# File 'lib/sakura/mail_address.rb', line 39

def tabularize(*args)
  args[0].ljust(20) <<
    args[1].to_s.rjust(11) <<
    "#{args[2]} /".to_s.rjust(15) <<
    args[3].to_s.rjust(10)
end

Instance Method Details

#deleteObject



57
58
59
60
61
62
63
64
# File 'lib/sakura/mail_address.rb', line 57

def delete
  link = @link_to_delete
  Client.current_session.process(MAIL_URL) do
    find("a[href=\"#{link}\"]").click
  end

  true
end

#delete_forward_to(mail) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/sakura/mail_address.rb', line 150

def delete_forward_to(mail)
  Client.current_session.process(MAIL_URL + @link) do
    find_field('DeleteAddress[]').select(mail)
    find('a[href="javascript:tr_delete();"]').click
  end

  @forward_list ||= []
  @forward_list.delete mail
end

#disable_keepObject



123
124
125
# File 'lib/sakura/mail_address.rb', line 123

def disable_keep
  keep = false
end

#disable_virus_scanObject



97
98
99
# File 'lib/sakura/mail_address.rb', line 97

def disable_virus_scan
  virus_scan = false
end

#enable_keepObject



119
120
121
# File 'lib/sakura/mail_address.rb', line 119

def enable_keep
  keep = true
end

#enable_virus_scanObject



93
94
95
# File 'lib/sakura/mail_address.rb', line 93

def enable_virus_scan
  virus_scan = true
end

#forward_listObject



127
128
129
130
131
132
133
134
# File 'lib/sakura/mail_address.rb', line 127

def forward_list
  if @forward_list.nil?
    page = Client.current_session.get(MAIL_URL + @link)
    @forward_list = page.all('select[name="DeleteAddress[]"] option').map(&:text)
  end

  @forward_list
end

#forward_to(mail) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sakura/mail_address.rb', line 136

def forward_to(mail)
  Client.current_session.process(MAIL_URL + @link) do
    execute_script "      var f = document.Transfer;\n      f.Address.value = '\#{mail}';\n      f.SubAction.value = 'add';\n      f.submit();\n    JS\n  end\n\n  @forward_list ||= []\n  @forward_list << mail\nend\n"

#keepObject



101
102
103
104
105
106
107
108
# File 'lib/sakura/mail_address.rb', line 101

def keep
  if @keep.nil?
    page = Client.current_session.get(MAIL_URL + @link)
    @keep = page.find('input[name="Save"]:checked').value == '1'
  end

  @keep
end

#keep=(value) ⇒ Object



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

def keep=(value)
  value = value ? 1 : 0
  Client.current_session.process(MAIL_URL + @link) do
    find("input[name='Save'][value='#{value}']").click
  end

  @keep = value == 1
end

#password=(value) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/sakura/mail_address.rb', line 76

def password=(value)
  Client.current_session.process(MAIL_URL + @link) do
    fill_in 'Password1', with: value
    fill_in 'Password2', with: value
    find('input[name="Submit_password"]').click
  end
end

#to_sObject



160
161
162
# File 'lib/sakura/mail_address.rb', line 160

def to_s
  self.class.tabularize(@address, @virus_scan, @usage, @quota)
end