acts_as_cleo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +20 -3
- data/{LICENSE → LICENSE.txt} +1 -3
- data/README.rdoc +24 -0
- data/Rakefile +54 -2
- data/VERSION +1 -0
- data/acts_as_cleo.gemspec +107 -13
- data/config/cleo.yml +10 -0
- data/lib/acts_as_cleo.rb +17 -105
- data/lib/acts_methods.rb +34 -0
- data/lib/class_methods.rb +4 -0
- data/lib/cleo/reference.rb +5 -0
- data/lib/cleo/result.rb +18 -0
- data/lib/cleo/server.rb +16 -0
- data/lib/cleo.rb +95 -0
- data/lib/common_methods.rb +17 -0
- data/lib/instance_methods.rb +45 -0
- data/test/config/cleo.yml +2 -0
- data/test/data/xml/cleo/element.xml +11 -0
- data/test/data/xml/cleo/elements.xml +90 -0
- data/test/data/xml/cleo/testing_element.xml +11 -0
- data/test/db/connection/sqlite.rb +8 -0
- data/test/db/migrate/001_create_books.rb +13 -0
- data/test/db/migrate/002_create_movies.rb +12 -0
- data/test/db/migrate/003_create_categories.rb +11 -0
- data/test/db/migrate/004_create_electronics.rb +15 -0
- data/test/db/migrate/005_create_authors.rb +12 -0
- data/test/db/migrate/006_create_postings.rb +9 -0
- data/test/db/test.db +0 -0
- data/test/fixtures/authors.yml +9 -0
- data/test/fixtures/books.yml +11 -0
- data/test/fixtures/categories.yml +7 -0
- data/test/fixtures/electronics.yml +39 -0
- data/test/fixtures/movies.yml +9 -0
- data/test/helper.rb +38 -0
- data/test/models/author.rb +7 -0
- data/test/models/book.rb +15 -0
- data/test/models/category.rb +7 -0
- data/test/models/electronic.rb +11 -0
- data/test/models/movie.rb +8 -0
- data/test/test_acts_as_cleo.rb +7 -0
- data/test/unit/test_acts_methods.rb +19 -0
- data/test/unit/test_cleo.rb +60 -0
- data/test/unit/test_result_parser.rb +23 -0
- metadata +161 -13
- data/.gitignore +0 -24
- data/README.md +0 -29
data/Gemfile
CHANGED
@@ -1,4 +1,21 @@
|
|
1
|
-
source
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
2
5
|
|
3
|
-
#
|
4
|
-
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development, :test do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "rdoc", "~> 3.12"
|
11
|
+
gem "bundler", ">= 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.8.3"
|
13
|
+
gem "simplecov", ">= 0"
|
14
|
+
gem 'ruby-debug19', :require => 'ruby-debug'
|
15
|
+
gem 'fixtures'
|
16
|
+
gem 'sqlite3'
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
gem 'happymapper'
|
21
|
+
gem 'rails', '>= 3.1.0'
|
data/{LICENSE → LICENSE.txt}
RENAMED
@@ -1,7 +1,5 @@
|
|
1
1
|
Copyright (c) 2012 Robert R. Meyer
|
2
2
|
|
3
|
-
MIT License
|
4
|
-
|
5
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
6
4
|
a copy of this software and associated documentation files (the
|
7
5
|
"Software"), to deal in the Software without restriction, including
|
@@ -19,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= acts_as_cleo
|
2
|
+
|
3
|
+
Acts As Cleo is a Rails gem that allows for easy integration with Linked In's Open Source type ahead manager.
|
4
|
+
|
5
|
+
Verify that you have downloaded and installed cleo locally.
|
6
|
+
|
7
|
+
== Contributing to acts_as_cleo
|
8
|
+
|
9
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
10
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
11
|
+
* Fork the project.
|
12
|
+
* Start a feature/bugfix branch.
|
13
|
+
* Commit and push until you are happy with your contribution.
|
14
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
15
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
16
|
+
|
17
|
+
== To test
|
18
|
+
* rake start:cleo
|
19
|
+
* rake
|
20
|
+
|
21
|
+
== Copyright
|
22
|
+
|
23
|
+
Copyright (c) 2012 Robert R. Meyer. See LICENSE.txt for
|
24
|
+
further details.
|
data/Rakefile
CHANGED
@@ -1,2 +1,54 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup(:default, :development)
|
9
|
+
rescue Bundler::BundlerError => e
|
10
|
+
$stderr.puts e.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit e.status_code
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'jeweler'
|
16
|
+
Jeweler::Tasks.new do |gem|
|
17
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
18
|
+
gem.name = "acts_as_cleo"
|
19
|
+
gem.homepage = "http://github.com/blue-dog-archolite/acts_as_cleo"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = %Q{A Rails enabled Cleo API}
|
22
|
+
gem.description = %Q{LinkedIn Open Source type ahead tool's REST API as a ruby gem}
|
23
|
+
gem.email = "Blue.Dog.Archolite@gmail.com"
|
24
|
+
gem.authors = ["Robert R. Meyer"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
#require 'rcov/rcovtask'
|
37
|
+
#Rcov::RcovTask.new do |test|
|
38
|
+
# test.libs << 'test'
|
39
|
+
# test.pattern = 'test/**/test_*.rb'
|
40
|
+
# test.verbose = true
|
41
|
+
# test.rcov_opts << '--exclude "gems/*"'
|
42
|
+
#end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rdoc/task'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "acts_as_cleo #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/acts_as_cleo.gemspec
CHANGED
@@ -1,17 +1,111 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/acts_as_cleo/version', __FILE__)
|
3
5
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
gem.description = %q{A Cleo Inegration Gem}
|
8
|
-
gem.summary = %q{A Rails interface for the Cleo REST API}
|
9
|
-
gem.homepage = ""
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "acts_as_cleo"
|
8
|
+
s.version = "0.0.2"
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Robert R. Meyer"]
|
12
|
+
s.date = "2012-03-21"
|
13
|
+
s.description = "LinkedIn Open Source type ahead tool's REST API as a ruby gem"
|
14
|
+
s.email = "Blue.Dog.Archolite@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"LICENSE.txt",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"acts_as_cleo.gemspec",
|
26
|
+
"config/cleo.yml",
|
27
|
+
"lib/acts_as_cleo.rb",
|
28
|
+
"lib/acts_as_cleo/result.rb",
|
29
|
+
"lib/acts_as_cleo/version.rb",
|
30
|
+
"lib/acts_methods.rb",
|
31
|
+
"lib/class_methods.rb",
|
32
|
+
"lib/cleo.rb",
|
33
|
+
"lib/cleo/reference.rb",
|
34
|
+
"lib/cleo/result.rb",
|
35
|
+
"lib/cleo/server.rb",
|
36
|
+
"lib/common_methods.rb",
|
37
|
+
"lib/instance_methods.rb",
|
38
|
+
"test/config/cleo.yml",
|
39
|
+
"test/data/xml/cleo/element.xml",
|
40
|
+
"test/data/xml/cleo/elements.xml",
|
41
|
+
"test/data/xml/cleo/testing_element.xml",
|
42
|
+
"test/db/connection/sqlite.rb",
|
43
|
+
"test/db/migrate/001_create_books.rb",
|
44
|
+
"test/db/migrate/002_create_movies.rb",
|
45
|
+
"test/db/migrate/003_create_categories.rb",
|
46
|
+
"test/db/migrate/004_create_electronics.rb",
|
47
|
+
"test/db/migrate/005_create_authors.rb",
|
48
|
+
"test/db/migrate/006_create_postings.rb",
|
49
|
+
"test/db/test.db",
|
50
|
+
"test/fixtures/authors.yml",
|
51
|
+
"test/fixtures/books.yml",
|
52
|
+
"test/fixtures/categories.yml",
|
53
|
+
"test/fixtures/electronics.yml",
|
54
|
+
"test/fixtures/movies.yml",
|
55
|
+
"test/helper.rb",
|
56
|
+
"test/models/author.rb",
|
57
|
+
"test/models/book.rb",
|
58
|
+
"test/models/category.rb",
|
59
|
+
"test/models/electronic.rb",
|
60
|
+
"test/models/movie.rb",
|
61
|
+
"test/test_acts_as_cleo.rb",
|
62
|
+
"test/unit/test_acts_methods.rb",
|
63
|
+
"test/unit/test_cleo.rb",
|
64
|
+
"test/unit/test_result_parser.rb"
|
65
|
+
]
|
66
|
+
s.homepage = "http://github.com/blue-dog-archolite/acts_as_cleo"
|
67
|
+
s.licenses = ["MIT"]
|
68
|
+
s.require_paths = ["lib"]
|
69
|
+
s.rubygems_version = "1.8.17"
|
70
|
+
s.summary = "A Rails enabled Cleo API"
|
71
|
+
|
72
|
+
if s.respond_to? :specification_version then
|
73
|
+
s.specification_version = 3
|
74
|
+
|
75
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
76
|
+
s.add_runtime_dependency(%q<happymapper>, [">= 0"])
|
77
|
+
s.add_runtime_dependency(%q<rails>, [">= 3.1.0"])
|
78
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
79
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
80
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
81
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
82
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
83
|
+
s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
|
84
|
+
s.add_development_dependency(%q<fixtures>, [">= 0"])
|
85
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
86
|
+
else
|
87
|
+
s.add_dependency(%q<happymapper>, [">= 0"])
|
88
|
+
s.add_dependency(%q<rails>, [">= 3.1.0"])
|
89
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
90
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
91
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
92
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
93
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
94
|
+
s.add_dependency(%q<ruby-debug19>, [">= 0"])
|
95
|
+
s.add_dependency(%q<fixtures>, [">= 0"])
|
96
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
97
|
+
end
|
98
|
+
else
|
99
|
+
s.add_dependency(%q<happymapper>, [">= 0"])
|
100
|
+
s.add_dependency(%q<rails>, [">= 3.1.0"])
|
101
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
102
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
103
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
104
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
105
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
106
|
+
s.add_dependency(%q<ruby-debug19>, [">= 0"])
|
107
|
+
s.add_dependency(%q<fixtures>, [">= 0"])
|
108
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
109
|
+
end
|
17
110
|
end
|
111
|
+
|
data/config/cleo.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Config file for the acts_as_solr plugin.
|
2
|
+
#
|
3
|
+
# If you change the host or port number here, make sure you update
|
4
|
+
# them in your Solr config file
|
5
|
+
|
6
|
+
development:
|
7
|
+
url: "http://localhost:8080/cleo-primer/"
|
8
|
+
|
9
|
+
production:
|
10
|
+
url: "http://localhost:8080/cleo-primer/"
|
data/lib/acts_as_cleo.rb
CHANGED
@@ -1,111 +1,23 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
#load Cleo Module for result and server
|
2
|
+
require File.dirname(__FILE__) + '/cleo'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
# end
|
4
|
+
#Load ActsAsCleo method load
|
5
|
+
require File.dirname(__FILE__) + '/acts_methods'
|
6
|
+
require File.dirname(__FILE__) + '/class_methods'
|
7
|
+
require File.dirname(__FILE__) + '/instance_methods'
|
8
|
+
require File.dirname(__FILE__) + '/common_methods'
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
module ClassMethods
|
17
|
-
def acts_as_cleo(opts = {})
|
18
|
-
class_eval <<-EOV
|
19
|
-
include Cleo::ActsAsCleo::InstanceMethods
|
20
|
-
before_create :set_cleo_id
|
21
|
-
after_commit :sync_with_cleo
|
22
|
-
after_destroy :remove_from_cleo
|
23
|
-
|
24
|
-
cattr_accessor :cleo_config
|
25
|
-
|
26
|
-
#set type to query against in Cleo
|
27
|
-
#defaults to class name
|
28
|
-
#ie "User" when acts_as_cleo is included in user.b
|
29
|
-
self.cleo_config = {}
|
30
|
-
self.cleo_config[:type] = opts[:type] || self.ancestors.first.name
|
31
|
-
|
32
|
-
self.cleo_config[:name] = opts[:name]
|
33
|
-
|
34
|
-
#specify what columns to add as part of the xml object
|
35
|
-
#defaults to all columns in database
|
36
|
-
self.cleo_config[:terms] = opts[:terms] || self.column_names
|
37
|
-
self.cleo_config[:terms] = self.cleo_config[:terms] - opts[:except] unless opts[:except].blank?
|
38
|
-
|
39
|
-
#figure out what the score param is. execute the self.send(#{opts[:score].to_s}.count)
|
40
|
-
self.cleo_config[:score] = opts[:score]
|
41
|
-
|
42
|
-
def query(query, opts = {})
|
43
|
-
Cleo.query(query)
|
44
|
-
end
|
45
|
-
|
46
|
-
def cleo_id
|
47
|
-
record_type = self.cleo_config[:type]
|
48
|
-
return nil unless self.id
|
49
|
-
end
|
50
|
-
|
51
|
-
def cleo_id= ( new_id)
|
52
|
-
end
|
53
|
-
|
54
|
-
EOV
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
module InstanceMethods
|
61
|
-
# callback hooks to keep cleo insync with data
|
62
|
-
def sync_with_cleo
|
63
|
-
return if self.is_a?(Moderation)
|
64
|
-
return if self.is_a?(Audit)
|
65
|
-
|
66
|
-
if self.persisted?
|
67
|
-
Cleo.update(self)
|
68
|
-
else
|
69
|
-
Cleo.create(self)
|
70
|
-
end
|
71
|
-
#after_update send the data to cleo
|
72
|
-
end
|
73
|
-
|
74
|
-
def set_cleo_id
|
75
|
-
self.cleo_id = Time.now.to_i unless self.is_a?(Moderation) || self.is_a?(Audit)
|
76
|
-
end
|
77
|
-
|
78
|
-
def remove_from_cleo
|
79
|
-
return if self.is_a?(Moderation)
|
80
|
-
return if self.is_a?(Audit)
|
81
|
-
Cleo.delete(self) if self.persisted?
|
82
|
-
end
|
83
|
-
#end callback hooks
|
84
|
-
|
85
|
-
def to_cleo_result
|
86
|
-
#take self and change it into a Cleo::Result and return
|
87
|
-
cr = Cleo::Result.new
|
88
|
-
cr.term = []
|
89
|
-
|
90
|
-
self.cleo_config[:terms].each do |term|
|
91
|
-
cr.term << self.send(term).to_s
|
92
|
-
end
|
93
|
-
|
94
|
-
cr.term = cr.term.compact.reject(&:blank?)
|
95
|
-
self.cleo_id ||= Time.now.to_i
|
96
|
-
cr.id = self.cleo_id || Time.now.to_i
|
97
|
-
|
98
|
-
cr.name = self.send(self.cleo_config[:name]).to_s
|
99
|
-
cr.name = cr.term.first if cr.name.blank?
|
10
|
+
module ActsAsCleo
|
11
|
+
include ActsMethods
|
12
|
+
end
|
100
13
|
|
101
|
-
|
102
|
-
cr.score = score.nil? ? 0 : score.count
|
14
|
+
ActiveRecord::Base.extend ActsAsCleo
|
103
15
|
|
104
|
-
|
105
|
-
|
16
|
+
root_path = Rails.root.nil? ? "#{File.dirname(File.expand_path(__FILE__))}/../" : Rails.root
|
17
|
+
cleo_file_path = File.join( root_path, 'config', 'cleo.yml' )
|
106
18
|
|
107
|
-
|
108
|
-
|
19
|
+
if File.exists?( cleo_file_path )
|
20
|
+
Cleo::Server.server_locations = YAML::load_file( cleo_file_path )[Rails.env].symbolize_keys
|
21
|
+
else
|
22
|
+
raise LoadError.new("No cleo.yml found. Please viery that you have a copy of cleo.yml in your config directory.")
|
109
23
|
end
|
110
|
-
|
111
|
-
ActiveRecord::Base.send :include, Cleo::ActsAsCleo
|
data/lib/acts_methods.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActsAsCleo
|
2
|
+
module ActsMethods
|
3
|
+
def acts_as_cleo(opts = {})
|
4
|
+
extend ClassMethods
|
5
|
+
include InstanceMethods
|
6
|
+
include CommonMethods
|
7
|
+
|
8
|
+
before_create :set_cleo_id
|
9
|
+
after_commit :sync_with_cleo
|
10
|
+
after_destroy :remove_from_cleo
|
11
|
+
|
12
|
+
cattr_accessor :cleo_config
|
13
|
+
|
14
|
+
#set type to query against in Cleo
|
15
|
+
#defaults to class name
|
16
|
+
#ie "User" when acts_as_cleo is included in user.b
|
17
|
+
self.cleo_config = {}
|
18
|
+
opts[:except] ||= []
|
19
|
+
opts[:except] += %w{id created_at updated_at}
|
20
|
+
self.cleo_config[:type] = opts[:type] || self.ancestors.first.name
|
21
|
+
|
22
|
+
self.cleo_config[:name] = opts[:name].blank? ? "name" : opts[:name]
|
23
|
+
|
24
|
+
#specify what columns to add as part of the xml object
|
25
|
+
#defaults to all columns in database
|
26
|
+
self.cleo_config[:terms] = opts[:terms] || self.column_names
|
27
|
+
self.cleo_config[:terms] = self.cleo_config[:terms] - opts[:except]
|
28
|
+
|
29
|
+
#figure out what the score param is. execute the self.send(#{opts[:score].to_s}.count)
|
30
|
+
opts[:score] ||= "id"
|
31
|
+
self.cleo_config[:score] = opts[:score]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/cleo/result.rb
ADDED
@@ -0,0 +1,18 @@
|
|
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
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Cleo
|
2
|
+
class Server
|
3
|
+
@@server_location = {:url => 'http://cleo.testingserver.com/cleo-primer/' }
|
4
|
+
|
5
|
+
def self.server_locations=(new_locations)
|
6
|
+
parts = new_locations[:url].split("/")
|
7
|
+
parts -= %w{rest elements}
|
8
|
+
parts += %w{rest elements}
|
9
|
+
@@server_location[:url] = parts.join('/') + '/'
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.url
|
13
|
+
@@server_location[:url]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/cleo.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
module Cleo
|
2
|
+
@@net_http = nil
|
3
|
+
|
4
|
+
def self.net_http
|
5
|
+
@@net_http unless @@net_http.blank?
|
6
|
+
uri = URI.parse(Cleo::Server.url)
|
7
|
+
|
8
|
+
@@net_http = Net::HTTP.new(uri.host, uri.port)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.get(uri)
|
12
|
+
response = net_http.request(Net::HTTP::Get.new(uri.request_uri))
|
13
|
+
|
14
|
+
return response if good_response_code?(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.find(id)
|
18
|
+
#query by element/id to get from cleo fast
|
19
|
+
uri = URI.parse(Cleo::Server.url + "#{id}")
|
20
|
+
response = self.get(uri)
|
21
|
+
|
22
|
+
Cleo::Result.parse(response.body, :single => true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.query(query_param)
|
26
|
+
uri = URI.parse(Cleo::Server.url + "search?query=#{CGI::escape query_param}")
|
27
|
+
response = self.get(uri)
|
28
|
+
|
29
|
+
Cleo::Result.parse(response.body, :single => false)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def self.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
|
+
def self.delete(obj)
|
47
|
+
obj = obj.to_cleo_result unless obj.is_a?(Cleo::Result)
|
48
|
+
|
49
|
+
result = `curl -v -X DELETE #{Cleo::Server.url}#{obj.id}`
|
50
|
+
|
51
|
+
#TODO I HATE THIS. Make this use Rails and not a fracking curl call
|
52
|
+
# uri = URI.parse Cleo::Server.url + "/#{obj.id}"
|
53
|
+
# request = Net::HTTP::Delete.new(uri.path)
|
54
|
+
# response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
|
55
|
+
|
56
|
+
|
57
|
+
# uri = URI.parse Cleo::Server.url + "#{obj.id}"
|
58
|
+
|
59
|
+
# request = Net::HTTP::Delete.new("#{obj.id}")
|
60
|
+
# request.content_type = 'application/xml'
|
61
|
+
# response = Net::HTTP.new(uri.host, 8080).start { |http| http.request request }
|
62
|
+
# http = Net::HTTP.new(Cleo::Server.url)
|
63
|
+
# response = http.request(request)
|
64
|
+
|
65
|
+
# return good_response_code?(response)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.create(obj)
|
69
|
+
obj = obj.to_cleo_result unless obj.is_a?(Cleo::Result)
|
70
|
+
|
71
|
+
uri = URI.parse Cleo::Server.url + "_"
|
72
|
+
request = Net::HTTP::Post.new(uri.path)
|
73
|
+
|
74
|
+
request.body = obj.to_xml
|
75
|
+
request.content_type = 'application/xml'
|
76
|
+
|
77
|
+
response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
|
78
|
+
|
79
|
+
return good_response_code?(response)
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def self.good_response_code?(response)
|
84
|
+
case response
|
85
|
+
when Net::HTTPOK
|
86
|
+
true # success response
|
87
|
+
when Net::HTTPClientError, Net::HTTPInternalServerError
|
88
|
+
false # non-success response
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
#require Result Class
|
94
|
+
require 'cleo/server'
|
95
|
+
require 'cleo/result'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActsAsCleo
|
2
|
+
module CommonMethods
|
3
|
+
def query(query, opts = {})
|
4
|
+
Cleo.query(query)
|
5
|
+
end
|
6
|
+
|
7
|
+
def cleo_id
|
8
|
+
record_type = self.cleo_config[:type]
|
9
|
+
cr = Cleo::Reference.find_by_reference_and_id(record_type, self.id)
|
10
|
+
return cr.nil? ? nil : cr.id
|
11
|
+
end
|
12
|
+
|
13
|
+
def cleo_id=( new_id)
|
14
|
+
#this needs to be rerolled to pull an Cleo::Reference record and persist it.
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module ActsAsCleo
|
2
|
+
module InstanceMethods
|
3
|
+
# callback hooks to keep cleo insync with data
|
4
|
+
def sync_with_cleo
|
5
|
+
if self.persisted?
|
6
|
+
Cleo.update(self)
|
7
|
+
else
|
8
|
+
Cleo.create(self)
|
9
|
+
end
|
10
|
+
#after_update send the data to cleo
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_cleo_id
|
14
|
+
#this needs to be rerolled to pull an Cleo::Reference record and persist it.
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove_from_cleo
|
18
|
+
Cleo.delete(self) if self.persisted?
|
19
|
+
end
|
20
|
+
#end callback hooks
|
21
|
+
|
22
|
+
def to_cleo_result
|
23
|
+
#take self and change it into a Cleo::Result and return
|
24
|
+
cr = Cleo::Result.new
|
25
|
+
cr.term = []
|
26
|
+
|
27
|
+
self.cleo_config[:terms].each do |term|
|
28
|
+
cr.term << self.send(term).to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
cr.term = cr.term.compact.reject(&:blank?)
|
32
|
+
cr.id = self.cleo_id
|
33
|
+
|
34
|
+
cr.name = self.send(self.cleo_config[:name]).to_s
|
35
|
+
cr.name = cr.term.first if cr.name.blank?
|
36
|
+
|
37
|
+
score = self.send(self.cleo_config[:score])
|
38
|
+
cr.score = score.nil? ? 0 : score.count
|
39
|
+
|
40
|
+
cr
|
41
|
+
end
|
42
|
+
|
43
|
+
alias :as_cleo :to_cleo_result
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<?xml version="1.0"?>
|
2
|
+
<element>
|
3
|
+
<term>rob</term>
|
4
|
+
<term>inc.</term>
|
5
|
+
<term>goog</term>
|
6
|
+
<id>99999</id>
|
7
|
+
<name>Cleo Inc.</name>
|
8
|
+
<score>159350.0</score>
|
9
|
+
<title>Technology - Computer Software: Programming Data Processing</title>
|
10
|
+
<url>http://www.nasdaq.com/symbol/goog</url>
|
11
|
+
</element>
|