Class: BaseService

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

Overview

Popbill API BaseService class

Constant Summary collapse

ServiceID_REAL =
"POPBILL"
ServiceID_TEST =
"POPBILL_TEST"
ServiceURL_REAL =
"https://popbill.linkhub.co.kr"
ServiceURL_TEST =
"https://popbill-test.linkhub.co.kr"
POPBILL_APIVersion =
"1.0"
BOUNDARY =
"==POPBILL_RUBY_SDK=="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ipRestrictOnOffObject

Returns the value of attribute ipRestrictOnOff.



17
18
19
# File 'lib/popbill.rb', line 17

def ipRestrictOnOff
  @ipRestrictOnOff
end

#isTestObject

Returns the value of attribute isTest.



17
18
19
# File 'lib/popbill.rb', line 17

def isTest
  @isTest
end

#linkhubObject

Returns the value of attribute linkhub.



17
18
19
# File 'lib/popbill.rb', line 17

def linkhub
  @linkhub
end

#scopesObject

Returns the value of attribute scopes.



17
18
19
# File 'lib/popbill.rb', line 17

def scopes
  @scopes
end

#token_tableObject

Returns the value of attribute token_table.



17
18
19
# File 'lib/popbill.rb', line 17

def token_table
  @token_table
end

Class Method Details

.instance(linkID, secretKey) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/popbill.rb', line 21

def instance(linkID, secretKey)
  @instance ||= new
  @instance.token_table = {}
  @instance.linkhub = Linkhub.instance(linkID, secretKey)
  @instance.scopes = ["member"]
  @instance.ipRestrictOnOff = true

  return @instance
end

Instance Method Details

#addScope(scopeValue) ⇒ Object

add Service Scope array



36
37
38
# File 'lib/popbill.rb', line 36

def addScope(scopeValue)
  @scopes.push(scopeValue)
end

#checkID(idValue) ⇒ Object

check Popbill Member ID



351
352
353
# File 'lib/popbill.rb', line 351

def checkID(idValue)
  http_response = httpget("/IDCheck?ID=" + idValue, "", "")
end

#checkIsMember(corpNum, linkID) ⇒ Object

Check is Partner’s Popbill Member



386
387
388
389
390
391
# File 'lib/popbill.rb', line 386

