Class: Safen
- Inherits:
-
Object
- Object
- Safen
- Defined in:
- lib/safen.rb,
lib/safen/body.rb,
lib/safen/error.rb,
lib/safen/header.rb,
lib/safen/version.rb
Defined Under Namespace
Classes: Body, Header, SafenError
Constant Summary collapse
- VERSION =
"0.1.2"
Instance Attribute Summary collapse
-
#api_server_entpoints ⇒ Object
readonly
Returns the value of attribute api_server_entpoints.
-
#corp_code ⇒ Object
readonly
Returns the value of attribute corp_code.
Instance Method Summary collapse
- #call_api(header, body) ⇒ Object
-
#create(old_tel_num, group_code) ⇒ String
매핑 요청.
-
#initialize(corp_code, api_server_entpoints) ⇒ Safen
constructor
고객코드와 api server endpoint 리스트로 초기화.
-
#remove(new_tel_num, group_code) ⇒ bool
취소 요청.
-
#show(new_tel_num, group_code) ⇒ bool
매핑 조회 요청.
-
#update(old_tel_num, new_old_tel_num, group_code, new_tel_num) ⇒ bool
수정 요청.
Constructor Details
#initialize(corp_code, api_server_entpoints) ⇒ Safen
고객코드와 api server endpoint 리스트로 초기화
13 14 15 16 |
# File 'lib/safen.rb', line 13 def initialize(corp_code, api_server_entpoints) @corp_code = corp_code @api_server_entpoints = api_server_entpoints end |
Instance Attribute Details
#api_server_entpoints ⇒ Object (readonly)
Returns the value of attribute api_server_entpoints.
8 9 10 |
# File 'lib/safen.rb', line 8 def api_server_entpoints @api_server_entpoints end |
#corp_code ⇒ Object (readonly)
Returns the value of attribute corp_code.
8 9 10 |
# File 'lib/safen.rb', line 8 def corp_code @corp_code end |
Instance Method Details
#call_api(header, body) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/safen.rb', line 18 def call_api(header, body) ret = {} @api_server_entpoints.each_with_index do |endpoint, idx| ip, port = endpoint.split(':') begin socket = TCPSocket.new(ip, port.to_i) socket.puts("#{header}#{body}") ret[:size] = socket.read(4).to_i ret[:code] = socket.read(4) ret[:corp_code] = socket.read(4) ret[:data] = socket.read(ret[:size]) break # 성공시 탈출 rescue => e if idx == @api_server_entpoints.size - 1 # last raise e else next # 실패시 다음 엔드포인드 진행 end ensure socket.close if socket end end if ret[:code] == '2001' # 매핑 응답 return ret[:data][20...24], ret[:data][0...20].strip elsif ret[:code] == '2002' # 해제 응답 return ret[:data][0...4], nil elsif ret[:code] == '2003' # 수정 응답 return ret[:data][0...4], nil elsif ret[:code] == '2004' # 조회 응답 return ret[:data][0...4], ret[:data][4...24].strip end return nil, nil end |
#create(old_tel_num, group_code) ⇒ String
매핑 요청
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/safen.rb', line 63 def create(old_tel_num, group_code) header = Header.mapping_request(@corp_code) body = Body.mapping_request(old_tel_num, group_code) code, data = call_api(header, body) if code != '0000' raise SafenError.new(code), "Safen API call error #{code}" end data end |
#remove(new_tel_num, group_code) ⇒ bool
취소 요청
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/safen.rb', line 99 def remove(new_tel_num, group_code) header = Header.mapping_cancel_request(@corp_code) body = Body.mapping_cancel_request(new_tel_num, group_code) code, data = call_api(header, body) if code != '0000' raise SafenError.new(code), "Safen API call error #{code}" end true end |
#show(new_tel_num, group_code) ⇒ bool
매핑 조회 요청
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/safen.rb', line 116 def show(new_tel_num, group_code) header = Header.mapping_show_request(@corp_code) body = Body.mapping_show_request( new_tel_num, group_code) code, data = call_api(header, body) if code != '0000' raise SafenError.new(code), "Safen API call error #{code}" end data end |
#update(old_tel_num, new_old_tel_num, group_code, new_tel_num) ⇒ bool
수정 요청
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/safen.rb', line 82 def update(old_tel_num, new_old_tel_num, group_code, new_tel_num) header = Header.mapping_update_request(@corp_code) body = Body.mapping_update_request(old_tel_num, new_old_tel_num, group_code, new_tel_num) code, data = call_api(header, body) if code != '0000' raise SafenError.new(code), "Safen API call error #{code}" end true end |