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.
Files changed (46) hide show
  1. data/Gemfile +20 -3
  2. data/{LICENSE → LICENSE.txt} +1 -3
  3. data/README.rdoc +24 -0
  4. data/Rakefile +54 -2
  5. data/VERSION +1 -0
  6. data/acts_as_cleo.gemspec +107 -13
  7. data/config/cleo.yml +10 -0
  8. data/lib/acts_as_cleo.rb +17 -105
  9. data/lib/acts_methods.rb +34 -0
  10. data/lib/class_methods.rb +4 -0
  11. data/lib/cleo/reference.rb +5 -0
  12. data/lib/cleo/result.rb +18 -0
  13. data/lib/cleo/server.rb +16 -0
  14. data/lib/cleo.rb +95 -0
  15. data/lib/common_methods.rb +17 -0
  16. data/lib/instance_methods.rb +45 -0
  17. data/test/config/cleo.yml +2 -0
  18. data/test/data/xml/cleo/element.xml +11 -0
  19. data/test/data/xml/cleo/elements.xml +90 -0
  20. data/test/data/xml/cleo/testing_element.xml +11 -0
  21. data/test/db/connection/sqlite.rb +8 -0
  22. data/test/db/migrate/001_create_books.rb +13 -0
  23. data/test/db/migrate/002_create_movies.rb +12 -0
  24. data/test/db/migrate/003_create_categories.rb +11 -0
  25. data/test/db/migrate/004_create_electronics.rb +15 -0
  26. data/test/db/migrate/005_create_authors.rb +12 -0
  27. data/test/db/migrate/006_create_postings.rb +9 -0
  28. data/test/db/test.db +0 -0
  29. data/test/fixtures/authors.yml +9 -0
  30. data/test/fixtures/books.yml +11 -0
  31. data/test/fixtures/categories.yml +7 -0
  32. data/test/fixtures/electronics.yml +39 -0
  33. data/test/fixtures/movies.yml +9 -0
  34. data/test/helper.rb +38 -0
  35. data/test/models/author.rb +7 -0
  36. data/test/models/book.rb +15 -0
  37. data/test/models/category.rb +7 -0
  38. data/test/models/electronic.rb +11 -0
  39. data/test/models/movie.rb +8 -0
  40. data/test/test_acts_as_cleo.rb +7 -0
  41. data/test/unit/test_acts_methods.rb +19 -0
  42. data/test/unit/test_cleo.rb +60 -0
  43. data/test/unit/test_result_parser.rb +23 -0
  44. metadata +161 -13
  45. data/.gitignore +0 -24
  46. data/README.md +0 -29
