Class: ClickClientScrap::Client

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

Overview

クライアント

Constant Summary collapse

DEFAULT_HOST_NAME =

ホスト名

"https://sec-sso.click-sec.com/mf/"
DEFAULT_DEMO_HOST_NAME =
"https://fx-demo.click-sec.com/ygmo/servlet/login?FAGD=2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy = ENV["http_proxy"], demo = false) ⇒ Client

コンストラクタ

proxy

プロキシホストを利用する場合、そのホスト名とパスを指定します。

例) proxyhost.com:80



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/clickclient_scrap.rb', line 52

def initialize( proxy=ENV["http_proxy"], demo=false )
  @client = Mechanize.new {|c|
    # プロキシ
    if proxy 
      uri = URI.parse( proxy )
      c.set_proxy( uri.host, uri.port )
    end
  }
  @client.keep_alive = false
  @client.max_history=0
  @client.user_agent_alias = 'Windows IE 7'
  #@client.log = Logger.new("log.txt")
  @demo = demo
  @host_name = @demo ?  DEFAULT_DEMO_HOST_NAME : DEFAULT_HOST_NAME
end

Instance Attribute Details

#host_nameObject

ホスト名



113
114
115
# File 'lib/clickclient_scrap.rb', line 113

def host_name
  @host_name
end

Class Method Details

.error(page) ⇒ Object



106
107
108
109
110
# File 'lib/clickclient_scrap.rb', line 106

def self.error( page )
    msgs = page.body.scan( /<font color="red">([^<]*)</ ).flatten
    error = !msgs.empty? ? msgs.map{|m| m.strip}.join(",") : page.body
    raise "operation failed.detail=#{error}".toutf8 
end

Instance Method Details

#fx_session(userid, password, options = {}, &block) ⇒ Object

ログインし、セッションを開始します。-ブロックを指定した場合、引数としてセッションを指定してブロックを実行します。ブロック実行後、ログアウトします。-そうでない場合、セッションを返却します。この場合、ClickClientScrap::FX::FxSession#logoutを実行しログアウトしてください。

userid

ユーザーID

password

パスワード

options

オプション

戻り値

ClickClientScrap::FX::FxSession



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
# File 'lib/clickclient_scrap.rb', line 76

def fx_session( userid, password, options={}, &block )
  
  page = @client.get(@host_name)
  ClickClientScrap::Client.error(page)  if page.forms.length <= 0
  form = page.forms.first
  form.j_username = userid
  form.j_password = password
  form.WIN_NAME = "CLICK_FX_HOME_TRADE" if @demo
  result = @client.submit(form, form.buttons.first)

  # デモサイトではjsによるリダイレクトは不要。
  if !@demo
    if result.body.toutf8 =~ /<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=([^"]*)">/
       result = @client.get($1)
       ClickClientScrap::Client.error( result ) if result.links.size <= 0
    else
       ClickClientScrap::Client.error( result )
    end
  end
  session = FX::FxSession.new( @client, result.links, options )
  if block_given?
    begin
      yield session
    ensure
      session.logout
    end
  else
    return session
  end
end