acts_as_cleo 2.4.5 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,5 @@
1
+ if [[ -s "~/.rvm/environments/ruby-1.9.2-p318@acts_as_cleo" ]] ; then
2
+ . "~/.rvm/environments/ruby-1.9.2-p318@acts_as_cleo"
3
+ else
4
+ rvm --create use "ruby-1.9.2-p318@acts_as_cleo"
5
+ fi
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.4.5
1
+ 3.0.0
data/acts_as_cleo.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "acts_as_cleo"
8
- s.version = "2.4.5"
8
+ s.version = "3.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Robert R. Meyer"]
12
- s.date = "2012-03-29"
12
+ s.date = "2012-04-11"
13
13
  s.description = "LinkedIn Open Source type ahead tool's REST API as a ruby gem. Now with Reddis support."
14
14
  s.email = "Blue.Dog.Archolite@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
+ ".rvmrc",
20
21
  "Gemfile",
21
22
  "LICENSE.txt",
22
23
  "README.rdoc",
@@ -24,23 +25,32 @@ Gem::Specification.new do |s|
24
25
  "VERSION",
25
26
  "acts_as_cleo.gemspec",
26
27
  "lib/acts_as_cleo.rb",
28
+ "lib/acts_as_cleo/acts_as_cleo.rb",
27
29
  "lib/acts_as_cleo/acts_methods.rb",
28
30
  "lib/acts_as_cleo/class_methods.rb",
29
31
  "lib/acts_as_cleo/instance_methods.rb",
30
32
  "lib/acts_as_cleo/private_methods.rb",
31
33
  "lib/acts_as_cleo/result.rb",
32
34
  "lib/acts_as_cleo/version.rb",
33
- "lib/cleo.rb",
34
- "lib/cleo/reference.rb",
35
- "lib/cleo/result.rb",
36
- "lib/cleo/server.rb",
35
+ "lib/acts_as_cleo_connection/acts_as_cleo_connection.rb",
36
+ "lib/acts_as_cleo_connection/acts_methods.rb",
37
+ "lib/acts_as_cleo_connection/instance_methods.rb",
38
+ "lib/cleo/base_server.rb",
39
+ "lib/cleo/cleo.rb",
40
+ "lib/cleo/connection_server.rb",
41
+ "lib/cleo/element.rb",
42
+ "lib/cleo/element_server.rb",
43
+ "lib/cleo/service.rb",
37
44
  "lib/generators/acts_as_cleo/install/install_generator.rb",
38
45
  "lib/generators/acts_as_cleo/install/templates/cleo.yml",
39
46
  "lib/generators/acts_as_cleo/install/templates/cleo_initalizer.rb",
40
47
  "lib/generators/acts_as_cleo/install/templates/install.rb",
41
- "lib/generators/acts_as_cleo/install/templates/jobs/cleo_processor.rb",
48
+ "lib/generators/acts_as_cleo/install/templates/jobs/cleo_connection_processor.rb",
49
+ "lib/generators/acts_as_cleo/install/templates/jobs/cleo_element_processor.rb",
42
50
  "lib/generators/acts_as_cleo/install/templates/update.rb",
43
51
  "lib/generators/acts_as_cleo/update/update_generator.rb",
52
+ "lib/xml/connection.rb",
53
+ "lib/xml/result.rb",
44
54
  "test/config/cleo.yml",
45
55
  "test/data/xml/cleo/element.xml",
46
56
  "test/data/xml/cleo/elements.xml",
@@ -53,6 +63,7 @@ Gem::Specification.new do |s|
53
63
  "test/db/migrate/005_create_authors.rb",
54
64
  "test/db/migrate/006_create_postings.rb",
55
65
  "test/db/migrate/007_cleo_table.rb",
66
+ "test/db/testing_db.sqlite",
56
67
  "test/fixtures/authors.yml",
57
68
  "test/fixtures/books.yml",
58
69
  "test/fixtures/categories.yml",
data/lib/acts_as_cleo.rb CHANGED
@@ -1,15 +1,6 @@
1
- #load Cleo Module for result and server
2
- require File.dirname(__FILE__) + '/cleo'
1
+ require File.dirname(__FILE__) + '/cleo/cleo'
2
+ require File.dirname(__FILE__) + '/acts_as_cleo/acts_as_cleo'
3
+ require File.dirname(__FILE__) + '/xml/result'
3
4
 
4
- #Load ActsAsCleo method load
5
- require File.dirname(__FILE__) + '/acts_as_cleo/acts_methods'
6
- require File.dirname(__FILE__) + '/acts_as_cleo/class_methods'
7
- require File.dirname(__FILE__) + '/acts_as_cleo/instance_methods'
8
-
9
- module ActsAsCleo
10
- include ActsMethods
11
-
12
- require 'rails/railtie' if defined?(Rails)
13
- end
14
-
15
- ActiveRecord::Base.extend ActsAsCleo
5
+ #require File.dirname(__FILE__) + '/acts_as_cleo_connection/acts_as_cleo_connection'
6
+ #require File.dirname(__FILE__) + '/xml/connection'
@@ -0,0 +1,11 @@
1
+ #Load ActsAsCleo method load
2
+ require File.dirname(__FILE__) + '/acts_methods'
3
+ require File.dirname(__FILE__) + '/class_methods'
4
+ require File.dirname(__FILE__) + '/instance_methods'
5
+
6
+ module ActsAsCleo
7
+ include ActsMethods
8
+
9
+ end
10
+
11
+ ActiveRecord::Base.extend ActsAsCleo
@@ -2,11 +2,11 @@ module ActsAsCleo
2
2
  module InstanceMethods
