Module: Ruflet::Protocol

Defined in:
lib/ruflet_protocol/ruflet/protocol.rb

Defined Under Namespace

Classes: DateTimeValue, DurationValue, TimeOfDayValue

Constant Summary collapse

ACTIONS =
{
  register_client: 1,
  patch_control: 2,
  control_event: 3,
  update_control: 4,
  invoke_control_method: 5,
  session_crashed: 6,
  python_output: 7,

  # Legacy JSON protocol aliases kept for compatibility.
  register_web_client: "registerWebClient",
  page_event_from_web: "pageEventFromWeb",
  update_control_props: "updateControlProps"
}.freeze

Class Method Summary collapse

Class Method Details

.date_time(value) ⇒ Object



29
30
31
32
33
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 29

def date_time(value)
  return value if value.nil? || value.is_a?(DateTimeValue)

  DateTimeValue.new(value.to_s)
end

.duration(value) ⇒ Object



41
42
43
44
45
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 41

def duration(value)
  return value if value.nil? || value.is_a?(DurationValue)

  DurationValue.new(value.to_s)
end

.normalize_control_event_payload(payload) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 66

def normalize_control_event_payload(payload)
  {
    "target" => payload["target"] || payload["eventTarget"],
    "name" => payload["name"] || payload["eventName"],
    "data" => payload["data"] || payload["eventData"]
  }
end

.normalize_invoke_method_result_payload(payload) ⇒ Object

The client's reply to invoke_control_method: the pending call id plus its result/error. Ruflet::ConnectionProtocol (ruflet_rails' Rack-hijack adapter and any other host adapter) normalizes this before handing it to Page#handle_invoke_method_result, which matches call_id back to the waiting callback. Without this method that path fell through to the DSL's method_missing and every invoke result was silently discarded.



87
88
89
90
91
92
93
94
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 87

def normalize_invoke_method_result_payload(payload)
  payload = {} unless payload.is_a?(Hash)
  {
    "call_id" => payload["call_id"] || payload["callId"],
    "result" => payload.key?("result") ? payload["result"] : payload["value"],
    "error" => payload["error"]
  }
end

.normalize_register_payload(payload) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 51

def normalize_register_payload(payload)
  page = payload["page"] || {}
  {
    "session_id" => payload["session_id"],
    "page_name" => payload["page_name"] || "",
    "route" => page["route"] || "/",
    "width" => page["width"],
    "height" => page["height"],
    "platform" => page["platform"],
    "platform_brightness" => page["platform_brightness"],
    "window" => page["window"] || {},
    "media" => page["media"] || {}
  }
end

.normalize_update_control_payload(payload) ⇒ Object



74
75
76
77
78
79
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 74

def normalize_update_control_payload(payload)
  {
    "id" => payload["id"] || payload["target"] || payload["eventTarget"],
    "props" => payload["props"].is_a?(Hash) ? payload["props"] : {}
  }
end

.pack_message(action:, payload:) ⇒ Object



47
48
49
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 47

def pack_message(action:, payload:)
  [action, payload]
end

.register_response(session_id:, page_patch: nil) ⇒ Object

page_patch defaults to nil rather than {} because mruby cannot parse a hash literal as a keyword argument default, and this file is compiled into the embedded VM.



99
100
101
102
103
104
105
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 99

def register_response(session_id:, page_patch: nil)
  {
    "session_id" => session_id,
    "page_patch" => page_patch || {},
    "error" => ""
  }
end

.time_of_day(value) ⇒ Object



35
36
37
38
39
# File 'lib/ruflet_protocol/ruflet/protocol.rb', line 35

def time_of_day(value)
  return value if value.nil? || value.is_a?(TimeOfDayValue)

  TimeOfDayValue.new(value.to_s)
end