linkhub 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/linkhub.rb +133 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d8c9b255cf59df63d8cc293937686b26266a6fe
4
+ data.tar.gz: 0bb4c9dd9a1e30f335e4ba93f0b0f4c1e1627327
5
+ SHA512:
6
+ metadata.gz: dc3c4f660199c478b7c25eed9e4b504ebb1cae8c4bc5fcbcc94e3a12d9ad1fc723b6430a29281b8c73382709dd8869e13d097295bae5aafe567539667173063f
7
+ data.tar.gz: 0b213228558fe734cac53357d658a17be6afe8dac2588f10a4731105c6efce6d82a407707780e2d912781a6a079f4d15f3b60375cb11d26a168872854d269fff
data/lib/linkhub.rb ADDED
@@ -0,0 +1,133 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "net/http"
3
+ require "uri"
4
+ require "json"
5
+ require "digest"
6
+ require "base64"
7
+
8
+ # Linkhub API Base Class
9
+ class Linkhub
10
+ attr_accessor :_linkID, :_secretKey
11
+
12
+ LINKHUB_APIVersion = "1.0"
13
+ LINKHUB_ServiceURL = "https://auth.linkhub.co.kr"
14
+
15
+ # Generate Linkhub Class Singleton Instance
16
+ class << self
17
+ def instance(linkID, secretKey)
18
+ @instance ||= new
19
+ @instance._linkID = linkID
20
+ @instance._secretKey = secretKey
21
+ return @instance
22
+ end
23
+ private :new
24
+ end
25
+
26
+ # Get SessionToken for Bearer Token
27
+ def getSessionToken(serviceid, accessid, scope)
28
+ uri = URI(LINKHUB_ServiceURL + "/" + serviceid + "/Token")
29
+ postData = {:access_id => accessid, :scope => scope}.to_json
30
+
31
+ apiServerTime = getTime()
32
+
33
+ hmacTarget = "POST\n"
34
+ hmacTarget += Base64.strict_encode64(Digest::MD5.digest(postData)) + "\n"
35
+ hmacTarget += apiServerTime + "\n"
36
+ hmacTarget += LINKHUB_APIVersion + "\n"
37
+ hmacTarget += "/" + serviceid + "/Token"
38
+
39
+ key = Base64.decode64(@_secretKey)
40
+
41
+ data = hmacTarget
42
+ digest = OpenSSL::Digest.new("sha1")
43
+ hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, key, data))
44
+
45
+ headers = {
46
+ "Content-Type" => "application/json",
47
+ "Authorization" => "LINKHUB " + @_linkID + " " + hmac,
48
+ "Accept-Encoding" => "gzip,deflate",
49
+ "x-lh-date" => apiServerTime,
50
+ "x-lh-version" => LINKHUB_APIVersion
51
+ }
52
+
53
+ https = Net::HTTP.new(uri.host, 443)
54
+ https.use_ssl = true
55
+ Net::HTTP::Post.new(uri)
56
+
57
+ res = https.post(uri.path, postData, headers)
58
+
59
+ if res.code == "200"
60
+ JSON.parse(res.body)
61
+ else
62
+ raise LinkhubException.new(JSON.parse(res.body)["code"],
63
+ JSON.parse(res.body)["message"])
64
+ end
65
+ end # end of getToken
66
+
67
+
68
+ # Get API Server UTC Time
69
+ def getTime
70
+ uri = URI(LINKHUB_ServiceURL + "/Time")
71
+ res = Net::HTTP.get_response(uri)
72
+
73
+ if res.code == "200"
74
+ res.body
75
+ else
76
+ raise LinkhubException.new(-99999999,
77
+ "failed get Time from Linkhub API server")
78
+ end
79
+ end
80
+
81
+ # Get Popbill member remain point
82
+ def getBalance(bearerToken, serviceID)
83
+ uri = URI(LINKHUB_ServiceURL + "/" + serviceID + "/Point")
84
+
85
+ headers = {
86
+ "Authorization" => "Bearer " + bearerToken,
87
+ }
88
+
89
+ https = Net::HTTP.new(uri.host, 443)
90
+ https.use_ssl = true
91
+ Net::HTTP::Post.new(uri)
92
+
93
+ res = https.get(uri.path, headers)
94
+
95
+ if res.code == "200"
96
+ JSON.parse(res.body)["remainPoint"]
97
+ else
98
+ raise LinkhubException.new(JSON.parse(res.body)["code"],
99
+ JSON.parse(res.body)["message"])
100
+ end
101
+ end
102
+
103
+ # Get Linkhub partner remain point
104
+ def getPartnerBalance(bearerToken, serviceID)
105
+ uri = URI(LINKHUB_ServiceURL + "/" + serviceID + "/PartnerPoint")
106
+
107
+ headers = {
108
+ "Authorization" => "Bearer " + bearerToken,
109
+ }
110
+
111
+ https = Net::HTTP.new(uri.host, 443)
112
+ https.use_ssl = true
113
+ Net::HTTP::Post.new(uri)
114
+
115
+ res = https.get(uri.path, headers)
116
+
117
+ if res.code == "200"
118
+ JSON.parse(res.body)["remainPoint"]
119
+ else
120
+ raise LinkhubException.new(JSON.parse(res.body)["code"],
121
+ JSON.parse(res.body)["message"])
122
+ end
123
+ end
124
+ end
125
+
126
+ # Linkhub API Exception class
127
+ class LinkhubException < StandardError
128
+ attr_reader :code, :message
129
+ def initialize(code, message)
130
+ @code = code
131
+ @message = message
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkhub
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Linkhub Dev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Linkhub API SDK for Popbill, Jusolink Service
14
+ email: code@linkhub.co.kr
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/linkhub.rb
20
+ homepage: https://github.com/linkhub-sdk/linkhub.ruby
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 2.0.0
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.4.8
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Linkhub API SDK
43
+ test_files: []