3
3
  # callback hooks to keep cleo insync with data
4
4
  def create_cleo
5
- Cleo.create(self)
5
+ Cleo::ElementServer.create(self)
6
6
  end
7
7
 
8
8
  def update_cleo
9
- Cleo.update(self)
9
+ Cleo::ElementServer.update(self)
10
10
  end
11
11
 
12
12
  def remove_from_cleo
@@ -14,12 +14,12 @@ module ActsAsCleo
14
14
  current_cleo_id = self.cleo_id
15
15
  cr.delete
16
16
 
17
- Cleo.delete(current_cleo_id)
17
+ Cleo::ElementServer.delete(current_cleo_id)
18
18
  end
19
19
 
20
20
  def set_cleo_id
21
- cr = Cleo::Reference.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
22
- cr = Cleo::Reference.create(:record_type => record_type, :record_id => self.id) if cr.nil?
21
+ cr = Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
22
+ cr = Cleo::Element.create(:record_type => record_type, :record_id => self.id) if cr.nil?
23
23
  end#
24
24
  #end callback hooks
25
25
 
@@ -32,8 +32,8 @@ module ActsAsCleo
32
32
  end
33
33
 
34
34
  def to_cleo_result
35
- #take self and change it into a Cleo::Result and return
36
- cr = Cleo::Result.new
35
+ #take self and change it into a Cleo::Xml::Result and return
36
+ cr = Cleo::Xml::Result.new
37
37
  cr.term = []
38
38
 
39
39
  to_process = []
@@ -70,7 +70,7 @@ module ActsAsCleo
70
70
  end
71
71
 
72
72
  def cleo_reference
73
- Cleo::Reference.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
73
+ Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
74
74
  end
75
75
 
76
76
  private
