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

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

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

Instance Method Details

#addScope(scopeValue) ⇒ Object

add Service Scope array



33
34
35
# File 'lib/popbill.rb', line 33

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

#checkID(idValue) ⇒ Object

check Popbill Member ID



318
319
320
# File 'lib/popbill.rb', line 318

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

#checkIsMember(corpNum, linkID) ⇒ Object

Check is Partner’s Popbill Member



333
334
335
336
337
338
# File 'lib/popbill.rb', line 333

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

#getBalance(corpNum) ⇒ Object

Get Popbill Member’s Remain Point



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/popbill.rb', line 287

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

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

Get Corp Info



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

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



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/popbill.rb', line 300

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

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

Get Pobill SSO URL



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

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

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

#getServiceIDObject



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

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

#getServiceURLObject



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

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



50
51
52
53
54
55
56
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
# File 'lib/popbill.rb', line 50

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)
    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



83
84
85
86
87
# File 'lib/popbill.rb', line 83

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



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

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 = '') ⇒ Object

Request HTTP Post



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

def httppost(url, corpNum, postData, action = '', userID = '')
  headers = {
    "x-pb-version" => BaseService::POPBILL_APIVersion,
    "Accept-Encoding" => "gzip,deflate",
    "Content-Type" => "application/json; charset=utf8",
  }

  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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
# File 'lib/popbill.rb', line 166

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



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

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



313
314
315
# File 'lib/popbill.rb', line 313

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

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

Get list Corp Contact



341
342
343
344
345
346
# File 'lib/popbill.rb', line 341

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



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

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

#setIsTest(testValue) ⇒ Object



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

def setIsTest(testValue)
  @isTest = testValue
end

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

Update Contact Info



349
350
351
352
353
354
355
# File 'lib/popbill.rb', line 349

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



374
375
376
377
378
379
# File 'lib/popbill.rb', line 374

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