Class: Coinone::Connection

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

Constant Summary collapse

BASE_URI =
"https://api.coinone.co.kr"
REQUEST_URI =
"https://coinone.co.kr/account/login/"
AUTH_URI =
"https://api.coinone.co.kr/oauth/access_token/"
REFRESH_AUTH_URI =
"https://api.coinone.co.kr/oauth/refresh_token/"
DELETE_AUTH_URI =
"https://api.coinone.co.kr/oauth/delete_token/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

:nodoc



27
28
29
30
31
32
# File 'lib/coinone/connection.rb', line 27

def initialize(options={}) # :nodoc

  @access_token = options[:access_token] || nil
  @secret_key = options[:secret_key] || nil

end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/coinone/connection.rb', line 10

def access_token
  @access_token
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



10
11
12
# File 'lib/coinone/connection.rb', line 10

def secret_key
  @secret_key
end

Class Method Details

.factory(params) ⇒ Object

ACCESS_TOKEN = “ACESS_TOKEN” SECRET_KEY= “SECRET_KEY”



20
21
22
23
24
25
# File 'lib/coinone/connection.rb', line 20

def self.factory(params) # :nodoc
  Connection.new(
    access_token: params[:access_token],
    secret_key: params[:secret_key]
  )
end

Instance Method Details

#check_for_errors(response) ⇒ Object



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
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
# File 'lib/coinone/connection.rb', line 76

def check_for_errors(response)
  # {"errorCode"=>"130", "errorMessage"=>"V2 API Nonce value must be a positive integer", "result"=>"error"} 
  response = JSON.parse(response)
  case response["errorCode"].to_i
    when 11 then raise AccessTokenMissingError, response["errorMessage"]
    when 12 then raise InvalidAccessTokenError, response["errorMessage"]
    when 40 then raise InvalidAPIPermissionError, response["errorMessage"]
    when 50 then raise AuthenticateError, response["errorMessage"]
    when 51 then raise InvalidAPIError, response["errorMessage"]
    when 100 then raise SessionExpiredError, response["errorMessage"]
    when 101 then raise InvalidFormatError, response["errorMessage"]
    when 102 then raise IDMissingError, response["errorMessage"]
    when 103 then raise LackOfBalanceError, response["errorMessage"]
    when 104 then raise OrderIdMissingError, response["errorMessage"]
    when 105 then raise PriceNotCorrectError, response["errorMessage"]
    when 106 then raise LockingError, response["errorMessage"]
    when 107 then raise ParameterError, response["errorMessage"]
    when 111 then raise OrderIdMissingError, response["errorMessage"]
    when 112 then raise CancelFailedError, response["errorMessage"]
    when 113 then raise QuantityTooLowError, response["errorMessage"]
    when 120 then raise APIV2PayloadMissingError, response["errorMessage"]
    when 121 then raise APIV2SignatureMissingError, response["errorMessa ge"]
    when 122 then raise APIV2NonceMissingError, response["errorMessage"]
    when 123 then raise APIV2SignatureIsNotCorrectError, response["errorMessage"]
    when 130 then raise APIV2NonceValueMustBePosiveIntegerError, response["errorMessage"]
    when 131 then raise APIV2NonceBiggerThenLastNonceError, response["errorMessage"]
    when 132 then raise APIV2BodyIsCorruptedError, response["errorMessage"]
    when 150 then raise APIV2Call150Error, response["errorMessage"]
    when 151 then raise APIV2Call151Error, response["errorMessage"]
    when 200 then raise WalletError, response["errorMessage"]
    when 202 then raise Limitation202Error, response["errorMessage"]
    when 210 then raise Limitation210Error, response["errorMessage"]
    when 220 then raise Limitation220Error, response["errorMessage"]
    when 221 then raise Limitation221Error, response["errorMessage"]
    when 310 then raise MobileAuthError, response["errorMessage"]
    when 311 then raise NeedMobileAuthError, response["errorMessage"]
    when 312 then raise NameIsNotCorrectError, response["errorMessage"]
    when 330 then raise PhoneNumberError, response["errorMessage"]
    when 404 then raise PageNotFoundError, response["errorMessage"]
    when 405 then raise ServerError, response["errorMessage"]
    when 444 then raise LockingError, response["errorMessage"]
    when 500 then raise Email500Error, response["errorMessage"]
    when 501 then raise EMail501Error, response["errorMessage"]
    when 777 then raise MobileAuthError, response["errorMessage"]
    when 778 then raise PhoneNumberError, response["errorMessage"]
    when 1202 then raise AppNotFoundError, response["errorMessage"]
    when 1203 then raise AlreadyRegisteredError, response["errorMessage"]
    when 1204 then raise InvalidAccessError, response["errorMessage"]
    when 1205 then raise APIKeyError, response["errorMessage"]
    when 1206 then raise UserNotFound1206Error, response["errorMessage"]
    when 1207 then raise UserNotFound1207Error, response["errorMessage"]
    when 1208 then raise UserNotFound1208Error, response["errorMessage"]
    when 1209 then raise UserNotFound1209Error, response["errorMessage"]
  end
end

#create_coinone_payload(data) ⇒ Object



72
73
74
# File 'lib/coinone/connection.rb', line 72

def create_coinone_payload( data )
  Base64.strict_encode64(data.to_json).chomp
end

#create_coinone_signature(payload) ⇒ Object



68
69
70
# File 'lib/coinone/connection.rb', line 68

def create_coinone_signature( payload )
  OpenSSL::HMAC.hexdigest( 'sha512', @secret_key.upcase, payload)
end

#get(connection_uri, params = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/coinone/connection.rb', line 38

def get( connection_uri, params = {} )

  response = resource[ connection_uri ].get params: params.merge(access_token: @access_token)
  check_for_errors(response.body)
  JSON.parse(response.body, symbolize_names: true)


end

#post(connection_uri, params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/coinone/connection.rb', line 47

def post( connection_uri, params = {} )
  params[:access_token] = @access_token
  params[:nonce]   = Time.now.to_i
  payload = create_coinone_payload(params)
  signature = create_coinone_signature(payload)
  #puts "Send To : #{connection_uri}"
  #puts "params: #{params}"
  #puts "payload: #{payload}"
  #puts "signature:  #{create_coinone_signature(payload)}"

  response = resource[ connection_uri ].post params, {'Content-Type': 'application/json', 'X-COINONE-PAYLOAD': payload, 'X-COINONE-SIGNATURE': signature }

  #puts "Response : #{response}\n\n"
  #puts "Response Body : #{response.body}\n\n"

  check_for_errors(response.body)

  JSON.parse(response.body, symbolize_names: true)

end

#resourceObject



34
35
36
# File 'lib/coinone/connection.rb', line 34

def resource
  @@resouce ||= RestClient::Resource.new( BASE_URI )
end