@@ -0,0 +1,13 @@
1
+ #Load Cleo
2
+ require File.dirname(__FILE__ + '/cleo')
3
+
4
+ #Load CleoConnection modules
5
+ %w{acts_methods class_methods instance_methods}.each do |l|
6
+ require File.dirname(__FILE__ + "/acts_as_cleo_connection/#{l}"
7
+ end
8
+
9
+ module ActsAsCleoConnection
10
+ include ActsMethods
11
+ end
12
+
13
+ ActiveRecord::Base.extend ActsAsCleoConnection
@@ -0,0 +1,22 @@
1
+ module ActsAsCleoConnection
2
+ module ActsMethods
3
+ def acts_as_cleo_connection(opts = {})
4
+ #push in Instance Methods
5
+ include InstanceMethods
6
+
7
+ after_create :create_cleo_connection
8
+ after_update :update_cleo_connection
9
+ before_destroy :remove_cleo_connection
10
+
11
+ #Target and Origin calls
12
+ cattr_accessor :cleo_target
13
+ cattr_accessor :cleo_origin
14
+ cattr_accessor :cleo_type
15
+
16
+ #Set taggign calls
17
+ self.cleo_origin = opts[:origin]
18
+ self.cleo_target = opts[:target]
19
+ self.cleo_type = opts[:type]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module ActsAsCleoConnection
2
+ module InstanceMethods
3
+ def create_cleo_connection
4
+ Cleo::Connection.create(self)
5
+ end
6
+
7
+ def update_cleo_connection
8
+ Cleo::Connection.update(self)
9
+ end
10
+
11
+ def remove_cleo_connection
12
+ Cleo::Connection.delete(self)
13
+ end
14
+
15
+ def disable_cleo_connection
16
+ Cleo::Connection.disable(self)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module Cleo
2
+ class Base
3
+ cattr_accessor :async
4
+ cattr_accessor :queue
5
+ cattr_accessor :base_url
6
+ cattr_accessor :auto_flush
7
+
8
+ def self.async?
9
+ async == true
10
+ end
11
+
12
+ def self.auto_flush?
13
+ auto_flush == true
14
+ end
15
+
16
+ def self.good_response_code?(response)
17
+ case response
18
+ when Net::HTTPOK
19
+ Cleo.flush if self.auto_flush?
20
+ true # success response
21
+ when Net::HTTPClientError, Net::HTTPInternalServerError
22
+ false # non-success response
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/cleo/cleo.rb ADDED
@@ -0,0 +1,67 @@
1
+ #Load Cleo Sub Modules
2
+ require File.dirname(__FILE__) + '/base_server'
3
+ require File.dirname(__FILE__) + '/connection_server'
4
+ require File.dirname(__FILE__) + '/element_server'
5
+ require File.dirname(__FILE__) + '/service'
6
+
7
+ module Cleo
8
+ def self.query(query)
9
+ Cleo::ElementServer.query(query)
10
+ end
11
+
12
+ def self.net_http
13
+ uri = URI.parse(Cleo::Service.url)
14
+ Net::HTTP.new(uri.host, uri.port)
15
+ end
16
+
17
+ def self.get(uri)
18
+ response = net_http.request(Net::HTTP::Get.new(uri.request_uri))
19
+
20
+ return response if Cleo::Service.good_response_code?(response)
21
+ end
22
+
23
+ def self.flush
24
+ uri = URI.parse Cleo::Service.url + "elements/flush"
25
+ request = Net::HTTP::Post.new(uri.path)
26
+ Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
27
+ end
28
+
29
+ def self.configure(new_config)
30
+ parts = new_config[:url].split("/")
31
+ parts -= %w{rest elements connections}
32
+ parts += %w{rest}
33
+
34
+ Cleo::Service.base_url = parts.join('/') + '/'
35
+
36
+ Cleo::ElementServer.url = (parts + %w{elements}).join('/') + '/'
37
+ Cleo::ConnectionServer.url = (parts + %w{connections}).join('/') + '/'
38
+
39
+ if new_config.has_key?(:async)
40
+ Cleo::Service.async = new_config[:async]
41
+ elsif new_config.has_key?(:run_async)
42
+ Cleo::Service.async = new_config[:run_async]
43
+ else
44
+ Cleo::Service.async = false
45
+ end
46
+
47
+ Cleo::Service.auto_flush = new_config.has_key?(:auto_flush) ? new_config[:auto_flush] : true
48
+ Cleo::Service.queue = new_config.has_key?(:queue) ? new_config[:queue] : "cleo"
49
+
50
+ if new_config.has_key?(:auto_enable_queue) && new_config[:auto_enable_queue]
51
+ env = ENV['QUEUE'] || ''
52
+ ENV['QUEUE'] = (env.split(',') << Cleo::Service.queue).uniq.join(',')
53
+ end
54
+ end
55
+
56
+ def self.load_configuration
57
+ cleo_file_path = File.join( Rails.root, 'config', 'cleo.yml' )
58
+
59
+ if File.exists?( cleo_file_path )
60
+ Cleo.configure(YAML::load_file( cleo_file_path )[Rails.env].symbolize_keys)
61
+ true
62
+ else
63
+ raise ArgumentError.new("No configuration file found. Check for cleo.yml")
64
+ false
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,50 @@
1
+ module Cleo
2
+ class ConnectionServer < Cleo::Base
3
+ cattr_accessor :url
4
+
5
+ #define delete, update, create dynamically in order to set up reddis backed calls if enabled
6
+ #Cleo.update(obj) will respect async settings
7
+ %w{delete update create disable}.each do |mn|
8
+ define_singleton_method(mn.to_s) do |obj|
9
+ if Cleo::Service.async?
10
+ Resque.enqueue(Cleo::ConnectionProcessor, mn, obj.class.name, obj.id)
11
+ else
12
+ Cleo::ConnectionServer.send("execute_#{mn}".to_sym, obj)
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.execute_create(con)
18
+ end
19
+
20
+ def self.execute_disable(con)
21
+ end
22
+
23
+ def self.execute_delete(con)
24
+ self.execute_disable(con)
25
+ end
26
+
27
+ def self.execute_update(con)
28
+ source = con.send("#{cleo_source}")
29
+ target = con.send("#{cleo_target}")
30
+
31
+ sender = Cleo::Xml::Connection.new(:source => source.cleo_id,
32
+ :target => target.cleo_id,
33
+ :active => con.active,
34
+ :strength => con.strength,
35
+ :type => con.cleo_type
36
+ )
37
+
38
+ uri = URI.parse Cleo::Service.connection_url + "_"
39
+ request = Net::HTTP::Post.new(uri.path)
40
+
41
+ request.body = sender.to_xml
42
+ request.content_type = 'application/xml'
43
+
44
+ response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
45
+
46
+ return good_response_code?(response)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Cleo
2
+ class Element < ActiveRecord::Base
3
+ self.table_name = 'references'
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ module Cleo
2
+ class ElementServer < Cleo::Base
3
+ cattr_accessor :url
4
+
5
+ #define delete, update, create dynamically in order to set up reddis backed calls if enabled
6
+ #Cleo.update(obj) will respect async settings
7
+ %w{delete update create}.each do |mn|
8
+ define_singleton_method(mn.to_s) do |obj|
9
+ if Cleo::Service.async?
10
+ Resque.enqueue(Cleo::ElementProcessor, mn, obj.class.name, obj.id)
11
+ else
12
+ Cleo::ElementServer.send("execute_#{mn}".to_sym, obj)
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.find(id)
18
+ #query by element/id to get from cleo fast
19
+ uri = URI.parse(Cleo::Service.element_server_url + "#{id}")
20
+ response = Cleo.get(uri)
21
+
22
+ return nil if response.body.blank?
23
+
24
+ Cleo::Xml::Result.parse(response.body, :single => true)
25
+ end
26
+
27
+ def self.query(query_param)
28
+ uri = URI.parse(Cleo::Service.element_server_url + "search?query=#{CGI::escape query_param}")
29
+ response = Cleo.get(uri)
30
+
31
+ Cleo::Xml::Result.parse(response.body, :single => false)
32
+ end
33
+
34
+ def self.execute_update(obj)
35
+ obj = obj.to_cleo_result unless obj.is_a?(Cleo::Xml::Result)
36
+
37
+ uri = URI.parse Cleo::Service.element_server_url + "#{obj.id}"
38
+ request = Net::HTTP::Put.new(uri.path)
39
+
40
+ request.content_type = 'application/xml'
41
+ request.body = obj.to_xml
42
+ response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
43
+
44
+ return good_response_code?(response)
45
+ end
46
+
47
+ def self.execute_delete(obj_or_id)
48
+ cleo_id = nil
49
+
50
+ if obj_or_id.is_a?(Fixnum)
51
+ cleo_id = obj_or_id
52
+ elsif obj_or_id.respond_to?("cleo_id")
53
+ cleo_id = obj_or_id.cleo_id
54
+ elsif obj_or_id.is_a?(Cleo::Xml::Result)
55
+ cleo_id = obj_or_id.id
56
+ end
57
+
58
+ uri = URI.parse Cleo::Service.element_server_url + "#{cleo_id}"
59
+ request = Net::HTTP::Delete.new(uri.path)
60
+
61
+ response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
62
+
63
+ return good_response_code?(response)
64
+ end
65
+
66
+ def self.execute_create(obj)
67
+ obj = obj.to_cleo_result unless obj.is_a?(Cleo::Xml::Result)
68
+
69
+ uri = URI.parse self.url + "_"
70
+ request = Net::HTTP::Post.new(uri.path)
71
+
72
+ request.body = obj.to_xml
73
+ request.content_type = 'application/xml'
74
+
75
+ response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
76
+
77
+ return good_response_code?(response)
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,16 @@
1
+ module Cleo
2
+ class Service < Cleo::Base
3
+ def self.url
4
+ return self.base_url
5
+ end
6
+
7
+ def self.connection_server_url
8
+ return Cleo::ConnectionServer.url.blank? ? nil : Cleo::ConnectionServer.url
9
+ end
10
+
11
+ def self.element_server_url
12
+ return Cleo::ElementServer.url.blank? ? nil : Cleo::ElementServer.url
13
+ end
14
+
15
+ end
16
+ end
@@ -1 +1 @@
1
- Cleo::Server.load_configuration
1
+ Cleo.load_configuration
@@ -0,0 +1,17 @@
1
+ module Cleo
2
+ module ConnectionProcessor
3
+ @queue = Cleo::ConnectionServer.queue
4
+ def self.perform(operation, klass, klass_id)
5
+ on_me = Kernel.const_get(klass).find(klass_id)
6
+
7
+ case operation.downcase
8
+ when "create"
9
+ Cleo::ConnectionServer.execute_create(on_me)
10
+ when "update"
11
+ Cleo::ConnectionServer.execute_update(on_me)
12
+ when "delete"
13
+ Cleo::ConnectionServer.execute_delete(on_me)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,16 +1,15 @@
1
1
  module Cleo
2
- module Processor
3
- @queue = Cleo::Server.queue
4
-
2
+ module ElementProcessor
3
+ @queue = Cleo::ElementServer.queue
5
4
  def self.perform(operation, klass, klass_id)
6
5
  on_me = Kernel.const_get(klass).find(klass_id)
7
6
  case operation.downcase
8
7
  when "create"
9
- Cleo.execute_create(on_me)
8
+ Cleo::ElementServer.execute_create(on_me)
10
9
  when "update"
11
- Cleo.execute_update(on_me)
10
+ Cleo::ElementServer.execute_update(on_me)
12
11
  when "delete"
13
- Cleo.execute_delete(on_me)
12
+ Cleo::ElementServer.execute_delete(on_me)
14
13
  end
15
14
  end
16
15
  end
@@ -0,0 +1,18 @@
1
+ require 'happymapper'
2
+
3
+ module Cleo
4
+ module Xml
5
+ class Connection
6
+ include HappyMapper
7
+ tag 'connection'
8
+
9
+ element :source, Integer
10
+ element :target, Integer
11
+
12
+ element :active, Boolean
13
+ element :strength, Float
14
+ element :timestamp, Time
15
+ element :type, String
16
+ end
17
+ end
18
+ end
data/lib/xml/result.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'happymapper'
2
+
3
+ module Cleo
4
+ module Xml
5
+ class Result
6
+ include HappyMapper
7
+ tag 'element'
8
+
9
+ has_many :term, String, :tag => 'term'
10
+ element :id, Integer
11
+ element :name, String
12
+ element :score, Float
13
+ element :timestamp, Time
14
+ element :title, String
15
+ element :url, String
16
+
17
+ alias :terms :term
18
+ end
19
+ end
20
+ end
data/test/config/cleo.yml CHANGED
@@ -1,16 +1,16 @@
1
1
  # Config file for the acts_as_cleo plugin.
2
2
  development:
3
- url: "http://localhost:8080/cleo-primer/"
3
+ url: "http://localhost:3030/cleo-primer/"
4
4
 
5
5
  test:
6
- url: "http://localhost:8080/cleo-primer/"
6
+ url: "http://localhost:3030/cleo-primer/"
7
7
 
8
8
  production:
9
- url: "http://localhost:8080/cleo-primer/"
9
+ url: "http://localhost:3030/cleo-primer/"
10
10
  run_async: true
11
11
  queue: "cleo"
12
12
 
13
13
  async_test:
14
- url: "http://localhost:8080/cleo-primer/"
14
+ url: "http://localhost:3030/cleo-primer/"
15
15
  run_async: true
16
16
  queue: "cleo"
Binary file
data/test/helper.rb CHANGED
@@ -8,9 +8,20 @@ require 'bundler'
8
8
  require 'active_record'
9
9
  require 'net/http'
10
10
 
11
- #require acts_as_cleo
12
11
 
13
- require File.dirname(__FILE__) + '/../lib/acts_as_cleo'
12
+ #load Cleo Module for result and server
13
+ require File.dirname(__FILE__) + '/../lib/cleo/cleo'
14
+
15
+ #load Cleo reference
16
+ require File.dirname(__FILE__) + '/../lib/cleo/element'
17
+
18
+ #load xml elements
19
+ require File.dirname(__FILE__) + '/../lib/xml/connection'
20
+ require File.dirname(__FILE__) + '/../lib/xml/result'
21
+
22
+ #require acts_as_cleo
23
+ require File.dirname(__FILE__) + '/../lib/acts_as_cleo/acts_as_cleo'
24
+ #require File.dirname(__FILE__) + '/../lib/acts_as_cleo_connection'
14
25
 
15
26
  begin
16
27
  Bundler.setup(:default, :development)
@@ -42,7 +53,7 @@ models_dir = File.join(file_dirname, 'models')
42
53
  Dir[ models_dir + '/*.rb'].each { |m| require m }
43
54
 
44
55
  server_config = {:url => "http://localhost:8080/cleo-primer/", :run_async => false}
45
- Cleo::Server.configure server_config
56
+ Cleo.configure server_config
46
57
 
47
58
  class Test::Unit::TestCase
48
59
  def setup
@@ -4,29 +4,29 @@ class TestActiveRecordCallbacHooks < Test::Unit::TestCase
4
4
  should "have all callbacks functioning properly" do
5
5
  book = Book.new(:name => "set cleo id", :author => "Rob Meyer")
6
6
  assert_nil book.cleo_id
7
- assert_nil Cleo::Reference.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book.id])
7
+ assert_nil Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book.id])
8
8
 
