Class: Agents::HttpRequestAgent

Inherits:
Agent
  • Object
show all
Includes:
EventHeadersConcern, FileHandling, WebRequestConcern
Defined in:
lib/huginn_http_request_agent/http_request_agent.rb

Constant Summary collapse

MIME_RE =
/\A\w+\/.+\z/

Instance Method Summary collapse

Instance Method Details

#checkObject



189
190
191
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 189

def check
  handle interpolated['payload'].presence || {}, headers
end

#default_optionsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 91

def default_options
  {
    'endpoint' => "http://www.example.com",
    'expected_receive_period_in_days' => '1',
    'content_type' => 'json',
    'method' => 'get',
    'payload' => {
      'key' => 'value',
      'something' => 'the event contained {{ somekey }}'
    },
    'headers' => {},
    'emit_events' => 'true',
    'no_merge' => 'false',
    'output_mode' => 'clean',
    'log_requests' => 'false'
  }
end

#methodObject



119
120
121
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 119

def method
  (interpolated['method'].presence || 'post').to_s.downcase
end

#receive(incoming_events) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 175

def receive(incoming_events)
  incoming_events.each do |event|
    interpolate_with(event) do
      outgoing = interpolated['payload'].presence || {}

      if boolify(interpolated['no_merge'])
        handle outgoing, event, headers(interpolated[:headers])
      else
        handle outgoing.merge(event.payload), event, headers(interpolated[:headers])
      end
    end
  end
end

#validate_optionsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 123

def validate_options
  unless options['endpoint'].present?
    errors.add(:base, "endpoint is a required field")
  end

  if options['payload'].present? && %w[get delete].include?(method) && !(options['payload'].is_a?(Hash) || options['payload'].is_a?(Array))
    errors.add(:base, "if provided, payload must be a hash or an array")
  end

  if options['payload'].present? && %w[post put patch].include?(method)
    if !(options['payload'].is_a?(Hash) || options['payload'].is_a?(Array)) && options['content_type'] !~ MIME_RE
      errors.add(:base, "if provided, payload must be a hash or an array")
    end
  end

  if options['content_type'] =~ MIME_RE && options['payload'].is_a?(String) && boolify(options['no_merge']) != true
    errors.add(:base, "when the payload is a string, `no_merge` has to be set to `true`")
  end

  if options['content_type'] == 'form' && options['payload'].present? && options['payload'].is_a?(Array)
    errors.add(:base, "when content_type is a form, if provided, payload must be a hash")
  end

  if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
    errors.add(:base, "if provided, emit_events must be true or false")
  end

  if options.has_key?('log_requests') && boolify(options['log_requests']).nil?
    errors.add(:base, "If provided, log_requests must be true or false")
  end

  validate_event_headers_options!

  unless %w[post get put delete patch].include?(method)
    errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'")
  end

  if options['no_merge'].present? && !%[true false].include?(options['no_merge'].to_s)
    errors.add(:base, "if provided, no_merge must be 'true' or 'false'")
  end

  if options['output_mode'].present? && !options['output_mode'].to_s.include?('{') && !%[clean merge].include?(options['output_mode'].to_s)
    errors.add(:base, "if provided, output_mode must be 'clean' or 'merge'")
  end

  unless headers.is_a?(Hash)
    errors.add(:base, "if provided, headers must be a hash")
  end

  validate_web_request_options!
end

#working?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
# File 'lib/huginn_http_request_agent/http_request_agent.rb', line 109

def working?
  return false if recent_error_logs?

  if interpolated['expected_receive_period_in_days'].present?
    return false unless last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago
  end

  true
end