Class: Gloo::Objs::Uri

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web/uri.rb

Constant Summary collapse

KEYWORD =
'uri'.freeze
KEYWORD_SHORT =
'url'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.doc_dataObject

Get the object's documentation data.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/gloo/objs/web/uri.rb', line 179

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A URI or URL.',
    :messages => [
      'open — Open the URL in the default browser.',
      'get_scheme — Get the URI scheme; example: http.',
      'get_host — Get the URI host; example: google.com.',
      'get_path — Get the URI resource path; example: /post.',
      'get_query — Get the URI query parameters; example: id=121.',
      'get_fragment — Get the URI fragment.',
      'get_cert_expires — Get the web site\'s certificate expiration date.'
    ],
    :examples => <<~EXAMPLES.strip
      url [can] :
        on_load [script] :
          tell url.u to get_scheme
          show "scheme: " + it

          tell url.u to get_host
          show "host: " + it

          tell url.u to get_path
          show "path: " + it

          show "opening URL: " + url.u
          tell url.u to open
        u [uri] : https://my.url/path/1234
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



53
54
55
56
57
58
# File 'lib/gloo/objs/web/uri.rb', line 53

def self.messages
  basic = %w[open]
  gets = %w[get_scheme get_host get_path]
  more = %w[get_query get_fragment get_cert_expires]
  return super + basic + gets + more
end

.open_url(url, engine = nil) ⇒ Object

Open the given URL with platform command.

NOTE that this was using "cmd = Gloo::Core::GlooSystem.open_for_platform" but refactored because on windows the command is more complex.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gloo/objs/web/uri.rb', line 155

def self.open_url( url, engine=nil )
  case 
  when OS.windows?
    system( "powershell", "-Command", "Start-Process", url )
  when OS.mac?
    `open "#{url}"`
  when OS.linux?
    if Gloo::Core::GlooSystem.wsl?
      `explorer.exe "#{url}"`
    else
      `xdg-open "#{url}"`
    end
  else
    engine.log.warn 'Opening URL not supported on this platform.' if engine
  end
end

.short_typenameObject

The short name of the object type.



27
28
29
# File 'lib/gloo/objs/web/uri.rb', line 27

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



20
21
22
# File 'lib/gloo/objs/web/uri.rb', line 20

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_get_cert_expiresObject

Get the expiration date for the certificate.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gloo/objs/web/uri.rb', line 63

def msg_get_cert_expires
  return unless value

  uri = URI( value )
  response = Net::HTTP.start( uri.host, uri.port, :use_ssl => true )
  cert = response.peer_cert
  o = cert.not_after

  @engine.heap.it.set_to o
  return o
end

#msg_get_fragmentObject

Get the URI fragment that comes after the '#' in the URL. Might be used to scroll down in the page.



79
80
81
82
83
84
85
# File 'lib/gloo/objs/web/uri.rb', line 79

def msg_get_fragment
  return unless value

  o = URI( value ).fragment
  @engine.heap.it.set_to o
  return o
end

#msg_get_hostObject

Get the URI host. Example: google.com



115
116
117
118
119
120
121
# File 'lib/gloo/objs/web/uri.rb', line 115

def msg_get_host
  return unless value

  o = URI( value ).host
  @engine.heap.it.set_to o
  return o
end

#msg_get_pathObject

Get the URI path. Example: /posts



103
104
105
106
107
108
109
# File 'lib/gloo/objs/web/uri.rb', line 103

def msg_get_path
  return unless value

  o = URI( value ).path
  @engine.heap.it.set_to o
  return o
end

#msg_get_queryObject

Get the URI query parameters. Example: id=121



91
92
93
94
95
96
97
# File 'lib/gloo/objs/web/uri.rb', line 91

def msg_get_query
  return unless value

  o = URI( value ).query
  @engine.heap.it.set_to o
  return o
end

#msg_get_schemeObject

Get the URI Scheme. Example: http



127
128
129
130
131
132
133
# File 'lib/gloo/objs/web/uri.rb', line 127

def msg_get_scheme
  return unless value

  o = URI( value ).scheme
  @engine.heap.it.set_to o
  return o
end

#msg_openObject

Open the URI in the default browser.



138
139
140
141
142
# File 'lib/gloo/objs/web/uri.rb', line 138

def msg_open
  return unless value

  Gloo::Objs::Uri.open_url value, @engine
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:



42
43
44
# File 'lib/gloo/objs/web/uri.rb', line 42

def multiline_value?
  return false
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



34
35
36
# File 'lib/gloo/objs/web/uri.rb', line 34

def set_value( new_value )
  self.value = new_value.to_s
end