Class: Pindo::BossConfigClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/client/bossconfigclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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::expand_path(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_typeObject

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_urlObject

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_configObject

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_loginObject



50
51
52
53
54
55
56
57
58
# File 'lib/pindo/client/bossconfigclient.rb', line 50

def ()

  # username = ask('请输入Boss网站的用户名:')
  # password = ask('请输入Boss网站的密码:')
  username = "shuangquan"
  password = "HIsdfUHfwdh"

  (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 (username:nil, password:nil)

     = @requests_config["do_login"]["path"]

     = @base_url + 
    puts "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 
        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']
    all_cookies = File.read("/Users/wade/Desktop/cookie.txt") 
    # puts "111"
    # puts all_cookies
    cookies_array = parse_cookie(all_cookies) 
    # puts JSON.pretty_generate(cookies_array) 
  

end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pindo/client/bossconfigclient.rb', line 60

def parse_cookie(raw_cookie)

  cookies = Hash.new([])
  return cookies unless raw_cookie
  raw_cookie.split(/[;]\s?/).each do |pairs|
    key, values = pairs.split('=',2)
    next unless key && values

    if key.include?("httponly, ")
      key = key.gsub(/httponly, /, '');
    end
    cookies[key] = values
  end
  cookies
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