Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Add OpenSearch specs
Browse files Browse the repository at this point in the history
  • Loading branch information
benmanns committed Jan 30, 2017
1 parent b048481 commit 032310a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/controllers/open_search/descriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module OpenSearch
class DescriptionsController < ApplicationController
respond_to :osd_xml
# Serves the OpenSearch Description document so that you can use the omnibox
# and similar to search on Orientation.
def show
end
end
end
14 changes: 14 additions & 0 deletions app/helpers/open_search/descriptions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module OpenSearch
module DescriptionsHelper
def open_search_description_link_tag
content_tag(
:link,
nil,
rel: 'search',
type: 'application/opensearchdescription+xml',
title: ENV.fetch('APP_NAME', 'Orientation'),
href: open_search_description_path
)
end
end
end
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
!!! 5
%html{ lang: 'en' }
%head
%head{ profile: 'http://a9.com/-/spec/opensearch/1.1/' }

%meta{ charset: 'utf-8' }
%meta{ content: 'IE=edge,chrome=1', 'http-equiv' => 'X-UA-Compatible' }
Expand All @@ -12,6 +12,7 @@
= favicon_link_tag ENV['ORIENTATION_FAVICON']
= csrf_meta_tags
= action_cable_meta_tag
= open_search_description_link_tag

%body{ class: body_class }

Expand Down
24 changes: 24 additions & 0 deletions app/views/open_search/descriptions/show.osd_xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
xml.instruct!
xml.OpenSearchDescription xmlns: 'http://a9.com/-/spec/opensearch/1.1/', 'xmlns:moz': 'http://www.mozilla.org/2006/browser/search/', 'xmlns:suggestions': 'http://www.opensearch.org/specifications/opensearch/extensions/suggestions/1.1' do
xml.AdultContent false
xml.Description "Search on #{ENV.fetch('APP_NAME', 'Orientation')}"
if ENV['ORIENTATION_FAVICON'].present?
xml.Image ENV['ORIENTATION_FAVICON'], width: 16, height: 16, type: 'image/x-icon'
end
if ENV['ORIENTATION_LOGO'].present?
xml.Image ENV['ORIENTATION_LOGO'], width: 64, height: 64, type: 'image/png'
end
xml.InputEncoding 'UTF-8'
xml.Language 'en-US'
xml.Language '*'
xml.LongName "#{ENV.fetch('APP_NAME', 'Orientation')} Search"
xml.OutputEncoding 'UTF-8'
xml.Query role: 'example', searchTerms: 'john smith'
xml.ShortName ENV.fetch('APP_NAME', 'Orientation')
xml.SyndicationRight 'limited'
xml.tag! 'moz:SearchForm', articles_url
# rels: results (default), suggestions, self, collection
# parameters: searchTerms (required), count, startIndex, startPage, language, inputEncoding, outputEncoding
xml.Url type: 'application/opensearchdescription+xml', rel: 'self', template: open_search_description_url
xml.Url type: 'text/html', template: "#{articles_url}?search={searchTerms}&utm_source=opensearch&utm_medium=search&utm_campaign=opensearch"
end
1 change: 1 addition & 0 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register 'application/opensearchdescription+xml', :osd_xml
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
resources :subscriptions, only: :index
resources :endorsements, only: :index

get 'opensearchdescription.xml' => 'open_search/descriptions#show',
format: false,
defaults: { format: :osd_xml },
as: :open_search_description

# this has to be the last route because we're catching slugs at the root path
resources :articles, path: "", only: :show

Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/open_search/descriptions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe OpenSearch::DescriptionsController, type: :controller do
describe 'GET #show' do
it 'returns http success' do
get :show, format: :osd_xml
expect(response).to have_http_status(:success)
end
end
end

0 comments on commit 032310a

Please sign in to comment.