rifle 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ class ApplicationController < ActionController::Base
2
+ respond_to :json
3
+ session :off
4
+ end
@@ -0,0 +1,16 @@
1
+ class SearchesController < ApplicationController
2
+
3
+ def store
4
+ urn = params[:urn]
5
+ payload = JSON.parse(request.body.read)
6
+ Rifle.store(urn, payload)
7
+ render json: {status: 'success'}, status: :ok
8
+ end
9
+
10
+ def search
11
+ words = params[:q]
12
+ results = Rifle.search(words)
13
+ render json: results, status: :ok
14
+ end
15
+
16
+ end
data/lib/rifle.rb CHANGED
@@ -1,17 +1,26 @@
1
+ require 'json'
1
2
  require 'set'
2
3
  require 'redis'
3
4
  require 'text'
5
+ require_relative 'rifle/rifle_resque'
4
6
 
5
7
  module Rifle
6
8
 
7
9
  class Settings
8
- attr_accessor :ignored_words, :min_word_length, :redis
10
+ attr_accessor :ignored_words, :min_word_length, :redis, :server
11
+
9
12
  def ignored_words
10
13
  @ignored_words ||= ["the", "and", "you", "that"]
11
14
  end
15
+
12
16
  def min_word_length
13
17
  @min_word_length ||= 3
14
18
  end
19
+
20
+ def server
21
+ @server ||= 'http://localhost:3000'
22
+ end
23
+
15
24
  def redis
16
25
  @redis ||= Redis.new
17
26
  end
@@ -23,50 +32,59 @@ module Rifle
23
32
  @@settings
24
33
  end
25
34
 
26
- def self.process_resource(urn, hash)
27
- p = Processor.new
28
- p.index_resource(urn, hash)
35
+ def self.store(urn, hash)
36
+ Processor.new.index_resource(urn, hash)
29
37
  end
30
38
 
31
- def self.search(words)
32
- p = Processor.new
33
- p.search_for(words)
39
+ def self.search(words, urns_only = false)
40
+ Processor.new.search_for(words, urns_only)
34
41
  end
35
42
 
36
43
  class Processor
37
44
 
38
45
  def index_resource(urn, hash)
39
- words = Set.new
40
- traverse_sentences(hash, words)
46
+ words = traverse_object(hash, words)
41
47
  metaphones = get_metaphones(words)
42
48
  metaphones.each do |metaphone|
43
49
  save_processed(urn, metaphone)
44
50
  end
51
+ save_payload(urn, hash)
45
52
  metaphones
46
53
  end
47
54
 
48
- def search_for(sentence)
55
+ def search_for(sentence, urns_only)
49
56
  words = get_words_from_text(sentence)
50
57
  metaphones = get_metaphones(words)
51
58
  urns = Set.new
52
59
  metaphones.each do |metaphone|
53
60
  new_urns = get_urns_for_metaphone(metaphone)
54
- urns =urns.merge(new_urns)
61
+ urns = urns.merge(new_urns)
62
+ end
63
+ if urns_only
64
+ urns
65
+ else
66
+ urns.map { |u|
67
+ {
68
+ urn: u,
69
+ payload: get_payload_for_urn(u)
70
+ }
71
+ }
55
72
  end
56
- urns
57
73
  end
58
74
 
59
75
  private
60
76
 
61
- def traverse_sentences(input, words)
62
- input.each do |key, value|
63
- examine_value(value, words)
64
- end
77
+ def traverse_object(input, words)
78
+ words ||= Set.new
79
+ examine_value(input, words)
80
+ words
65
81
  end
66
82
 
67
83
  def examine_value(value, words)
68
84
  if value.is_a? Hash
69
- traverse_sentences(value, words)
85
+ value.each do |k, v|
86
+ examine_value(v, words)
87
+ end
70
88
  elsif value.is_a? Array
71
89
  value.each do |a|
72
90
  examine_value(a, words)
@@ -78,7 +96,7 @@ module Rifle
78
96
 
79
97
  def get_words_from_text(text)
80
98
  return [] if !text.is_a?(String)
81
- words = text.downcase.split(/[^a-zA-Z]/).select{|w| w.length >= Rifle.settings.min_word_length}
99
+ words = text.downcase.split(/[^a-zA-Z]/).select { |w| w.length >= Rifle.settings.min_word_length }
82
100
  return words - Rifle.settings.ignored_words
83
101
  end
84
102
 
@@ -87,11 +105,19 @@ module Rifle
87
105
  end
88
106
 
89
107
  def save_processed(urn, metaphone)
90
- Rifle.settings.redis.sadd("rifle:#{metaphone}", urn)
108
+ Rifle.settings.redis.sadd("rifle:m:#{metaphone}", urn)
109
+ end
110
+
111
+ def save_payload(urn, hash)
112
+ Rifle.settings.redis.set("rifle:u:#{urn}", hash.to_json)
91
113
  end
92
114
 
93
115
  def get_urns_for_metaphone(metaphone)
94
- Rifle.settings.redis.smembers("rifle:#{metaphone}")
116
+ Rifle.settings.redis.smembers("rifle:m:#{metaphone}")
117
+ end
118
+
119
+ def get_payload_for_urn(urn)
120
+ JSON.parse(Rifle.settings.redis.get("rifle:u:#{urn}"))
95
121
  end
96
122
 
97
123
  end
@@ -0,0 +1,5 @@
1
+ module Rifle
2
+ class Engine < ::Rails::Engine
3
+ p 'Starting Rifle Engine'
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ gem 'resque'
2
+ gem 'rest-client'
3
+
4
+ # This simple resque job pings the search server with a payload.
5
+
6
+ class RifleResque
7
+ @queue = :rifle
8
+
9
+ def initialize(urn, payload)
10
+ @urn = urn
11
+ @payload = payload
12
+ end
13
+
14
+ def store
15
+ RestClient.post("#{Rifle.settings.server}/store/#{@urn}", @payload)
16
+ end
17
+
18
+ def self.perform(urn, payload)
19
+ h = RifleResque.new(urn, payload)
20
+ h.store
21
+ end
22
+ end
data/lib/rifle/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rifle
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rifle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -59,6 +59,54 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: resque
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rest-client
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: session_off
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
62
110
  description:
63
111
  email:
64
112
  - harry@harrylascelles.com
@@ -66,7 +114,11 @@ executables: []
66
114
  extensions: []
67
115
  extra_rdoc_files: []
68
116
  files:
117
+ - app/controllers/application_controller.rb
118
+ - app/controllers/searches_controller.rb
119
+ - lib/rifle/engine.rb
69
120
  - lib/rifle/version.rb
121
+ - lib/rifle/rifle_resque.rb
70
122
  - lib/rifle.rb
71
123
  - README.md
72
124
  homepage: