Skip to content

Instantly share code, notes, and snippets.

@floere
Created April 27, 2011 11:44
Show Gist options
  • Save floere/944113 to your computer and use it in GitHub Desktop.
Save floere/944113 to your computer and use it in GitHub Desktop.
An example of how to search an RSS feed in Picky.
class RssSearch < Application
# Indexing and searching however you need it.
#
indexing removes_characters: /[^a-zA-Z0-9\s\/\-\_\:\"\&\.\|]/i,
stopwords: /\b(and|the|or|on|of|in|is|to|from|as|at|an)\b/i,
splits_text_on: /[\s\/\-\_\:\"\&\/]/,
removes_characters_after_splitting: /[\.]/
searching removes_characters: /[^a-zA-Z0-9\s\/\-\_\,\&\.\"\~\*\:]/i,
stopwords: /\b(and|the|or|on|of|in|is|to|from|as|at|an)\b/i,
splits_text_on: /[\s\/\,\&\/]/
# This proxy is only needed as we don't
# want to have the RSS lib load the stream
# every time app/application.rb is
# loaded.
#
class EachRSSItemProxy
def each &block
require 'rss'
require 'open-uri'
rss_feed = "http://florianhanke.com/blog/atom.xml"
rss_content = ""
open rss_feed do |f|
rss_content = f.read
end
rss = RSS::Parser.parse rss_content, true
rss.items.each &block
end
end
# We then create an index with a new #each proxy.
#
# Since the ids are strings, we need to tell
# Picky to use :to_s as key formatter.
#
# Add categories and customize as you wish.
#
rss_index = Index::Memory.new :rss do
source EachRSSItemProxy.new
key_format :to_s
category :title
# etc...
end
route %r{\A/rss\Z} => Search.new(rss_index)
end
@floere
Copy link
Author

floere commented Apr 27, 2011

Testing using e.g. RSpec:

require 'spec_helper'
require 'picky-client/spec'

describe 'Integration Tests' do

  before(:all) do
    Indexes.index_for_tests
    Indexes.load_from_cache
  end

  let(:feed) { Picky::TestClient.new(RssSearch, :path => '/rss') }

  # Testing a count of results.
  #
  it { feed.search('hello test').total.should == 42 }

  # Testing a specific order of result ids.
  #
  it { feed.search('meow').ids.should == ['catpost', 'notdogpost', 'purrpurr'] }

end

@karmi
Copy link

karmi commented Apr 28, 2011

An ElasticSearch example: https://gist.github.com/947437

@floere
Copy link
Author

floere commented Apr 28, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment