Class: IrisRb::Client
- Inherits:
-
Object
- Object
- IrisRb::Client
- Defined in:
- lib/iris_rb/client.rb
Instance Method Summary collapse
- #connect_websocket ⇒ Object
- #handle_message(raw_msg) ⇒ Object
- #image_base64(image_path) ⇒ Object
-
#initialize(url:, hot_reload: false) ⇒ Client
constructor
A new instance of Client.
- #log_error(msg) ⇒ Object
- #log_info(msg) ⇒ Object
- #query(query_str, bind = []) ⇒ Object
- #reply(message) ⇒ Object
- #send_image(room_id, image_path_or_base64) ⇒ Object
- #send_image_multiple(room_id, image_base64s) ⇒ Object
- #start_hot_reload ⇒ Object
Constructor Details
#initialize(url:, hot_reload: false) ⇒ Client
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/iris_rb/client.rb', line 12 def initialize(url:, hot_reload: false) if url =~ %r{^https?://} @http_url = url @ws_url = url.sub(%r{/$}, "") + "/ws" else raise ArgumentError, "url must start with http:// or https://" end @logger = Logger.new($stdout) @current_room_id = nil connect_websocket start_hot_reload if hot_reload end |
Instance Method Details
#connect_websocket ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/iris_rb/client.rb', line 25 def connect_websocket @ws = WebSocket::Client::Simple.connect(@ws_url) client = self @ws.on :open do client.log_info("WebSocket connected") end @ws.on :message do |msg| client.(msg.data) end @ws.on :error do |e| client.log_error("WebSocket error: #{e.message}") end @ws.on :close do |e| client.log_info("WebSocket disconnected: #{e.inspect}") end end |
#handle_message(raw_msg) ⇒ Object
46 47 48 49 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/iris_rb/client.rb', line 46 def (raw_msg) parsed = parse_json(raw_msg) return unless parsed json = parsed["json"] json = parse_json(json) if json.is_a?(String) json ||= {} = json["attachment"] = parse_json() if .is_a?(String) ||= {} v = json["v"] v = parse_json(v) if v.is_a?(String) v ||= {} chat = { room: { id: json["chat_id"], name: parsed["room"] }, sender: { id: json["user_id"], name: parsed["sender"] }, message: { id: json["id"], type: json["type"], content: json["message"], attachment: , v: v }, raw: parsed["json"] } extended_chat = extend_chat(chat) @current_room_id = extended_chat[:room][:id] $current_chat = extended_chat $client = self $msg = extended_chat[:message][:content].strip Thread.new do event = event_type(v) case event when :message (extended_chat) if defined?() when :new_member on_newmem(extended_chat) if defined?(on_newmem) when :del_member on_delmem(extended_chat) if defined?(on_delmem) when :unknown # do nothing? end end rescue => e log_error("Failed to handle message: #{e}") end |
#image_base64(image_path) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/iris_rb/client.rb', line 255 def image_base64(image_path) begin if File.exist?(image_path) image_data = File.binread(image_path) Base64.strict_encode64(image_data) else nil end rescue => e nil end end |
#log_error(msg) ⇒ Object
152 153 154 |
# File 'lib/iris_rb/client.rb', line 152 def log_error(msg) @logger ? @logger.error(msg) : puts(msg) end |
#log_info(msg) ⇒ Object
148 149 150 |
# File 'lib/iris_rb/client.rb', line 148 def log_info(msg) @logger ? @logger.info(msg) : puts(msg) end |
#query(query_str, bind = []) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/iris_rb/client.rb', line 130 def query(query_str, bind = []) endpoint = URI.join(@http_url, "/query") payload = { query: query_str, bind: bind }.to_json headers = { "Content-Type" => "application/json" } res = post_request(endpoint, payload, headers) if res.is_a?(Net::HTTPSuccess) body = JSON.parse(res.body) body["data"] else log_error("Failed to run query: #{res.body}") nil end rescue => e log_error("Query error: #{e}") nil end |
#reply(message) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/iris_rb/client.rb', line 105 def reply() if @current_room_id.nil? raise ArgumentError, "No current room ID available. Make sure a chat event has been processed first." end send_reply(@current_room_id, , "text") end |
#send_image(room_id, image_path_or_base64) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/iris_rb/client.rb', line 113 def send_image(room_id, image_path_or_base64) if File.exist?(image_path_or_base64) image_base64 = image_base64(image_path_or_base64) if image_base64.nil? log_error("이미지 파일을 읽을 수 없습니다: #{image_path_or_base64}") return nil end send_reply(room_id, image_base64, "image") else send_reply(room_id, image_path_or_base64, "image") end end |
#send_image_multiple(room_id, image_base64s) ⇒ Object
126 127 128 |
# File 'lib/iris_rb/client.rb', line 126 def send_image_multiple(room_id, image_base64s) send_reply(room_id, image_base64s, "image_multiple") end |
#start_hot_reload ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/iris_rb/client.rb', line 156 def start_hot_reload current_dir = File.(File.dirname($0)) listener = Listen.to(current_dir, only: /\.rb$/) do |_modified, _added, _removed| exec("ruby #{$0} #{ARGV.join(' ')}") end listener.start end |