Add Liquid variables _response_.url and _url_ to WebsiteAgent

This commit is contained in:
Akinori MUSHA 2016-10-17 20:45:56 +09:00
parent fe35df8752
commit 8b897f5da3
2 changed files with 23 additions and 3 deletions

View file

@ -124,7 +124,9 @@ module Agents
# Liquid Templating
In Liquid templating, the following variable is available:
In Liquid templating, the following variables are available except when invoked by `data_from_event`:
* `_url_`: The URL specified to fetch the content from.
* `_response_`: A response object with the following keys:
@ -132,6 +134,8 @@ module Agents
* `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_.
* `url`: The final URL of the fetched page, following redirects.
# Ordering Events
#{description_events_order}
@ -328,6 +332,7 @@ module Agents
raise "Failed: #{response.inspect}" unless consider_response_successful?(response)
interpolation_context.stack {
interpolation_context['_url_'] = uri.to_s
interpolation_context['_response_'] = ResponseDrop.new(response)
handle_data(response.body, response.env[:url], existing_payload)
}
@ -603,6 +608,11 @@ module Agents
def status
@object.status
end
# The URL
def url
@object.env.url.to_s
end
end
# Wraps Faraday::Utils::Headers

View file

@ -8,6 +8,10 @@ describe Agents::WebsiteAgent do
headers: {
'X-Status-Message' => 'OK'
})
stub_request(:any, /xkcd\.com\/index$/).to_return(status: 301,
headers: {
'Location' => 'http://xkcd.com/'
})
@valid_options = {
'name' => "XKCD",
'expected_update_period_in_days' => "2",
@ -729,14 +733,20 @@ describe Agents::WebsiteAgent do
end
it "should interpolate _response_" do
@valid_options['url'] = 'http://xkcd.com/index'
@valid_options['extract']['response_info'] =
@valid_options['extract']['url'].merge(
'value' => '"{{ "The reponse was " | append:_response_.status | append:" " | append:_response_.headers.X-Status-Message | append:"." }}"'
'value' => '{{ "The reponse from " | append:_response_.url | append:" was " | append:_response_.status | append:" " | append:_response_.headers.X-Status-Message | append:"." | to_xpath }}'
)
@valid_options['extract']['original_url'] =
@valid_options['extract']['url'].merge(
'value' => '{{ _url_ | to_xpath }}'
)
@checker.options = @valid_options
@checker.check
event = Event.last
expect(event.payload['response_info']).to eq('The reponse was 200 OK.')
expect(event.payload['response_info']).to eq('The reponse from http://xkcd.com/ was 200 OK.')
expect(event.payload['original_url']).to eq('http://xkcd.com/index')
end
it "should be formatted by template after extraction" do