Class: Safen

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(corp_code, api_server_entpoints) ⇒ Safen

고객코드와 api server endpoint 리스트로 초기화

Parameters:

  • corp_code (String)

    고객코드

  • api_server_entpoints

    list endpoint ip:port



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_entpointsObject (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_codeObject (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

매핑 요청

Parameters:

  • old_tel_num (String)

    실제 사용중인 착신번호

  • group_code (String)

    그룹코드

Returns:

  • (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

취소 요청

Parameters:

  • new_tel_num (String)

    매핑할 번호

  • group_code (String)

    그룹코드

Returns:

  • (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

매핑 조회 요청

Parameters:

  • new_tel_num (String)

    매핑된 번호

  • group_code (String)

    그룹 코드

Returns:

  • (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

수정 요청

Parameters:

  • old_tel_num (String)

    기존 착신번호 (고객사에서 관리하는 경우 신규시 1234567890)

  • new_old_tel_num (String)

    신규 착신번호 (고객사에서 관리하는 경우 삭제시 1234567890)

  • group_code (String)

    그룹코드

  • new_tel_num (String)

    매핑할 번호

Returns:

  • (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