9
9
  assert book.save!
10
10
  book_id = book.id
11
11
 
12
12
  assert book.cleo_id
13
- ref = Cleo::Reference.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book_id])
13
+ ref = Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book_id])
14
14
  assert_not_nil ref
15
15
 
16
16
  assert book.destroy
17
- assert_nil Cleo::Reference.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book_id])
18
- assert_nil Cleo.find(ref.id)
17
+ assert_nil Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", "Book", book_id])
18
+ assert_nil Cleo::ElementServer.find(ref.id)
19
19
  end
20
20
 
21
21
  should "update cleo on record update" do
22
22
  book = Book.new(:name => "update cleo", :author => "Rob Meyer")
23
23
  assert book.save!
24
- cleo_record = Cleo.find(book.cleo_id)
24
+ cleo_record = Cleo::ElementServer.find(book.cleo_id)
25
25
  assert_not_nil cleo_record
26
26
  book.name = "Updated my Cleo"
27
27
  assert book.save!
28
28
 
29
- updated_cleo_record = Cleo.find(book.cleo_id)
29
+ updated_cleo_record = Cleo::ElementServer.find(book.cleo_id)
30
30
  assert_equal book.name.downcase, updated_cleo_record.name
31
31
  assert_not_equal cleo_record.name, updated_cleo_record.name
