summaryrefslogtreecommitdiffhomepage
path: root/samples/11_http/03_in_game_web_server_http_post/app/main.rb
blob: 8116358ba0623993277bd9204aaa05a95ece9f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def tick args
  # defaults
  args.state.post_button      = args.layout.rect(row: 0, col: 0, w: 5, h: 1).merge(text: "execute http_post")
  args.state.post_body_button = args.layout.rect(row: 1, col: 0, w: 5, h: 1).merge(text: "execute http_post_body")
  args.state.request_to_s ||= ""
  args.state.request_body ||= ""

  # render
  args.state.post_button.yield_self do |b|
    args.outputs.borders << b
    args.outputs.labels  << b.merge(text: b.text,
                                    y:    b.y + 30,
                                    x:    b.x + 10)
  end

  args.state.post_body_button.yield_self do |b|
    args.outputs.borders << b
    args.outputs.labels  << b.merge(text: b.text,
                                    y:    b.y + 30,
                                    x:    b.x + 10)
  end

  draw_label args, 0,  6, "Request:", args.state.request_to_s
  draw_label args, 0, 14, "Request Body Unaltered:", args.state.request_body

  # input
  if args.inputs.mouse.click
    # ============= HTTP_POST =============
    if (args.inputs.mouse.inside_rect? args.state.post_button)
      # ========= DATA TO SEND ===========
      form_fields = { "userId" => "#{Time.now.to_i}" }
      # ==================================

      args.gtk.http_post "http://localhost:9001/testing",
                         form_fields,
                         ["Content-Type: application/x-www-form-urlencoded"]

      args.gtk.notify! "http_post"
    end

    # ============= HTTP_POST_BODY =============
    if (args.inputs.mouse.inside_rect? args.state.post_body_button)
      # =========== DATA TO SEND ==============
      json = "{ \"userId\": \"#{Time.now.to_i}\"}"
      # ==================================

      args.gtk.http_post_body "http://localhost:9001/testing",
                              json,
                              ["Content-Type: application/json", "Content-Length: #{json.length}"]

      args.gtk.notify! "http_post_body"
    end
  end

  # calc
  args.inputs.http_requests.each do |r|
    puts "#{r}"
    if r.uri == "/testing"
      puts r
      args.state.request_to_s = "#{r}"
      args.state.request_body = r.raw_body
      r.respond 200, "ok"
    end
  end
end

def draw_label args, row, col, header, text
  label_pos = args.layout.rect(row: row, col: col, w: 0, h: 0)
  args.outputs.labels << "#{header}\n\n#{text}".wrapped_lines(80).map_with_index do |l, i|
    { x: label_pos.x, y: label_pos.y - (i * 15), text: l, size_enum: -2 }
  end
end