Class: BSClient::App

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ App

Returns a new instance of App.

[View source]

10
11
12
13
# File 'lib/bsclient/app.rb', line 10

def initialize(options, config)
  @options = options
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.


8
9
10
# File 'lib/bsclient/app.rb', line 8

def config
  @config
end

#optionsObject

Returns the value of attribute options.


8
9
10
# File 'lib/bsclient/app.rb', line 8

def options
  @options
end

Instance Method Details

#build_query_params(params = {}) ⇒ Object

[View source]

52
53
54
55
56
# File 'lib/bsclient/app.rb', line 52

def build_query_params(params = {})
  params.merge(developerId: @config.developer_id,
               rtick: rtick,
               signType: 'rsa')
end

#build_to_signed(url, params, body_digest) ⇒ Object

[View source]

58
59
60
61
62
# File 'lib/bsclient/app.rb', line 58

def build_to_signed(url, params, body_digest)
  params = params.sort.map { |kv| kv.join('=') }.join
  path = URI.parse(url).path
  "#{params}#{path}#{body_digest}"
end

#create_user_image(request) ⇒ Object

[View source]

35
36
37
# File 'lib/bsclient/app.rb', line 35

def create_user_image(request)
  post_json(@config.create_user_image_url, request)
end

#digest_body(body) ⇒ Object

[View source]

74
75
76
# File 'lib/bsclient/app.rb', line 74

def digest_body(body)
  Digest::MD5.hexdigest(body)
end

#download_user_image(params) ⇒ Object

[View source]

39
40
41
# File 'lib/bsclient/app.rb', line 39

def download_user_image(params)
  get(@config.download_user_image_url, params)
end

#format_response(resp) ⇒ Object

[View source]

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
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
# File 'lib/bsclient/app.rb', line 110

def format_response(resp)
  unless options[:verbose]
    return resp.body
  end
  request_body = if resp.env.request_body.nil?
                   <<-EOF
                     +-------------------------------------------------+
                     |               No Request Body                   |
                     +-------------------------------------------------+
                   EOF
                 elsif resp.env.request_headers['content-type'] =~ /multipart/
                   <<-EOF
                     +-------------------------------------------------+
                     |                   Binary Data                   |
                     +-------------------------------------------------+
                   EOF
                 elsif resp.env.request_headers['content-type'] =~ /json/
                   json = JSON.pretty_generate(JSON.parse(resp.env.request_body))
                   json.gsub(/^/, '  ')
                 else
                   resp.env.request_body
                 end
  response_body = if resp.env.response_headers['content-type'] =~ /json/
                   json = JSON.pretty_generate(JSON.parse(resp.env.response_body))
                   json.gsub(/^/, '  ')
                 elsif resp.env.response_headers['content-type'] =~ /text/
                   resp.env.response_body
                 else
                   <<-EOF
                     +-------------------------------------------------+
                     |                   Binary Data                   |
                     +-------------------------------------------------+
                   EOF
                end
  <<~EOF

    Request Method: #{resp.env.method.to_s.upcase}
    ----------------------

    URL: #{resp.env.url}
    ----------------------

    Request Headers:
    ----------------

    #{resp.env.request_headers.map { |k, v| "  #{k}: #{v}" }.join("\n")}

    Request Body:
    -------------
    #{request_body}

    ============================================

    Response Status: #{resp.status} #{resp.reason_phrase}
    ----------------------

    Response Headers:
    ----------------
    #{resp.env.response_headers.map { |k, v| "  #{k}: #{v}" }.join("\n")}

    Response Body:
    ----------------
    #{response_body}
  EOF
end

#get(url, params) ⇒ Object

[View source]

90
91
92
93
94
95
96
97
# File 'lib/bsclient/app.rb', line 90

def get(url, params)
  params = build_query_params(params)
  params.merge!(sign: sign(url, params))
  resp = Faraday.get(url) do |req|
    req.params = params
  end
  format_response(resp)
end

#post_json(url, request) ⇒ Object

[View source]

99
100
101
102
103
104
105
106
107
108
# File 'lib/bsclient/app.rb', line 99

def post_json(url, request)
  params = build_query_params
  params.merge!(sign: sign(url, params, request))
  resp = Faraday.post(url) do |req|
    req.headers[:content_type] = 'application/json; charset=UTF-8'
    req.params = params
    req.body = request
  end
  format_response(resp)
end
[View source]

82
83
84
85
86
87
88
# File 'lib/bsclient/app.rb', line 82

def print_response(resp)
  if options[:verbose]
    print format_response(resp)
  else
    print resp.body
  end
end

#query_registration(task_id, account) ⇒ Object

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bsclient/app.rb', line 19

def query_registration(task_id, )
  url = @config.query_url
  request = {
    taskId: task_id,
    account: 
  }.to_json
  params = build_query_params
  params.merge!(sign: sign(url, params, request))
  resp = Faraday.post(url) do |req|
    req.headers[:content_type] = 'application/json; charset=UTF-8'
    req.params = params
    req.body = request
  end
  format_response(resp)
end

#read_private_keyObject

[View source]

68
69
70
71
72
# File 'lib/bsclient/app.rb', line 68

def read_private_key
  raw_key = File.read(File.expand_path(@config.private_key)).chomp
  key = "-----BEGIN RSA PRIVATE KEY-----\n#{raw_key}\n-----END RSA PRIVATE KEY-----\n"
  OpenSSL::PKey::RSA.new(key)
end

#register_account(request) ⇒ Object

[View source]

15
16
17
# File 'lib/bsclient/app.rb', line 15

def (request)
  post_json(@config.register_url, request)
end

#rsa_sign(content) ⇒ Object

[View source]

64
65
66
# File 'lib/bsclient/app.rb', line 64

def rsa_sign(content)
  Base64.strict_encode64(read_private_key.sign(OpenSSL::Digest::SHA1.new, content))
end

#rtickObject

[View source]

78
79
80
# File 'lib/bsclient/app.rb', line 78

def rtick
  "#{(Time.now.to_f * 10000).to_i}"
end

#sign(url, params, body = nil) ⇒ Object

[View source]

43
44
45
46
47
48
49
50
# File 'lib/bsclient/app.rb', line 43

def sign(url, params, body = nil)
  body_digest = if body
                  digest_body(body)
                else
                  ''
                end
  rsa_sign(build_to_signed(url, params, body_digest))
end