Class: Sakura::MailAddress

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

Constant Summary collapse

MAIL_URL =
BASE_URL + 'users/list/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, usage) ⇒ MailAddress

Returns a new instance of MailAddress.



62
63
64
65
# File 'lib/sakura/mail_address.rb', line 62

def initialize(address, usage)
  @address = address
  @usage, @quota = usage.split(/\s*\/\s*/)
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

#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

Class Method Details

.allObject



25
26
27
28
29
30
31
32
# File 'lib/sakura/mail_address.rb', line 25

def all
  page = Client.current_session.get(MAIL_URL, /メールアドレス一覧/)
  page.first('.input-text').select '300件'

  page.all(:css, '.entity-lists .entity-lists-row').map { |element|
    MailAddress.new_from_element(element)
  }
end

.create(local_part, password) ⇒ Object



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

def create(local_part, password)
  Client.current_session.process(MAIL_URL, /メールアドレス一覧/) do |page|
    page.first(:xpath, '//a[text() = "新規追加"]').click

    page.find(:xpath, '//label[contains(text(), "ユーザ名")]/..//input')
        .fill_in with: local_part
    page.find_all(:xpath, '//label[contains(text(), "パスワード")]/..//input').each do |e|
      e.fill_in with: password
    end
    page.find(:xpath, '//button[text() = "作成する"]').click
  end

  true
end

.find(local_part) ⇒ Object



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

def find(local_part)
  page = Client.current_session.get(MAIL_URL, /メールアドレス一覧/)
  page.first('.input-text').select '300件'

  element = page.find(:xpath, "//div[contains(@class, \"entity-lists-row\")]//div[@class=\"username\" and contains(text(), \"#{local_part}\")]/../../..")
  MailAddress.new_from_element(element)
end

.headerObject



49
50
51
52
# File 'lib/sakura/mail_address.rb', line 49

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

.new_from_element(element) ⇒ Object



42
43
44
45
46
47
# File 'lib/sakura/mail_address.rb', line 42

def new_from_element(element)
  MailAddress.new(
    element.find('.username').text.split('@').first,
    element.find('.capacity').text
  )
end

.tabularize(*args) ⇒ Object



54
55
56
57
58
59
# File 'lib/sakura/mail_address.rb', line 54

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

Instance Method Details

#deleteObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/sakura/mail_address.rb', line 67

def delete
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    page.accept_confirm do
      page.find('button.dangerous-button').click
    end
  end

  true
end

#delete_forward_to(mail) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/sakura/mail_address.rb', line 235

def delete_forward_to(mail)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
    page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea')
        .fill_in with: (@forward_list - [mail]).uniq.join("\n")
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @forward_list.delete mail
end

#detailObject



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/sakura/mail_address.rb', line 251

def detail
  # FIXME: The URL won't work when mail addresses are more than 300
  page = Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)

  <<-EOS
usage / quota: #{usage} / #{quota}  (#{percentage(@usage, @quota)})
forward_to:    #{forward_list(page).join(' ')}
keep mail:     #{keep(page)}
virus scan:    #{virus_scan(page)}
spam filter:   #{spam_filter(page)}
  EOS
end

#disable_keepObject



166
167
168
# File 'lib/sakura/mail_address.rb', line 166

def disable_keep
  keep = false
end

#disable_virus_scanObject



138
139
140
# File 'lib/sakura/mail_address.rb', line 138

def disable_virus_scan
  virus_scan = false
end

#enable_keepObject



162
163
164
# File 'lib/sakura/mail_address.rb', line 162

def enable_keep
  keep = true
end

#enable_virus_scanObject



134
135
136
# File 'lib/sakura/mail_address.rb', line 134

def enable_virus_scan
  virus_scan = true
end

#forward_list(page = nil) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/sakura/mail_address.rb', line 213

def forward_list(page = nil)
  if @forward_list.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
  end

  @forward_list
end

#forward_to(mail) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sakura/mail_address.rb', line 223

def forward_to(mail)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
    page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea')
        .fill_in with: (@forward_list + [mail]).uniq.join("\n")
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @forward_list << mail
end

#keep(page = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/sakura/mail_address.rb', line 142

def keep(page = nil)
  if @keep.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @keep = page.find('[name="receiveType"]:checked').value == '1'
  end

  @keep
end

#keep=(value) ⇒ Object



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

def keep=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    page.find("[name='receiveType'][value='#{value ? 1 : 2}']").choose
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @keep = value
end

#password=(value) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/sakura/mail_address.rb', line 104

def password=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/password/#{@address}", /#{@address}のパスワード設定/) do |page|
    page.find_all(:xpath, '//label[contains(text(), "パスワード")]/..//input').each do |e|
      e.fill_in with: value
    end
    page.find(:xpath, '//button[text() = "変更する"]').click
  end
end

#spam_filter(page = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sakura/mail_address.rb', line 170

def spam_filter(page = nil)
  if @spam_filter.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)

    case page.find(:xpath, '//label[contains(text(), "迷惑メールフィルタ")]/..//select').value
    when "0"
      @spam_filter = :disable
    when "1"
      @spam_filter = :quarantine
    when "2"
      @spam_filter = :discard
    when "3"
      @spam_filter = :mark
    end
  end

  @spam_filter
end

#spam_filter=(value) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/sakura/mail_address.rb', line 190

def spam_filter=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    select = page.find(:xpath, '//label[contains(text(), "迷惑メールフィルタ")]/..//select')

    value = value.to_sym
    case value
    when :disable
      select.select '利用しない'
    when :quarantine
      select.select '「迷惑メール」フォルダに保存'
    when :discard
      select.select 'メールを破棄'
    when :mark
      select.select 'フィルタの利用'
    end

    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @spam_filter = value
end

#to_sObject



247
248
249
# File 'lib/sakura/mail_address.rb', line 247

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

#virus_scan(page = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/sakura/mail_address.rb', line 114

def virus_scan(page = nil)
  if @virus_scan.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @virus_scan = page.find('[name="usesVirusCheck"]:checked').value == '1'
  end

  @virus_scan
end

#virus_scan=(value) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/sakura/mail_address.rb', line 124

def virus_scan=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    page.find("[name='usesVirusCheck'][value='#{value ? 1 : 0}']").choose
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @virus_scan = value
end