def checkIsMember(corpNum, linkID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  http_response = httpget("/Join?CorpNum=" + corpNum + "&LID=" + linkID, "", "")
end

#getAccessURL(corpNum, userID) ⇒ Object

팝빌 로그인 URL



366
367
368
369
370
371
372
373
# File 'lib/popbill.rb', line 366

def getAccessURL(corpNum, userID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=LOGIN", corpNum, userID)
  response['url']
end

#getBalance(corpNum) ⇒ Object

Get Popbill Member’s Remain Point



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/popbill.rb', line 307

def getBalance(corpNum)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getBalance(getSession_Token(corpNum), getServiceID())
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getChargeURL(corpNum, userID) ⇒ Object

팝빌 연동회원 포인트 충전 URL



376
377
378
379
380
381
382
383
# File 'lib/popbill.rb', line 376

def getChargeURL(corpNum, userID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=CHRG", corpNum, userID)
  response['url']
end

#getCorpInfo(corpNum, userID = "") ⇒ Object

Get Corp Info



419
420
421
422
423
424
# File 'lib/popbill.rb', line 419

def getCorpInfo(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/CorpInfo", corpNum, userID)
end

#getPartnerBalance(corpNum) ⇒ Object

Get Linkhub Partner’s Remain Point



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/popbill.rb', line 320

def getPartnerBalance(corpNum)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getPartnerBalance(getSession_Token(corpNum), getServiceID())
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getPartnerURL(corpNum, togo) ⇒ Object

파트너 포인트 충전 URL - 2017/08/29 추가



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/popbill.rb', line 333

def getPartnerURL(corpNum, togo)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getPartnerURL(getSession_Token(corpNum), getServiceID(), togo)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getPopbillURL(corpNum, togo, userID = "") ⇒ Object

Get Pobill SSO URL



356
357
358
359
360
361
362
363
# File 'lib/popbill.rb', line 356

def getPopbillURL(corpNum, togo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=" + togo, corpNum, userID)
  response['url']
end

#getServiceIDObject



52
53
54
# File 'lib/popbill.rb', line 52

def getServiceID()
  return @isTest ? BaseService::ServiceID_TEST : BaseService::ServiceID_REAL
end

#getServiceURLObject



48
49
50
# File 'lib/popbill.rb', line 48

def getServiceURL()
  return @isTest ? BaseService::ServiceURL_TEST : BaseService::ServiceURL_REAL
end

#getSession_Token(corpNum) ⇒ Object

Get Session Token by checking token-cached hash or token Request



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

def getSession_Token(corpNum)
  targetToken = nil
  refresh = false

  # check already cached CorpNum's SessionToken
  if @token_table.key?(corpNum.to_sym)
    targetToken = @token_table[corpNum.to_sym]
  end

  if targetToken.nil?
    refresh = true
  else
    # Token's expireTime must use parse() because time format is hh:mm:ss.SSSZ
    expireTime = DateTime.parse(targetToken['expiration'])
    serverUTCTime = DateTime.strptime(@linkhub.getTime())
    refresh = expireTime < serverUTCTime
  end

  if refresh
    begin
      # getSessionToken from Linkhub
      targetToken = @linkhub.getSessionToken(
          @isTest ? ServiceID_TEST : ServiceID_REAL, corpNum, @scopes, @ipRestrictOnOff ? "" : "*")

    rescue LinkhubException => le
      raise PopbillException.new(le.code, le.message)
    end
    # append token to cache hash
    @token_table[corpNum.to_sym] = targetToken
  end

  targetToken['session_token']
end

#gzip_parse(target) ⇒ Object

end of getSession_Token



93
94
95
96
97
# File 'lib/popbill.rb', line 93

def gzip_parse (target)
  sio = StringIO.new(target)
  gz = Zlib::GzipReader.new(sio)
  gz.read()
end

#httpget(url, corpNum, userID = '') ⇒ Object

Popbill API http Get Request Func



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

def httpget(url, corpNum, userID = '')
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Accept-Encoding" => "gzip,deflate",
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  uri = URI(getServiceURL() + url)
  request = Net::HTTP.new(uri.host, 443)
  request.use_ssl = true

  Net::HTTP::Get.new(uri)

  res = request.get(uri.request_uri, headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end
end

#httppost(url, corpNum, postData, action = '', userID = '', contentsType = '') ⇒ Object

Request HTTP Post



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
175
176
177
178
179
180
# File 'lib/popbill.rb', line 137

def httppost(url, corpNum, postData, action = '', userID = '', contentsType = '')

  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Accept-Encoding" => "gzip,deflate",
  }

  if contentsType == ''
    headers["Content-Type"] = "application/json; charset=utf8"
  else
    headers["Content-Type"] = contentsType
  end

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  if action.to_s != ''
    headers["X-HTTP-Method-Override"] = action
  end

  uri = URI(getServiceURL() + url)

  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, postData, headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end
end

#httppostfile(url, corpNum, form, files, userID) ⇒ Object

Request HTTP Post File



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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/popbill.rb', line 186

def httppostfile(url, corpNum, form, files, userID)
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Content-Type" => "multipart/form-data;boundary=" + BaseService::BOUNDARY,
      "Accept-Encoding" => "gzip,deflate",
      "Connection" => "Keep-Alive"
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  post_body = []

  if form.to_s != ''
    post_body << "--#{BaseService::BOUNDARY}\r\n"
    post_body << "Content-Disposition: form-data; name=\"form\"\r\n"
    post_body << "Content-Type: Application/json;\r\n\r\n"
    post_body << form.to_json + "\r\n"
  end

  files.each do |filePath|
    begin
      fileName = File.basename(filePath)
      post_body << "--#{BaseService::BOUNDARY}\r\n"
      post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{fileName}\"\r\n"
      post_body << "Content-Type: Application/octet-stream\r\n\r\n"
      post_body << File.read(filePath)
    rescue
      raise PopbillException.new(-99999999, "Failed to reading filedata from filepath")
    end
  end

  post_body << "\r\n\r\n--#{BaseService::BOUNDARY}--\r\n"
  # Add the file Data

  uri = URI(getServiceURL() + url)
  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, post_body.join.encode("UTF-8"), headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end

end

#httppostfiles(url, corpNum, form, files, userID) ⇒ Object



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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/popbill.rb', line 246

def httppostfiles(url, corpNum, form, files, userID)
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Content-Type" => "multipart/form-data;boundary=" + BaseService::BOUNDARY,
      "Accept-Encoding" => "gzip,deflate",
      "Connection" => "Keep-Alive"
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  post_body = []

  if form.to_s != ''
    post_body << "--#{BaseService::BOUNDARY}\r\n"
    post_body << "Content-Disposition: form-data; name=\"form\"\r\n"
    post_body << "Content-Type: Application/json; charset=euc-kr\r\n\r\n"
    post_body << form.to_json
  end

  files.each do |filePath|
    begin
      fileName = File.basename(filePath)
      post_body << "--#{BaseService::BOUNDARY}\r\n"
      post_body << "Content-Disposition: form-data; name=\"Filedata\"; filename=\"#{fileName}\"\r\n"
      post_body << "Content-Type: Application/octet-stream\r\n\r\n"
      post_body << File.read(filePath)
    rescue
      raise PopbillException.new(-99999999, "Failed to reading filedata from filepath")
    end
  end

  post_body << "\r\n\r\n--#{BaseService::BOUNDARY}--\r\n"
  # Add the file Data

  uri = URI(getServiceURL() + url)
  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, post_body.join.encode("UTF-8"), headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end

end

#joinMember(joinInfo) ⇒ Object

Join Popbill Member



346
347
348
# File 'lib/popbill.rb', line 346

def joinMember(joinInfo)
  httppost("/Join", "", joinInfo.to_json, "", "")
end

#listContact(corpNum, userID = "") ⇒ Object

Get list Corp Contact



394
395
396
397
398
399
# File 'lib/popbill.rb', line 394

def listContact(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/IDs", corpNum, userID)
end

#registContact(corpNum, contactInfo, userID = "") ⇒ Object

Regist Contact



411
412
413
414
415
416
# File 'lib/popbill.rb', line 411

def registContact(corpNum, contactInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httppost("/IDs/New", corpNum, contactInfo.to_json, "", userID)
end

#setIpRestrictOnOff(value) ⇒ Object



44
45
46
# File 'lib/popbill.rb', line 44

def setIpRestrictOnOff(value)
  @ipRestrictOnOff = value
end

#setIsTest(testValue) ⇒ Object



40
41
42
# File 'lib/popbill.rb', line 40

def setIsTest(testValue)
  @isTest = testValue
end

#updateContact(corpNum, contactInfo, userID = "") ⇒ Object

Update Contact Info



402
403
404
405
406
407
408
# File 'lib/popbill.rb', line 402

def updateContact(corpNum, contactInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  httppost("/IDs", corpNum, contactInfo.to_json, "", userID)
end

#updateCorpInfo(corpNum, corpInfo, userID = "") ⇒ Object

Update Corp Info



427
428
429
430
431
432
# File 'lib/popbill.rb', line 427

def updateCorpInfo(corpNum, corpInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httppost("/CorpInfo", corpNum, corpInfo.to_json, "", userID)
end