32
32
 
@@ -7,11 +7,11 @@ class TestClassMethods < Test::Unit::TestCase
7
7
 
8
8
  should "find record with book name" do
9
9
  book = Book.create(:name => "Otherland", :author => "Tad Williams")
10
- assert Cleo.create(book)
10
+ assert Cleo::ElementServer.create(book)
11
11
  book_cleo_id = book.cleo_id
12
- assert Cleo.find(book_cleo_id)
12
+ assert Cleo::ElementServer.find(book_cleo_id)
13
13
 
14
- assert Cleo.delete(book_cleo_id), "Didn't Delete"
14
+ assert Cleo::ElementServer.delete(book_cleo_id), "Didn't Delete"
15
15
  end
16
16
 
17
17
  should "be able to query from Klass" do
@@ -4,69 +4,48 @@ class TestCleo < Test::Unit::TestCase
4
4
  def setup
5
5
  file = File.open("#{Dir.pwd}/test/data/xml/cleo/testing_element.xml")
6
6
  contents = file.read
7
- @wedge = Cleo::Result.parse(contents, :singluar => true)
7
+ @wedge = Cleo::Xml::Result.parse(contents, :singluar => true)
8
8
  end
9
9
 
10
10
  should "create then destroy Testing Object " do
11
- code = Cleo.create(@wedge)
11
+ code = Cleo::ElementServer.create(@wedge)
12
12
  assert code
13
13
 
14
- wa = Cleo.find(@wedge.id)
14
+ wa = Cleo::ElementServer.find(@wedge.id)
15
15
  assert_not_nil wa
16
16
  assert_equal @wedge.id, wa.id
17
17
 
18
- assert Cleo.delete(wa.id), "Didn't delete"
18
+ assert Cleo::ElementServer.delete(wa.id), "Didn't delete"
19
19
  end
20
20
 
