@@ -39,6 +39,13 @@ module Agents |
||
| 39 | 39 |
}, |
| 40 | 40 |
"_contents": "tag contents (can be an object for nesting)" |
| 41 | 41 |
} |
| 42 |
+ |
|
| 43 |
+ # Liquid Templating |
|
| 44 |
+ |
|
| 45 |
+ In Liquid templating, the following variable is available: |
|
| 46 |
+ |
|
| 47 |
+ * `events`: An array of events to being output sorted in descending order up to `events_to_show` in number. For example, if source events contain a site title in the `site_title` key, you can put `{{events.first.site_title}}` at `template.title`.
|
|
| 48 |
+ |
|
| 42 | 49 |
MD |
| 43 | 50 |
end |
| 44 | 51 |
|
@@ -115,8 +122,20 @@ module Agents |
||
| 115 | 122 |
end |
| 116 | 123 |
|
| 117 | 124 |
def receive_web_request(params, method, format) |
| 118 |
- if interpolated['secrets'].include?(params['secret']) |
|
| 119 |
- items = received_events.order('id desc').limit(events_to_show).map do |event|
|
|
| 125 |
+ unless interpolated['secrets'].include?(params['secret']) |
|
| 126 |
+ if format =~ /json/ |
|
| 127 |
+ return [{ error: "Not Authorized" }, 401]
|
|
| 128 |
+ else |
|
| 129 |
+ return ["Not Authorized", 401] |
|
| 130 |
+ end |
|
| 131 |
+ end |
|
| 132 |
+ |
|
| 133 |
+ source_events = received_events.order(id: :desc).limit(events_to_show).to_a |
|
| 134 |
+ |
|
| 135 |
+ interpolation_context.stack do |
|
| 136 |
+ interpolation_context['events'] = source_events |
|
| 137 |
+ |
|
| 138 |
+ items = source_events.map do |event| |
|
| 120 | 139 |
interpolated = interpolate_options(options['template']['item'], event) |
| 121 | 140 |
interpolated['guid'] = {'_attributes' => {'isPermaLink' => 'false'},
|
| 122 | 141 |
'_contents' => interpolated['guid'].presence || event.id} |
@@ -165,12 +184,6 @@ module Agents |
||
| 165 | 184 |
|
| 166 | 185 |
return [content, 200, 'text/xml'] |
| 167 | 186 |
end |
| 168 |
- else |
|
| 169 |
- if format =~ /json/ |
|
| 170 |
- return [{ error: "Not Authorized" }, 401]
|
|
| 171 |
- else |
|
| 172 |
- return ["Not Authorized", 401] |
|
| 173 |
- end |
|
| 174 | 187 |
end |
| 175 | 188 |
end |
| 176 | 189 |
|
@@ -93,9 +93,10 @@ describe Agents::DataOutputAgent do |
||
| 93 | 93 |
expect(status).to eq(200) |
| 94 | 94 |
end |
| 95 | 95 |
|
| 96 |
- describe "outputtng events as RSS and JSON" do |
|
| 96 |
+ describe "outputting events as RSS and JSON" do |
|
| 97 | 97 |
let!(:event1) do |
| 98 | 98 |
agents(:bob_website_agent).create_event :payload => {
|
| 99 |
+ "site_title" => "XKCD", |
|
| 99 | 100 |
"url" => "http://imgs.xkcd.com/comics/evolving.png", |
| 100 | 101 |
"title" => "Evolving", |
| 101 | 102 |
"hovertext" => "Biologists play reverse Pokemon, trying to avoid putting any one team member on the front lines long enough for the experience to cause evolution." |
@@ -104,6 +105,7 @@ describe Agents::DataOutputAgent do |
||
| 104 | 105 |
|
| 105 | 106 |
let!(:event2) do |
| 106 | 107 |
agents(:bob_website_agent).create_event :payload => {
|
| 108 |
+ "site_title" => "XKCD", |
|
| 107 | 109 |
"url" => "http://imgs.xkcd.com/comics/evolving2.png", |
| 108 | 110 |
"title" => "Evolving again", |
| 109 | 111 |
"date" => '', |
@@ -113,6 +115,7 @@ describe Agents::DataOutputAgent do |
||
| 113 | 115 |
|
| 114 | 116 |
let!(:event3) do |
| 115 | 117 |
agents(:bob_website_agent).create_event :payload => {
|
| 118 |
+ "site_title" => "XKCD", |
|
| 116 | 119 |
"url" => "http://imgs.xkcd.com/comics/evolving0.png", |
| 117 | 120 |
"title" => "Evolving yet again with a past date", |
| 118 | 121 |
"date" => '2014/05/05', |
@@ -204,6 +207,28 @@ describe Agents::DataOutputAgent do |
||
| 204 | 207 |
] |
| 205 | 208 |
}) |
| 206 | 209 |
end |
| 210 |
+ |
|
| 211 |
+ describe "interpolating \"events\"" do |
|
| 212 |
+ before do |
|
| 213 |
+ agent.options['template']['title'] = "XKCD comics as a feed{% if events.first.site_title %} ({{events.first.site_title}}){% endif %}"
|
|
| 214 |
+ agent.save! |
|
| 215 |
+ end |
|
| 216 |
+ |
|
| 217 |
+ it "can output RSS" do |
|
| 218 |
+ stub(agent).feed_link { "https://yoursite.com" }
|
|
| 219 |
+ content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
|
|
| 220 |
+ expect(status).to eq(200) |
|
| 221 |
+ expect(content_type).to eq('text/xml')
|
|
| 222 |
+ expect(Nokogiri(content).at('/rss/channel/title/text()').text).to eq('XKCD comics as a feed (XKCD)')
|
|
| 223 |
+ end |
|
| 224 |
+ |
|
| 225 |
+ it "can output JSON" do |
|
| 226 |
+ content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
|
|
| 227 |
+ expect(status).to eq(200) |
|
| 228 |
+ |
|
| 229 |
+ expect(content['title']).to eq('XKCD comics as a feed (XKCD)')
|
|
| 230 |
+ end |
|
| 231 |
+ end |
|
| 207 | 232 |
end |
| 208 | 233 |
|
| 209 | 234 |
describe "outputting nesting" do |