Class: Pindo::BossConfigClient
- Inherits:
-
Object
- Object
- Pindo::BossConfigClient
- Defined in:
- lib/pindo/client/bossconfigclient.rb
Instance Attribute Summary collapse
-
#app_type ⇒ Object
Returns the value of attribute app_type.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#requests_config ⇒ Object
Returns the value of attribute requests_config.
Instance Method Summary collapse
- #do_login ⇒ Object
- #do_login_req(username: nil, password: nil) ⇒ Object
-
#initialize(app_type: "FunnyApp", is_dev: true) ⇒ BossConfigClient
constructor
A new instance of BossConfigClient.
- #parse_cookie(raw_cookie) ⇒ Object
- #send_phone_code(username: nil) ⇒ Object
Constructor Details
#initialize(app_type: "FunnyApp", is_dev: true) ⇒ BossConfigClient
Returns a new instance of BossConfigClient.
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 46 47 48 |
# File 'lib/pindo/client/bossconfigclient.rb', line 20 def initialize(app_type:"FunnyApp", is_dev:true) # @app_type = app_type @is_dev = is_dev begin boss_config_file = File.join(File::(Pindoconfig.instance.pindo_common_configdir), "bossconfig_client_config.json") @boss_config_json = JSON.parse(File.read(boss_config_file)) @base_url_config = @boss_config_json["base_url_config"] @requests_config = @boss_config_json["boss_req_config"] rescue => error puts error raise Informative, "加载bossconfig 配置文件失败, 需要执行 pindo setup 或者pindo env dreamstudio" end begin @base_url = @base_url_config[@app_type] puts "@base_url #{@base_url}" rescue => error raise Informative, "App Type #{@app_type} 错误!!! 没有配置该类型的Boss域名" end end |
Instance Attribute Details
#app_type ⇒ Object
Returns the value of attribute app_type.
16 17 18 |
# File 'lib/pindo/client/bossconfigclient.rb', line 16 def app_type @app_type end |
#base_url ⇒ Object
Returns the value of attribute base_url.
17 18 19 |
# File 'lib/pindo/client/bossconfigclient.rb', line 17 def base_url @base_url end |
#requests_config ⇒ Object
Returns the value of attribute requests_config.
15 16 17 |
# File 'lib/pindo/client/bossconfigclient.rb', line 15 def requests_config @requests_config end |
Instance Method Details
#do_login ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/pindo/client/bossconfigclient.rb', line 50 def do_login() # username = ask('请输入Boss网站的用户名:') # password = ask('请输入Boss网站的密码:') username = "shuangquan" password = "HIsdfUHfwdh" do_login_req(username:username, password:password) end |
#do_login_req(username: nil, password: nil) ⇒ Object
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 135 136 137 138 139 140 141 142 |
# File 'lib/pindo/client/bossconfigclient.rb', line 76 def do_login_req(username:nil, password:nil) login_path = @requests_config["do_login"]["path"] login_url = @base_url + login_path puts "login_url: #{login_url}" body_params = { username:username, password:password, phone_code:nil } # send_phone_code(username:username) phone_code = ask('请输入Boss网站的手机验证码:') || nil puts "Phone Code #{phone_code}" body_params[:phone_code] = phone_code # con = Faraday.new # res = con.post do |req| # req.url login_url # req.headers['Content-Type'] = 'application/json' # req.body = body_params.to_json # end conn = Faraday.new(:url => @base_url) do |builder| builder.use :cookie_jar builder.adapter Faraday.default_adapter end res = conn.post do |req| req.url login_path req.headers['Content-Type'] = 'application/json' req.body = body_params.to_json end result_data = nil if !res.body.nil? result_data = JSON.parse(res.body) if result_data["err"].nil end end if !res.body.nil? result_data = JSON.parse(res.body) end puts JSON.pretty_generate(result_data) # all_cookies = res['set-cookie'] = File.read("/Users/wade/Desktop/cookie.txt") # puts "111" # puts all_cookies = () # puts JSON.pretty_generate(cookies_array) end |
#parse_cookie(raw_cookie) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pindo/client/bossconfigclient.rb', line 60 def () = Hash.new([]) return unless .split(/[;]\s?/).each do |pairs| key, values = pairs.split('=',2) next unless key && values if key.include?("httponly, ") key = key.gsub(/httponly, /, ''); end [key] = values end end |
#send_phone_code(username: nil) ⇒ Object
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 |
# File 'lib/pindo/client/bossconfigclient.rb', line 145 def send_phone_code(username:nil) boss_url = @base_url + @requests_config["send_phone_code"]["path"] body_params = { username:username } # puts JSON.pretty_generate(body_params) con = Faraday.new res = con.post do |req| req.url boss_url req.headers['Content-Type'] = 'application/json' req.body = body_params.to_json end result_data = nil if !res.body.nil? result_data = JSON.parse(res.body) end puts JSON.pretty_generate(result_data) return result_data end |