Class: Demos::Dynamic::App

Inherits:
Hokusai::Block show all
Defined in:
ui/examples/dynamic.rb

Instance Attribute Summary

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, #initialize, inject, inject!, #method_missing, mount, #on_resize, provide, provides, #render, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

This class inherits a constructor from Hokusai::Block

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Method Details

#scriptObject



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'ui/examples/dynamic.rb', line 57

def script
  <<~RUBY
    require "rest-client"
    require "uri"

    class TestDynamic < Hokusai::Block
      style <<~EOF
      [style]
      inputStyle {
        background: rgb(18, 18, 18);
        text_color: rgb(244,244,244);
        size: 25;
        height: 80;
        outline: outline(1,0,1,0);
        outline_color: rgb(222,222,222);
      }

      textStyle {
        color: rgb(225, 228, 224);
        size: 23;
        cursor: "manual";
        padding: padding(5, 20, 0, 20);
        markdown: true;
      }

      buttonStyle {
        size: 20;
        content: "Open in Maps";
      }
      EOF
      
      template <<~EOF
      [template]
      input {
        ...inputStyle
        @change="geocode"
      }
      hblock
        [if="response"]
          panel
            text {
              ...textStyle
              :content="content"
            }
      EOF

      uses(
        hblock: Hokusai::Blocks::Hblock,
        text: Hokusai::Blocks::Text,
        input: Hokusai::Blocks::Input,
        panel: Hokusai::Blocks::Panel,
        button: Hokusai::Blocks::Button
      )

      attr_reader :response

      def geocode(address)
        uri = "https://geoffrey.skinnyjames.net/api/v1/us/geocode/\#{URI.encode_uri_component(address)}"
        @response = JSON.parse(RestClient.get(uri).body, symbolize_names: true)          
      end

      def content
        @response&.reduce("") do |memo, payload|
          link = "https://maps.google.com/maps?q=\#{payload[:latitude]}+\#{payload[:longitude]}"

          memo << "Address: [\#{payload[:address_number]} \#{payload[:street_name]} (\#{payload[:postal_community]}, \#{payload[:state]})](\#{link})\n"
          memo << "  **Latitude:** _\#{payload[:latitude]}_, **Longitude:** _\#{payload[:longitude]}_\n"
          memo << "  [Some link](https://google.com)\n\n"
        end || "Placeholder"
      end
    end

    TestDynamic
  RUBY
end