21
- should "get google results from qery" do
22
- r = Cleo.query("goog")
23
- assert_not_nil r
24
- assert_equal 0, r.count
25
- assert_kind_of Array, r
26
- end
27
-
28
- =begin
29
- This is a bad test and require default value to be in Cleo. Baddd test
30
- should "fetch the same record by id" do
31
- r = Cleo.query("goog").first
32
- assert_equal r.id.blank?, false
33
-
34
-
35
- i = Cleo.find(r.id)
36
- assert_equal r.timestamp, i.timestamp
37
- assert_equal r.id, i.id
38
- end
39
- =end
40
-
41
21
  should "create, update, then destroy, Testing Object" do
42
- code = Cleo.create(@wedge)
22
+ code = Cleo::ElementServer.create(@wedge)
43
23
  assert code
44
24
 
45
- wa = Cleo.find(@wedge.id)
25
+ wa = Cleo::ElementServer.find(@wedge.id)
46
26
  assert_not_nil wa
47
27
  assert_equal @wedge.id, wa.id
48
28
 
49
29
  wa.name = "Luke Skywalker"
50
- assert Cleo.update(wa)
30
+ assert Cleo::ElementServer.update(wa)
51
31
 
52
- wa_update = Cleo.find(@wedge.id)
32
+ wa_update = Cleo::ElementServer.find(@wedge.id)
53
33
  assert_equal "Luke Skywalker", wa_update.name
54
34
 
55
- assert Cleo.delete(wa.id), "didn't delete"
35
+ assert Cleo::ElementServer.delete(wa.id), "didn't delete"
56
36
  end
57
37
 
58
38
  should "change the configuration of the server" do
59
39
  server_config = {:url => "http://localhost:8080/cleo-primer/", :run_async => true, :queue => "cleo"}
60
40
 
61
- Cleo::Server.configure server_config
62
- assert Cleo::Server.async?
63
- assert "cleo", Cleo::Server.queue.to_s
41
+ Cleo.configure server_config
42
+ assert Cleo::Service.async?
43
+ assert "cleo", Cleo::Service.queue.to_s
64
44
 
65
45
 
66
46
  server_config = {:url => "http://localhost:8080/cleo-primer/", :run_async => false}
67
- Cleo::Server.configure server_config
68
- assert_same false, Cleo::Server.async?
69
- assert "cleo", Cleo::Server.queue.to_s
47
+ Cleo.configure server_config
48
+ assert_same false, Cleo::Service.async?
49
+ assert "cleo", Cleo::Service.queue.to_s
70
50
  end
71
51
  end
72
-
@@ -13,6 +13,6 @@ class TestModelToCleo < Test::Unit::TestCase
13
13
 
14
14
  assert_equal cb.name, b.name.downcase
15
15
  assert_equal cb.score, b.cleo_score
16
- assert_equal 2, cb.terms.count
16
+ assert_equal 3, cb.terms.count
17
17
  end
18
18
  end
@@ -4,7 +4,7 @@ class TestResult < Test::Unit::TestCase
4
4
  def test_parser_gets_correct_element
5
5
  file = File.open("#{Dir.pwd}/test/data/xml/cleo/element.xml")
6
6
  contents = file.read
7
- cr = Cleo::Result.parse(contents, :singluar => true)
7
+ cr = Cleo::Xml::Result.parse(contents, :singluar => true)
8
8
 
9
9
  assert_not_equal cr, nil
10
10
  assert_equal cr.name, "Cleo Inc."
@@ -13,7 +13,7 @@ class TestResult < Test::Unit::TestCase
13
13
  def test_parser_gets_many_elements
14
14
  file = File.open("#{Dir.pwd}/test/data/xml/cleo/elements.xml")
15
15
  contents = file.read
16
- cr = Cleo::Result.parse(contents)
16
+ cr = Cleo::Xml::Result.parse(contents)
17
17
 
18
18
  assert_equal 7, cr.count
19
19
  assert_match /google/i, cr.first.name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_cleo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.5
4
+ version: 3.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-29 00:00:00.000000000Z
12
+ date: 2012-04-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: happymapper
16
- requirement: &70133557417120 !ruby/object:Gem::Requirement
16
+ requirement: &70296838476020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70133557417120
24
+ version_requirements: *70296838476020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &70133557416640 !ruby/object:Gem::Requirement
27
+ requirement: &70296838475540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70133557416640
35
+ version_requirements: *70296838475540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: resque
38
- requirement: &70133557416160 !ruby/object:Gem::Requirement
38
+ requirement: &70296838475060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70133557416160
46
+ version_requirements: *70296838475060
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shoulda
49
- requirement: &70133557415680 !ruby/object:Gem::Requirement
49
+ requirement: &70296838474580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70133557415680
57
+ version_requirements: *70296838474580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rdoc
60
- requirement: &70133557415200 !ruby/object:Gem::Requirement
60
+ requirement: &70296838474100 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.12'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70133557415200
68
+ version_requirements: *70296838474100
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
- requirement: &70133557414720 !ruby/object:Gem::Requirement
71
+ requirement: &70296838473620 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.0.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70133557414720
79
+ version_requirements: *70296838473620
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &70133557414240 !ruby/object:Gem::Requirement
82
+ requirement: &70296838473140 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.8.3
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70133557414240
90
+ version_requirements: *70296838473140
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: simplecov
93
- requirement: &70133557413760 !ruby/object:Gem::Requirement
93
+ requirement: &70296838472660 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70133557413760
101
+ version_requirements: *70296838472660
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: ruby-debug19
104
- requirement: &70133557413280 !ruby/object:Gem::Requirement
104
+ requirement: &70296838472180 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70133557413280
112
+ version_requirements: *70296838472180
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: fixtures
115
- requirement: &70133557412800 !ruby/object:Gem::Requirement
115
+ requirement: &70296838471700 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70133557412800
123
+ version_requirements: *70296838471700
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: sqlite3
126
- requirement: &70133557412320 !ruby/object:Gem::Requirement
126
+ requirement: &70296838471220 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70133557412320
134
+ version_requirements: *70296838471220
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: database_cleaner
137
- requirement: &70133557411840 !ruby/object:Gem::Requirement
137
+ requirement: &70296838470740 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,7 +142,7 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70133557411840
145
+ version_requirements: *70296838470740
146
146
  description: LinkedIn Open Source type ahead tool's REST API as a ruby gem. Now with
