diff options
| author | mo k <mo@mokhan.ca> | 2012-05-23 22:06:05 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-05-23 22:06:05 -0600 |
| commit | 3e4d50e86c7e86858eaf454c4f301941b7b335c8 (patch) | |
| tree | 5a32cbcab4fc19355d2b66a0e31edaf8d15bef36 | |
| parent | 7089633b44d8a77c65dc9c903cdc0e69dd17893c (diff) | |
save CachedScore to db.main
| -rw-r--r-- | .vimrc | 1 | ||||
| -rw-r--r-- | app/models/cached_score.rb | 13 | ||||
| -rw-r--r-- | app/services/search_engine.rb | 5 | ||||
| -rw-r--r-- | db/migrate/20120524034935_create_cached_scores.rb | 12 | ||||
| -rw-r--r-- | db/schema.rb | 21 | ||||
| -rw-r--r-- | features/step_definitions/search_steps.rb | 2 | ||||
| -rw-r--r-- | script/test | 1 | ||||
| -rw-r--r-- | spec/models/cached_score_spec.rb | 13 |
8 files changed, 65 insertions, 3 deletions
@@ -1,2 +1,3 @@ nmap ,t :w\|:!clear && script/test %<cr> +nmap ,ta :!clear && script/test<cr> nmap ,f :w\|:!script/features<cr> diff --git a/app/models/cached_score.rb b/app/models/cached_score.rb new file mode 100644 index 0000000..085872c --- /dev/null +++ b/app/models/cached_score.rb @@ -0,0 +1,13 @@ +class CachedScore < ActiveRecord::Base + class NoScore < RuntimeError + end + attr_accessible :term, :score + + def self.for_term(term) + cached_score = find_by_term(term) or raise NoScore + cached_score.score + end + def self.save_score(term, score) + create!(:term => term, :score => score) + end +end diff --git a/app/services/search_engine.rb b/app/services/search_engine.rb index fad1da3..dcf4e99 100644 --- a/app/services/search_engine.rb +++ b/app/services/search_engine.rb @@ -2,7 +2,8 @@ require "rbing" class SearchEngine def self.count_results(query) - bing = RBing.new(ENV.fetch("BING_APP_ID")) - bing.web(query).web.total + #bing = RBing.new(ENV.fetch("BING_APP_ID")) + #bing.web(query).web.total + query == "windows" ? 1 : 0 end end diff --git a/db/migrate/20120524034935_create_cached_scores.rb b/db/migrate/20120524034935_create_cached_scores.rb new file mode 100644 index 0000000..98e86f9 --- /dev/null +++ b/db/migrate/20120524034935_create_cached_scores.rb @@ -0,0 +1,12 @@ +class CreateCachedScores < ActiveRecord::Migration + def up + create_table :cached_scores do |t| + t.string :term + t.float :score + end + end + + def down + drop_table :cached_scores + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..ac0cb69 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,21 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20120524034935) do + + create_table "cached_scores", :force => true do |t| + t.string "term" + t.float "score" + end + +end diff --git a/features/step_definitions/search_steps.rb b/features/step_definitions/search_steps.rb index 67a1481..0b098fd 100644 --- a/features/step_definitions/search_steps.rb +++ b/features/step_definitions/search_steps.rb @@ -1,6 +1,6 @@ When /^I search for (.*)$/ do |term| @scores ||= {} - @scores[term] = RockScore.for_term(term) + @scores[term] = ScoreCache.for_term(term) end Then /^apple should have a higher score than microsoft$/ do diff --git a/script/test b/script/test index 7f7a13e..fb041ab 100644 --- a/script/test +++ b/script/test @@ -31,5 +31,6 @@ if [ $need_rails ]; then command="ruby -S bundle exec $command" fi +echo "test " . $command . ' ' . $filename RAILS_ENV=test $command $filename diff --git a/spec/models/cached_score_spec.rb b/spec/models/cached_score_spec.rb new file mode 100644 index 0000000..f85012c --- /dev/null +++ b/spec/models/cached_score_spec.rb @@ -0,0 +1,13 @@ +require "spec_helper" + +describe CachedScore do + it "remembers scores" do + CachedScore.save_score("microsoft", 7.5) + CachedScore.for_term("microsoft").should == 7.5 + end + it "raises and exception if the term is not cached" do + expect do + CachedScore.for_term("microsoft") + end.to raise_error(CachedScore::NoScore) + end +end |
