Class: Linkhub

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

Overview

Linkhub API Base Class

Constant Summary collapse

LINKHUB_APIVersion =
"2.0"
LINKHUB_ServiceURL =
"https://auth.linkhub.co.kr"
LINKHUB_ServiceURL_Static =
"https://static-auth.linkhub.co.kr"
LINKHUB_ServiceURL_GA =
"https://ga-auth.linkhub.co.kr"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_linkIDObject

Returns the value of attribute _linkID.



14
15
16
# File 'lib/linkhub.rb', line 14

def _linkID
  @_linkID
end

#_secretKeyObject

Returns the value of attribute _secretKey.



14
15
16
# File 'lib/linkhub.rb', line 14

def _secretKey
  @_secretKey
end

Class Method Details

.instance(linkID, secretKey) ⇒ Object



22
23
24
25
26
27
# File 'lib/linkhub.rb', line 22

def instance(linkID, secretKey)
  @instance ||= new
  @instance._linkID = linkID
  @instance._secretKey = secretKey
  return @instance
end

Instance Method Details

#getBalance(bearerToken, serviceID, useStaticIP = false, useGAIP = false) ⇒ Object

Get Popbill member remain point



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/linkhub.rb', line 147

def getBalance(bearerToken, serviceID, useStaticIP=false, useGAIP=false)
  uri = URI(getServiceURL(useStaticIP, useGAIP) + "/" + serviceID + "/Point")

  headers = {
    "Authorization" => "Bearer " + bearerToken,
  }

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

  res = https.get(uri.path, headers)

  if res.code == "200"
    JSON.parse(res.body)["remainPoint"]
  else
    raise LinkhubException.new(JSON.parse(res.body)["code"],
      JSON.parse(res.body)["message"])
  end
end

#getPartnerBalance(bearerToken, serviceID, useStaticIP, useGAIP = false) ⇒ Object

Get Linkhub partner remain point



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/linkhub.rb', line 169

def getPartnerBalance(bearerToken, serviceID, useStaticIP, useGAIP=false)
  uri = URI(getServiceURL(useStaticIP, useGAIP) + "/" + serviceID + "/PartnerPoint")

  headers = {
    "Authorization" => "Bearer " + bearerToken,
  }

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

  res = https.get(uri.path, headers)

  if res.code == "200"
    JSON.parse(res.body)["remainPoint"]
  else
    raise LinkhubException.new(JSON.parse(res.body)["code"],
      JSON.parse(res.body)["message"])
  end
end

#getPartnerURL(bearerToken, serviceID, togo, useStaticIP = false, useGAIP = false) ⇒ Object

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/linkhub.rb', line 125

def getPartnerURL(bearerToken, serviceID, togo, useStaticIP=false, useGAIP=false)
  uri = URI(getServiceURL(useStaticIP, useGAIP) + "/" + serviceID + "/URL?TG=" + togo)

  headers = {
    "Authorization" => "Bearer " + bearerToken,
  }

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

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

  if res.code == "200"
    JSON.parse(res.body)["url"]
  else
    raise LinkhubException.new(JSON.parse(res.body)["code"],
      JSON.parse(res.body)["message"])
  end
end

#getServiceURL(useStaticIP, useGAIP) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linkhub.rb', line 35

def getServiceURL(useStaticIP, useGAIP)
  if @_serviceURL.nil? || @_serviceURL == ""
    if useGAIP
        return LINKHUB_ServiceURL_GA
    elsif useStaticIP
        return LINKHUB_ServiceURL_Static
    else
        return LINKHUB_ServiceURL
    end
  else
    return @_serviceURL
  end
end

#getSessionToken(serviceid, accessid, scope, forwardip = "", useStaticIP = false, useGAIP = false, useLocalTimeYN = true) ⇒ Object

Get SessionToken for Bearer Token



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/linkhub.rb', line 50

def getSessionToken(serviceid, accessid, scope, forwardip="",useStaticIP=false,useGAIP=false,useLocalTimeYN=true)
  uri = URI(getServiceURL(useStaticIP, useGAIP) + "/" + serviceid + "/Token")

  postData = {:access_id => accessid, :scope => scope}.to_json

  apiServerTime = getTime(useStaticIP, useGAIP, useLocalTimeYN)

  hmacTarget = "POST\n"
  hmacTarget += Base64.strict_encode64(Digest::SHA256.digest(postData)) + "\n"
  hmacTarget += apiServerTime + "\n"

  if forwardip != ""
    hmacTarget += forwardip + "\n"
  end

  hmacTarget += LINKHUB_APIVersion + "\n"
  hmacTarget += "/" + serviceid + "/Token"

  key = Base64.decode64(@_secretKey)

  data = hmacTarget
  digest = OpenSSL::Digest.new("sha256")
  hmac = Base64.strict_encode64(OpenSSL::HMAC.digest(digest, key, data))

  headers = {
    "Content-Type" => "application/json",
    "Authorization" => "LINKHUB " + @_linkID + " " + hmac,
    "Accept-Encoding" => "gzip,deflate",
    "x-lh-date" => apiServerTime,
    "x-lh-version" => LINKHUB_APIVersion
  }

  if forwardip != ""
    headers.store("x-lh-forwarded", forwardip)
  end

  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  res = https.post(uri.path, postData, headers)

  begin
    gz = Zlib::GzipReader.new(StringIO.new(res.body.to_s))
    uncompressed_string = gz.read
  rescue Zlib::Error => le
    uncompressed_string = res.body
  end

  if res.code == "200"
    JSON.parse(uncompressed_string)
  else
    raise LinkhubException.new(JSON.parse(uncompressed_string)["code"],
      JSON.parse(uncompressed_string)["message"])
  end
end

#getTime(useStaticIP = false, useGAIP = false, useLocalTimeYN = true) ⇒ Object

Get API Server UTC Time



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/linkhub.rb', line 107

def getTime(useStaticIP=false,useGAIP=false,useLocalTimeYN=true)
  if useLocalTimeYN
    Time.now.utc.iso8601
  else
    uri = URI(getServiceURL(useStaticIP, useGAIP) + "/Time")

    res = Net::HTTP.get_response(uri)

    if res.code == "200"
      res.body
    else
      raise LinkhubException.new(-99999999,
        "failed get Time from Linkhub API server")
    end
  end
end

#setServiceURL(value) ⇒ Object



31
32
33
# File 'lib/linkhub.rb', line 31

def setServiceURL(value)
  @_serviceURL = value
end