147
147
  Reddis support.
148
148
  email: Blue.Dog.Archolite@gmail.com
@@ -152,6 +152,7 @@ extra_rdoc_files:
152
152
  - LICENSE.txt
153
153
  - README.rdoc
154
154
  files:
155
+ - .rvmrc
155
156
  - Gemfile
156
157
  - LICENSE.txt
157
158
  - README.rdoc
@@ -159,23 +160,32 @@ files:
159
160
  - VERSION
160
161
  - acts_as_cleo.gemspec
161
162
  - lib/acts_as_cleo.rb
163
+ - lib/acts_as_cleo/acts_as_cleo.rb
162
164
  - lib/acts_as_cleo/acts_methods.rb
163
165
  - lib/acts_as_cleo/class_methods.rb
164
166
  - lib/acts_as_cleo/instance_methods.rb
165
167
  - lib/acts_as_cleo/private_methods.rb
166
168
  - lib/acts_as_cleo/result.rb
167
169
  - lib/acts_as_cleo/version.rb
168
- - lib/cleo.rb
169
- - lib/cleo/reference.rb
170
- - lib/cleo/result.rb
171
- - lib/cleo/server.rb
170
+ - lib/acts_as_cleo_connection/acts_as_cleo_connection.rb
171
+ - lib/acts_as_cleo_connection/acts_methods.rb
172
+ - lib/acts_as_cleo_connection/instance_methods.rb
173
+ - lib/cleo/base_server.rb
174
+ - lib/cleo/cleo.rb
175
+ - lib/cleo/connection_server.rb
176
+ - lib/cleo/element.rb
177
+ - lib/cleo/element_server.rb
178
+ - lib/cleo/service.rb
172
179
  - lib/generators/acts_as_cleo/install/install_generator.rb
173
180
  - lib/generators/acts_as_cleo/install/templates/cleo.yml
174
181
  - lib/generators/acts_as_cleo/install/templates/cleo_initalizer.rb
175
182
  - lib/generators/acts_as_cleo/install/templates/install.rb
176
- - lib/generators/acts_as_cleo/install/templates/jobs/cleo_processor.rb
183
+ - lib/generators/acts_as_cleo/install/templates/jobs/cleo_connection_processor.rb
184
+ - lib/generators/acts_as_cleo/install/templates/jobs/cleo_element_processor.rb
177
185
  - lib/generators/acts_as_cleo/install/templates/update.rb
178
186
  - lib/generators/acts_as_cleo/update/update_generator.rb
187
+ - lib/xml/connection.rb
188
+ - lib/xml/result.rb
179
189
  - test/config/cleo.yml
180
190
  - test/data/xml/cleo/element.xml
181
191
  - test/data/xml/cleo/elements.xml
@@ -188,6 +198,7 @@ files:
188
198
  - test/db/migrate/005_create_authors.rb
189
199
  - test/db/migrate/006_create_postings.rb
190
200
  - test/db/migrate/007_cleo_table.rb
201
+ - test/db/testing_db.sqlite
191
202
  - test/fixtures/authors.yml
192
203
  - test/fixtures/books.yml
193
204
  - test/fixtures/categories.yml
