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/"
ACCESS_TOKEN =
"ACESS_TOKEN"
SECRET_KEY =
"SECRET_KEY"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

:nodoc



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

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.



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

def access_token
  @access_token
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

Class Method Details

.factory(params) ⇒ Object

:nodoc



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

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



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

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



70
71
72
# File 'lib/coinone/connection.rb', line 70

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

#create_coinone_signature(payload) ⇒ Object



66
67
68
# File 'lib/coinone/connection.rb', line 66

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

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



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

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



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

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

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

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

#resourceObject



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

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