@@ -0,0 +1,90 @@
1
+ <?xml version="1.0"?>
2
+ <element-list>
3
+ <element>
4
+ <term>google</term>
5
+ <term>inc.</term>
6
+ <term>goog</term>
7
+ <id>1063</id>
8
+ <name>Google Inc. (GOOG)</name>
9
+ <score>159350.0</score>
10
+ <timestamp>1331833525493</timestamp>
11
+ <title>Technology - Computer Software: Programming Data Processing</title>
12
+ <url>http://www.nasdaq.com/symbol/goog</url>
13
+ </element>
14
+ <element>
15
+ <term>goods</term>
16
+ <term>corporation</term>
17
+ <term>big</term>
18
+ <term>5</term>
19
+ <term>sporting</term>
20
+ <term>bgfv</term>
21
+ <id>303</id>
22
+ <name>Big 5 Sporting Goods Corporation (BGFV)</name>
23
+ <score>212.701</score>
24
+ <timestamp>1331833525461</timestamp>
25
+ <title>Consumer Services - Other Specialty Stores</title>
26
+ <url>http://www.nasdaq.com/symbol/bgfv</url>
27
+ </element>
28
+ <element>
29
+ <term>gladstone</term>
30
+ <term>commercial</term>
31
+ <term>corporation</term>
32
+ <term>good</term>
33
+ <id>1034</id>
34
+ <name>Gladstone Commercial Corporation (GOOD)</name>
35
+ <score>194.274</score>
36
+ <timestamp>1331833525492</timestamp>
37
+ <title>Consumer Services - Real Estate</title>
38
+ <url>http://www.nasdaq.com/symbol/good</url>
39
+ </element>
40
+ <element>
41
+ <term>gladstone</term>
42
+ <term>commercial</term>
43
+ <term>corporation</term>
44
+ <term>goodo</term>
45
+ <id>1035</id>
46
+ <name>Gladstone Commercial Corporation (GOODO)</name>
47
+ <score>28.06</score>
48
+ <timestamp>1331833525492</timestamp>
49
+ <title>Consumer Services - Real Estate</title>
50
+ <url>http://www.nasdaq.com/symbol/goodo</url>
51
+ </element>
52
+ <element>
53
+ <term>gladstone</term>
54
+ <term>commercial</term>
55
+ <term>corporation</term>
56
+ <term>goodp</term>
57
+ <id>1036</id>
58
+ <name>Gladstone Commercial Corporation (GOODP)</name>
59
+ <score>24.5</score>
60
+ <timestamp>1331833525492</timestamp>
61
+ <title>Consumer Services - Real Estate</title>
62
+ <url>http://www.nasdaq.com/symbol/goodp</url>
63
+ </element>
64
+ <element>
65
+ <term>inc.</term>
66
+ <term>real</term>
67
+ <term>goods</term>
68
+ <term>solar</term>
69
+ <term>rsol</term>
70
+ <id>2052</id>
71
+ <name>Real Goods Solar Inc. (RSOL)</name>
72
+ <score>22.5954</score>
73
+ <timestamp>1331833525534</timestamp>
74
+ <title>Capital Goods - Building Products</title>
75
+ <url>http://www.nasdaq.com/symbol/rsol</url>
76
+ </element>
77
+ <element>
78
+ <term>inc.</term>
79
+ <term>good</term>
80
+ <term>times</term>
81
+ <term>restaurants</term>
82
+ <term>gtim</term>
83
+ <id>1062</id>
84
+ <name>Good Times Restaurants Inc. (GTIM)</name>
85
+ <score>3.43476</score>
86
+ <timestamp>1331833525493</timestamp>
87
+ <title>Consumer Services - Restaurants</title>
88
+ <url>http://www.nasdaq.com/symbol/gtim</url>
89
+ </element>
90
+ </element-list>
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0"?>
2
+ <element>
3
+ <term>Wedge</term>
4
+ <term>Antilles</term>
5
+ <term>Rogue Squadron</term>
6
+ <id>4747</id>
7
+ <name>Wedge Antilles</name>
8
+ <score>159350.0</score>
9
+ <title>Rogue Sqadron Member</title>
10
+ <url>http://starwars.wikia.com/wiki/Wedge_Antilles</url>
11
+ </element>
@@ -0,0 +1,8 @@
1
+ require 'logger'
2
+ ActiveRecord::Base.logger = Logger.new("debug.log")
3
+
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "sqlite3",
6
+ :encoding => "utf8",
7
+ :database => File.join(File.dirname(File.expand_path(__FILE__)), '..', 'test.db')
8
+ )
@@ -0,0 +1,13 @@
1
+ class CreateBooks < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :books, :force => true do |t|
4
+ t.column :category_id, :integer
5
+ t.column :name, :string
6
+ t.column :author, :string
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :books
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class CreateMovies < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :movies, :force => true do |t|
4
+ t.column :name, :string
5
+ t.column :description, :string
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :movies
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateCategories < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :categories, :force => true do |t|
4
+ t.column :name, :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :categories
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ class CreateElectronics < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :electronics, :force => true do |t|
4
+ t.column :name, :string
5
+ t.column :manufacturer, :string
6
+ t.column :features, :string
7
+ t.column :category, :string
8
+ t.column :price, :string
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :electronics
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAuthors < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :authors, :force => true do |t|
4
+ t.column :name, :string
5
+ t.column :biography, :text
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :authors
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class CreatePostings < ActiveRecord::Migration
2
+ def self.up
3
+ execute "CREATE TABLE postings(`guid` varchar(20) NOT NULL PRIMARY KEY, `name` varchar(200), `description` text)"
4
+ end
5
+
6
+ def self.down
7
+ drop_table :postings
8
+ end
9
+ end
data/test/db/test.db ADDED
Binary file
@@ -0,0 +1,9 @@
1
+ tom_clancy:
2
+ id: 1
3
+ name: Tom Clancy
4
+ biography: Tom Clancy (born 1947) writes novels of adventure and espionage in the international military-industrial complex that have earned him enormous popularity in the 1980s as a creator of the "techno-thriller" genre.
5
+
6
+ stephen_king:
7
+ id: 2
8
+ name: Stephen King
9
+ biography: Stephen King (born 1947) is a prolific and immensely popular author of horror fiction. In his works, King blends elements of the traditional gothic tale with those of the modern psychological thriller, detective, and science fiction genres.
@@ -0,0 +1,11 @@
1
+ splinter_cell:
2
+ id: 1
3
+ category_id: 1
4
+ name: Splinter Cell
5
+ author: Tom Clancy
6
+
7
+ ruby:
8
+ id: 2
9
+ category_id: 2
10
+ name: Ruby for Dummies
11
+ author: Peter McPeterson
@@ -0,0 +1,7 @@
1
+ thriller:
2
+ id: 1
3
+ name: Thriller/Novels
4
+
5
+ technical:
6
+ id: 2
7
+ name: Technical Books
@@ -0,0 +1,39 @@
1
+ ipod_video:
2
+ id: 1
3
+ name: Apple 60 GB Memory iPod with Video Playback Black
4
+ manufacturer: Apple Computer Inc.
5
+ features: iTunes, Podcasts, Audiobooks
6
+ category: Electronics
7
+ price: 599.00
8
+
9
+ dell_monitor:
10
+ id: 2
11
+ name: Dell Widescreen UltraSharp 3007WFP
12
+ manufacturer: Dell, Inc
13
+ features: 30" TFT active matrix LCD, 2560 x 1600, .25mm dot pitch, 700:1 contrast
14
+ category: Electronics
15
+ price: 750.00
16
+
17
+ samsung_hd:
18
+ id: 3
19
+ name: Samsung SpinPoint P120 SP2514N - hard drive - 250 GB of Memory Storage - ATA-133
20
+ manufacturer: Samsung Electronics Co. Ltd.
21
+ features: 7200RPM, 8MB cache, IDE Ultra ATA-133
22
+ category: Hard Drive
23
+ price: 319.00
24
+
25
+ corsair_ram:
26
+ id: 4
27
+ name: CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail
28
+ manufacturer: Corsair Microsystems Inc.
29
+ features: CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader
30
+ category: Memory
31
+ price: 155.00
32
+
33
+ a_data_ram:
34
+ id: 5
35
+ name: A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
36
+ manufacturer: A-DATA Technology Inc.
37
+ features: CAS latency 3, 2.7v
38
+ category: Memory
39
+ price: 65.79
@@ -0,0 +1,9 @@
1
+ napoleon_dynamite:
2
+ id: 1
3
+ name: Napoleon Dynamite
4
+ description: Cool movie about a goofy guy
5
+
6
+ office_space:
7
+ id: 2
8
+ name: Office Space
9
+ description: Hypnotized dude loves fishing but not working
data/test/helper.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rails'
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ require 'active_record'
8
+ require 'net/http'
9
+
10
+ #require acts_as_cleo
11
+ require File.dirname(__FILE__) + '/../lib/acts_as_cleo'
12
+
13
+ begin
14
+ Bundler.setup(:default, :development)
15
+ rescue Bundler::BundlerError => e
16
+ $stderr.puts e.message
17
+ $stderr.puts "Run `bundle install` to install missing gems"
18
+ exit e.status_code
19
+ end
20
+ require 'test/unit'
21
+ require 'shoulda'
22
+
23
+ file_dirname = File.dirname(__FILE__)
24
+
25
+ #Load Lib
26
+ $LOAD_PATH.unshift(File.join(file_dirname, '..', 'lib'))
27
+ $LOAD_PATH.unshift file_dirname
28
+
29
+ #Load sqlilte for testing
30
+ sqlite = File.join(file_dirname, 'db', 'connection', 'sqlite')
31
+ require sqlite
32
+
33
+ # Load Models
34
+ models_dir = File.join(file_dirname, 'models')
35
+ Dir[ models_dir + '/*.rb'].each { |m| require m }
36
+
37
+ class Test::Unit::TestCase
38
+ end
@@ -0,0 +1,7 @@
1
+ # Table fields for 'movies'
2
+ # - id
3
+ # - name
4
+ # - biography
5
+
6
+ class Author < ActiveRecord::Base
7
+ end
@@ -0,0 +1,15 @@
1
+ # Table fields for 'books'
2
+ # - id
3
+ # - category_id
4
+ # - name
5
+ # - author
6
+
7
+ class Book < ActiveRecord::Base
8
+ belongs_to :category
9
+
10
+ acts_as_cleo :terms => %w{name author}, :score => "cleo_score", :name => "name"
11
+
12
+ def cleo_score
13
+ Random.rand(10...47)
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # Table fields for 'categories'
2
+ # - id
3
+ # - name
4
+
5
+ class Category < ActiveRecord::Base
6
+ has_many :books
7
+ end
@@ -0,0 +1,11 @@
1
+ # Table fields for 'electronics'
2
+ # - id
3
+ # - name
4
+ # - manufacturer
5
+ # - features
6
+ # - category
7
+ # - price
8
+
9
+ class Electronic < ActiveRecord::Base
10
+
11
+ end
@@ -0,0 +1,8 @@
1
+ # Table fields for 'movies'
2
+ # - id
3
+ # - name
4
+ # - description
5
+
6
+ class Movie < ActiveRecord::Base
7
+ acts_as_cleo
8
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestActsAsCleo < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ # flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ #Literally checking that the acts_as_cleo tag takes args and sets them as expected
4
+ class TestActsAsMethods < Test::Unit::TestCase
5
+
6
+ should "respond to and return as_cleo" do
7
+ book = Book.new(:name => 'Wedge\'s Life', :author => "Luke Skywalker")
8
+ assert_equal %w{name author}, book.cleo_config[:terms]
9
+ assert_equal "name", book.cleo_config[:name]
10
+ assert_equal "Book", book.cleo_config[:type]
11
+ end
12
+
13
+ should "exclude terms by default" do
14
+ movie = Movie.create(:name => "A New Hope", :description => "The Best")
15
+ assert_equal %w{name description}, movie.cleo_config[:terms]
16
+ assert_equal "id", movie.cleo_config[:score]
17
+ assert_equal "name", movie.cleo_config[:name]
18
+ end
19
+ end
@@ -0,0 +1,60 @@
1
+ require 'helper'
2
+
3
+ class TestCleo < Test::Unit::TestCase
4
+ def setup
5
+ file = File.open("#{Dir.pwd}/test/data/xml/cleo/testing_element.xml")
6
+ contents = file.read
7
+ @wedge = Cleo::Result.parse(contents, :singluar => true)
8
+ end
9
+
10
+ should "get HTTP Connection" do
11
+ net = Cleo.net_http
12
+ assert_not_equal nil, net
13
+ end
14
+
15
+ should "create then destroy Testing Object " do
16
+ code = Cleo.create(@wedge)
17
+ assert code
18
+
19
+ wa = Cleo.find(@wedge.id)
20
+ assert_not_nil wa
21
+ assert_equal @wedge.id, wa.id
22
+
23
+ assert Cleo.delete(wa)
24
+ end
25
+
26
+ should "get google results from qery" do
27
+ r = Cleo.query("goog")
28
+ assert_not_nil r
29
+ assert_equal 1, r.count
30
+ assert_kind_of Array, r
31
+ end
32
+
33
+ should "fetch the same record by id" do
34
+ r = Cleo.query("goog").first
35
+ assert_equal r.id.blank?, false
36
+
37
+
38
+ i = Cleo.find(r.id)
39
+ assert_equal r.timestamp, i.timestamp
40
+ assert_equal r.id, i.id
41
+ end
42
+
43
+ should "create, update, then destroy, Testing Object" do
44
+ code = Cleo.create(@wedge)
45
+ assert code
46
+
47
+ wa = Cleo.find(@wedge.id)
48
+ assert_not_nil wa
49
+ assert_equal @wedge.id, wa.id
50
+
51
+ wa.name = "Luke Skywalker"
52
+ assert Cleo.update(wa)
53
+
54
+ wa_update = Cleo.find(@wedge.id)
55
+ assert_equal "Luke Skywalker", wa_update.name
56
+
57
+ assert Cleo.delete(wa)
58
+ end
59
+ end
60
+
@@ -0,0 +1,23 @@
1
+ require "#{File.dirname(File.expand_path(__FILE__))}/../helper"
2
+
3
+ class TestResult < Test::Unit::TestCase
4
+ def test_parser_gets_correct_element
5
+ file = File.open("#{Dir.pwd}/test/data/xml/cleo/element.xml")
6
+ contents = file.read
7
+ cr = Cleo::Result.parse(contents, :singluar => true)
8
+
9
+ assert_not_equal cr, nil
10
+ assert_equal cr.name, "Cleo Inc."
11
+ end
12
+
13
+ def test_parser_gets_many_elements
14
+ file = File.open("#{Dir.pwd}/test/data/xml/cleo/elements.xml")
15
+ contents = file.read
16
+ cr = Cleo::Result.parse(contents)
17
+
18
+ assert_equal 7, cr.count
19
+ assert_match /google/i, cr.first.name
20
+ assert_equal cr.first.terms, %w{google inc. goog}
21
+ end
22
+ end
23
+