data/lib/cleo.rb DELETED
@@ -1,116 +0,0 @@
1
- module Cleo
2
- @@net_http = nil
3
-
4
- #define delete, update, create dynamically in order to set up reddis backed calls if enabled
5
- #Cleo.update(obj) will respect async settings
6
- %w{delete update create}.each do |mn|
7
- define_singleton_method(mn.to_s) do |obj|
8
- if Cleo::Server.async?
9
- Resque.enqueue(Cleo::Processor, mn, obj.class.name, obj.id)
10
- else
11
- Cleo.send("execute_#{mn}".to_sym, obj)
12
- end
13
- end
14
- end
15
-
16
- def self.find(id)
17
- #query by element/id to get from cleo fast
18
- uri = URI.parse(Cleo::Server.url + "#{id}")
19
- response = get(uri)
20
-
21
- return nil if response.body.blank?
22
-
23
- Cleo::Result.parse(response.body, :single => true)
24
- end
25
-
26
- def self.query(query_param)
27
- uri = URI.parse(Cleo::Server.url + "search?query=#{CGI::escape query_param}")
28
- response = get(uri)
29
-
30
- Cleo::Result.parse(response.body, :single => false)
31
- end
32
-
33
- def self.execute_update(obj)
34
- obj = obj.to_cleo_result unless obj.is_a?(Cleo::Result)
35
-
36
- uri = URI.parse Cleo::Server.url + "#{obj.id}"
37
- request = Net::HTTP::Put.new(uri.path)
38
-
39
- request.content_type = 'application/xml'
40
- request.body = obj.to_xml
41
- response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
42
-
43
- return good_response_code?(response)
44
- end
45
-
46
-
47
- def self.execute_delete(obj_or_id)
48
-
49
- cleo_id = nil
50
-
51
- if obj_or_id.is_a?(Cleo::Result)
52
- cleo_id = obj_or_id.id
53
- elsif obj_or_id.is_a?(Fixnum)
54
- cleo_id = obj_or_id
55
- elsif obj_or_id.respond_to?("cleo_id")
56
- cleo_id = obj_or_id.cleo_id
57
- end
58
-
59
- uri = URI.parse Cleo::Server.url + "#{cleo_id}"
60
- request = Net::HTTP::Delete.new(uri.path)
61
-
62
- response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
63
-
64
- return good_response_code?(response)
65
- end
66
-
67
- def self.execute_create(obj)
68
- obj = obj.to_cleo_result unless obj.is_a?(Cleo::Result)
69
-
70
- uri = URI.parse Cleo::Server.url + "_"
71
- request = Net::HTTP::Post.new(uri.path)
72
-
73
- request.body = obj.to_xml
74
- request.content_type = 'application/xml'
75
-
76
- response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
77
-
78
- return good_response_code?(response)
79
- end
80
-
81
- private
82
- def self.net_http
83
- @@net_http unless @@net_http.blank?
84
- uri = URI.parse(Cleo::Server.url)
85
-
86
- @@net_http = Net::HTTP.new(uri.host, uri.port)
87
- end
88
-
89
- def self.get(uri)
90
- response = net_http.request(Net::HTTP::Get.new(uri.request_uri))
91
-
92
- return response if good_response_code?(response)
93
- end
94
-
95
- private
96
- def self.flush
97
- uri = URI.parse Cleo::Server.url + "flush"
98
- request = Net::HTTP::Post.new(uri.path)
99
- Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
100
- end
101
-
102
- def self.good_response_code?(response)
103
- case response
104
- when Net::HTTPOK
105
- flush if Cleo::Server.auto_flush?
106
- true # success response
107
- when Net::HTTPClientError, Net::HTTPInternalServerError
108
- false # non-success response
109
- end
110
- end
111
- end
112
-
113
- #require Result Class
114
- require 'cleo/server'
115
- require 'cleo/result'
116
- require 'cleo/reference'
@@ -1,5 +0,0 @@
1
- module Cleo
2
- class Reference < ActiveRecord::Base
3
-
4
- end
5
- end
data/lib/cleo/result.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'happymapper'
2
-
3
- module Cleo
4
- class Result
5
- include HappyMapper
6
- tag 'element'
7
-
8
- has_many :term, String, :tag => 'term'
9
- element :id, Integer
10
- element :name, String
11
- element :score, Float
12
- element :timestamp, Time
13
- element :title, String
14
- element :url, String
15
-
16
- alias :terms :term
17
- end
18
- end
data/lib/cleo/server.rb DELETED
@@ -1,53 +0,0 @@
1
- module Cleo
2
- class Server
3
- @@configuration= {:url => 'http://cleo.testingserver.com/cleo-primer/', :run_async => false, :queue => "cleo"}
4
-
5
- def self.configure(new_config)
6
- parts = new_config[:url].split("/")
7
- parts -= %w{rest elements}
8
- parts += %w{rest elements}
9
- @@configuration[:url] = parts.join('/') + '/'
10
-
11
- if new_config.has_key?(:async)
12
- @@configuration[:async] = new_config[:async]
13
- elsif new_config.has_key?(:run_async)
14
- @@configuration[:async] = new_config[:run_async]
15
- else
16
- @@configuration[:async] = false
17
- end
18
-
19
- @@configuration[:auto_flush] = new_config.has_key?(:auto_flush) ? new_config[:auto_flush] : true
20
- @@configuration[:queue] = new_config.has_key?(:queue) ? new_config[:queue] : "cleo"
21
-
22
- if new_config.has_key?(:auto_enable_queue) && new_config[:auto_enable_queue]
23
- env = ENV['QUEUE'] || ''
24
- ENV['QUEUE'] = (env.split(',') << @@configuration[:queue]).uniq.join(',')
25
- end
26
- end
27
-
28
- def self.load_configuration
29
- cleo_file_path = File.join( Rails.root, 'config', 'cleo.yml' )
30
-
31
- if File.exists?( cleo_file_path )
32
- Cleo::Server.configure(YAML::load_file( cleo_file_path )[Rails.env].symbolize_keys)
33
- end
34
- end
35
-
36
- #meta these out
37
- def self.url
38
- @@configuration[:url]
39
- end
40
-
41
- def self.auto_flush?
42
- @@configuration[:auto_flush]
43
- end
44
-
45
- def self.async?
46
- @@configuration[:async]
47
- end
48
-
49
- def self.queue
50
- @@configuration[:queue].to_sym
51
- end
52
- end
53
- end