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

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, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #msg_unload, #multiline_value?, #pn, #remove_child, #root?, #send_message, #set_parent, #set_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.



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

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

.post_json(url, body, use_ssl = true) ⇒ Object

Post the content to the endpoint.



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

def self.post_json( url, body, use_ssl = true )
  # Structure the request
  uri = URI.parse( url )
  request = Net::HTTP::Post.new( uri.path )
  request.content_type = 'application/json'
  request.body = body
  n = Net::HTTP.new( uri.host, uri.port )
  n.use_ssl = use_ssl

  # Send the payload to the endpoint.
  result = n.start { |http| http.request( request ) }
  $log.debug result.code
  $log.debug result.message
  return result.body
end

.short_typenameObject

The short name of the object type.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

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:



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

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.



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

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.



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

def body_as_json
  h = {}

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

  return h.to_json
end

#msg_runObject

Post the content to the endpoint.



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

def msg_run
  uri = uri_value
  return unless uri

  $log.debug "posting to: #{uri}"
  body = self.body_as_json
  $log.debug "posting body: #{body}"
  use_ssl = uri.downcase.start_with?( 'https' )
  data = Gloo::Objs::HttpPost.post_json uri, body, use_ssl
  self.update_result data
end

#update_result(data) ⇒ Object

Set the result of the API call.



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

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.



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

def uri_value
  uri = find_child URL
  return nil unless uri

  return uri.value
end