Class: Iijmio::CLI::ImplementedCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/iijmio/cli.rb

Instance Method Summary collapse

Instance Method Details

#auth(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/iijmio/cli.rb', line 51

def auth *args
  developer_id = args[ 0 ] || ::Iijmio::CLI.get_config[ :developer_id ]
  username     = args[ 1 ] || ::Iijmio::CLI.get_config[ :username ]
  password     = args[ 2 ] || ::Iijmio::CLI.get_config[ :password ]

  if developer_id == %{}
    puts %{Please input your IIJmio developer ID.}

    developer_id = STDIN.gets
  end

  if username == %{}
    puts %{Please input your IIJmio username.}

    username = STDIN.gets
  end

  if password == %{}
    puts %{Please input your IIJmio password.}

    password = STDIN.noecho(&:gets)
  end

  developer_id.strip!
  username.strip!
  password.strip!

  ::Capybara.register_driver :poltergeist do | application |
    ::Capybara::Poltergeist::Driver.new(application, js_errors: true, phantomjs: ::Phantomjs.path, timeout: 3000)
  end

  # NOTE: This Webrick is slient HTTP server.
  web_server_thread =
    ::Thread.start {
      server =
        ::WEBrick::HTTPServer.new(
          BindAddress: %{127.0.0.1},
          Port: %{8888},
          AccessLog: [],
          Logger: ::WEBrick::Log.new(%{/dev/null})
        )

      server.mount_proc("/") { | request, response |
        response.body = %{}
      }

      server.start
    }

  session = ::Capybara::Session.new(:poltergeist)
  session.driver.headers = {
    "User-Agent": ::Iijmio::CLI.user_agent
  }

  session.visit(%{https://api.iijmio.jp/mobile/d/v1/authorization/?response_type=token&client_id=#{ developer_id }&state=#{ ::SecureRandom.hex(16) }&redirect_uri=http%3A%2F%2F127.0.0.1%3A8888})

  username_element = session.find(%{input[id=username]})
  username_element.native.send_key(username) if username_element.value == %{}

  password_element = session.find(%{input[id=password]})
  password_element.native.send_key(password) if password_element.value == %{}

  link_element = session.find(%{a[id*=submit]})
  link_element.click

  sleep 1

  link_element = session.find(%{a[id*=confir]})
  link_element.click

  sleep 1

  if session.current_url =~ /access_token=(\w+)/
    ::Iijmio::CLI.set_config({
      token: $1
    })
  end

  web_server_thread.kill

  puts %{}
  puts %{... Completed !!}
  puts %{}
end

#config(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/iijmio/cli.rb', line 20

def config *args
  case args.length
  when 0
    require "pp"
    pp ::Iijmio::CLI.get_config
  when 2
    case args[ 0 ]
    when %{developer_id}
      ::Iijmio::CLI.set_config({
        developer_id: args[ 1 ]
      })
    when %{username}
      ::Iijmio::CLI.set_config({
        username: args[ 1 ]
      })
    when %{password}
      ::Iijmio::CLI.set_config({
        password: args[ 1 ]
      })
    when %{default_sim}
      ::Iijmio::CLI.set_config({
        default_sim: args[ 1 ]
      })
    end
  end
end

#logs(*args) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/iijmio/cli.rb', line 140

def logs *args
  days = args[ 0 ] || 4 # NOTE: 4 days

  response =
    ::Iijmio::CLI.get_iijmio_rest_api(%{/mobile/d/v1/log/packet/})

  # FIXME
  logs = {}

  if ::File.exists?(::File.join(::Dir.home, %{.iijmio-logs}))
    ::File.open ::File.join(::Dir.home, %{.iijmio-logs}), "rb" do | file |
      logs = ::JSON.parse(file.read)
    end
  end
  # FIXME

=begin
  + Family Share (ServiceCode: xxxx)
=end

  response[ %{packetLogInfo} ].each do | hdd_info |
    plan   = hdd_info[ %{plan}   ]
    hdd_service_code = hdd_info[ %{hddServiceCode} ]

    logs[ hdd_service_code ] ||= {}

    puts %{+ #{ plan } (ServiceCode: #{ hdd_service_code })}

=begin
  + SIMs
   + ID: xxxx
    - yyyymmdd 0.0 [MB] | 0.0 [MB]
=end
    red_st = "\x1b[31m"
    red_en = "\x1b[39m"

    puts %{+ SIMs}

    hdd_info[ %{hdoInfo} ].each do | hdo_info |
      hdo_service_code = hdo_info[ %{hdoServiceCode} ]
      packet_logs      = hdo_info[ %{packetLog}      ]

      logs[ hdd_service_code ][ hdo_service_code ] ||= {}

      puts %{ + ID: #{ hdo_service_code }}

      with_coupon_total = 0
      without_coupon_total = 0

      packet_logs.reverse.each_with_index do | packet_log, index |
        if index < days.to_i
          date           = packet_log[ %{date}          ]
          with_coupon    = packet_log[ %{withCoupon}    ]
          without_coupon = packet_log[ %{withoutCoupon} ]

          with_coupon_total += with_coupon
          without_coupon_total += without_coupon

          with_coupon_str          = %{#{ red_st if with_coupon          > 122 }#{ sprintf("%4d", with_coupon)          }#{ red_en if with_coupon          > 122 } [MB]}
          with_coupon_total_str    = %{#{ red_st if with_coupon_total    > 366 }#{ sprintf("%4d", with_coupon_total)    }#{ red_en if with_coupon_total    > 366 } [MB]}
          without_coupon_str       = %{#{ red_st if without_coupon       > 122 }#{ sprintf("%4d", without_coupon)       }#{ red_en if without_coupon       > 122 } [MB]}
          without_coupon_total_str = %{#{ red_st if without_coupon_total > 366 }#{ sprintf("%4d", without_coupon_total) }#{ red_en if without_coupon_total > 366 } [MB]}

          logs[ hdd_service_code ][ hdo_service_code ][ date ] = {
            withCoupon: with_coupon,
            withoutCoupon: without_coupon
          }

          puts %{      date   |    LTE    |   total   |  200Kbps  |   total  } if index == 0
          puts %{  ----------------------------------------------------------} if index == 0
          puts %{  - #{ date } | #{ with_coupon_str } | #{ with_coupon_total_str } | #{ without_coupon_str } | #{ without_coupon_total_str }}
        end
      end

      puts %{}
    end
  end

  # FIXME
  ::File.open ::File.join(::Dir.home, %{.iijmio-logs}), "wb" do | file |
    file.write(logs.to_json)
  end
  # FIXME
end

#ls(*args) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/iijmio/cli.rb', line 229

def ls *args
  response =
    ::Iijmio::CLI.get_iijmio_rest_api(%{/mobile/d/v1/coupon/})

=begin
  + Family Share (ServiceCode: xxxx)
   - クーポン残量: 10.00 [GB]
=end

  response[ %{couponInfo} ].each do | hdd_info |
    plan             = hdd_info[ %{plan}           ]
    hdd_service_code = hdd_info[ %{hddServiceCode} ]
    coupon           = hdd_info[ %{coupon}         ]

    puts %{+ #{ plan } (ServiceCode: #{ hdd_service_code })}
    puts %{ - クーポン残量: #{ sprintf(%{%.2f}, coupon.map { | v | v[ %{volume} ] }.reduce(:+) / 1000.0) } [GB]}

=begin
  + SIMs
   + ID: xxxx
    - TEL: 000-0000-0000
    - クーポン: 無効
   + ID: xxxx
    - TEL: 000-0000-0000
    - クーポン: 無効
   + ID: xxxx
    - TEL: 000-0000-0000
    - クーポン: 無効
=end

    puts %{+ SIMs}

    hdd_info[ %{hdoInfo} ].each do | hdo_info |
      hdo_service_code = hdo_info[ %{hdoServiceCode} ]
      tel              = hdo_info[ %{number}         ]
      coupon_use       = hdo_info[ %{couponUse}      ]

      puts %{ + ID: #{ hdo_service_code }}
      puts %{  - TEL: #{ %{#{$1}-#{$2}-#{$3}} if tel =~ /([0-9]{3})([0-9]{4})([0-9]{4})/ }}
      puts %{  - クーポン: #{ coupon_use ? %{有効} : %{無効} }}
    end
  end
end

#switch(*args) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/iijmio/cli.rb', line 277

def switch *args
  if args.length < 1 ||
     args.length > 2
    STDERR.puts %{Invalid args.}
    exit 1
  end

  coupon_use      = args[ 0 ].downcase != %{low}
  hdo_service_code = args[ 1 ] || ::Iijmio::CLI.get_config[ :default_sim ]

  if hdo_service_code !~ /[a-z]{1,3}[0-9]{1,9}/ # FIXME: It's vague validation.
    STDERR.puts %{Invalid SIM ID (#{ hdoServiceCode }).}
    exit 1
  end

  ::Iijmio::CLI.put_iijmio_rest_api(%{/mobile/d/v1/coupon/}, {
    couponInfo: [ { hdoInfo: [ { couponUse: coupon_use, hdoServiceCode: hdo_service_code } ] } ]
  })
end