Class: Gloo::Objs::HttpPost

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

Constant Summary collapse

KEYWORD =
'http_post'.freeze
KEYWORD_SHORT =
'post'.freeze
URL =
'uri'.freeze
BODY =
'body'.freeze
RESULT =
'result'.freeze
SKIP_SSL_VERIFY =
'skip_ssl_verify'.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, 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_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #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

.messagesObject

Get a list of message names that this object receives.



103
104
105
# File 'lib/gloo/objs/web/http_post.rb', line 103

def self.messages
  return super + [ 'run' ]
end

.post_json(url, body, skip_ssl_verify = false) ⇒ Object

Post the content to the endpoint.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gloo/objs/web/http_post.rb', line 131

def self.post_json( url, body, skip_ssl_verify = false )
  uri = URI( url )
  params = { use_ssl: uri.scheme == 'https' }
  params[ :verify_mode ] = ::OpenSSL::SSL::VERIFY_NONE if skip_ssl_verify

  Net::HTTP.start( uri.host, uri.port, params ) do |http|
    request = Net::HTTP::Post.new uri
    request.content_type = 'application/json'
    request.body = body

    return http.request( request )  # returns Net::HTTPResponse object
  end
end

.short_typenameObject

The short name of the object type.



31
32
33
# File 'lib/gloo/objs/web/http_post.rb', line 31

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



24
25
26
# File 'lib/gloo/objs/web/http_post.rb', line 24

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



81
82
83
# File 'lib/gloo/objs/web/http_post.rb', line 81

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



90
91
92
93
94
# File 'lib/gloo/objs/web/http_post.rb', line 90

def add_default_children
  fac = @engine.factory
  fac.create_string URL, 'https://web.site/', self
  fac.create_can BODY, self
end

#body_as_jsonObject

Get all the children of the body container and convert to JSON that will be sent in the HTTP body.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gloo/objs/web/http_post.rb', line 50

def body_as_json
  h = {}

  body = find_child BODY
  body.children.each do |child|
    child_val = Gloo::Objs::Alias.resolve_alias( @engine, child )
    h[ child.name ] = child_val.value
  end

  return h.to_json
end

#msg_runObject

Post the content to the endpoint.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gloo/objs/web/http_post.rb', line 110

def msg_run
  uri = uri_value
  return unless uri

  @engine.log.debug "posting to: #{uri}"
  body = self.body_as_json
  @engine.log.debug "posting body: #{body}"
  result = Gloo::Objs::HttpPost.post_json( uri, body, skip_ssl_verify? )
  @engine.log.debug result.code
  @engine.log.debug result.message

  self.update_result result.body
end

#update_result(data) ⇒ Object

Set the result of the API call.



65
66
67
68
69
70
# File 'lib/gloo/objs/web/http_post.rb', line 65

def update_result( data )
  r = find_child RESULT
  return nil unless r

  r.set_value data
end

#uri_valueObject

Get the URI from the child object. Returns nil if there is none.



39
40
41
42
43
44
# File 'lib/gloo/objs/web/http_post.rb', line 39

def uri_value
  uri = find_child URL
  return nil unless uri

  return uri.value
end