Skip to content

Commit

Permalink
Merge pull request #9 from bengler/handle-ack-not-found
Browse files Browse the repository at this point in the history
Return 404 if record not found
  • Loading branch information
bjoerge committed Aug 28, 2013
2 parents 1f0cd91 + 2ddb2e5 commit d7a6d8b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions api/v1.rb
Expand Up @@ -12,6 +12,10 @@ class KuduV1 < Sinatra::Base

register Sinatra::Pebblebed

error ActiveRecord::RecordNotFound do
halt 404, "Record not found"
end

before do
response.headers['Cache-Control'] = 'public, max-age=300'

Expand Down
5 changes: 3 additions & 2 deletions lib/kudu/score.rb
Expand Up @@ -38,9 +38,10 @@ def calculate_all
end
end

def pick(already_picked, resultset, number, random)
def pick(already_picked, resultset, number, random, seed = nil)
picked = []
until resultset.empty? || picked.size == number
srand seed if seed
pos = random ? rand(resultset.length) : 0
score = resultset.delete_at(pos)
picked << score unless already_picked.include?(score.id)
Expand Down Expand Up @@ -69,7 +70,7 @@ def combine_resultsets(params)
sampled = segments.map do |segment|

results = Score.by_field(segment.query_parameters)
sampled = Score.pick(picked, results, segment.share_of_results, segment.randomize)
sampled = Score.pick(picked, results, segment.share_of_results, segment.randomize, params[:random_seed])

picked |= sampled.map(&:id)
remaining.concat results
Expand Down
7 changes: 7 additions & 0 deletions spec/api/v1/acks_spec.rb
Expand Up @@ -59,6 +59,7 @@ def get(url, *args)
}

describe 'GET /acks/:uid' do

it 'returns an ack' do
an_ack
get "/acks/#{an_ack.uid}"
Expand All @@ -68,6 +69,12 @@ def get(url, *args)
ack_response['ack']['kind'].should eq 'kudos'
ack_response['ack']['uid'].should eq an_ack.uid
end

it 'returns 404 if not found' do
get "/acks/post:realm.some.non.existing.post$1"
last_response.status.should eq 404
end

end

# FIXME: Most of these examples are testing functionality of
Expand Down
3 changes: 1 addition & 2 deletions spec/score_collection_spec.rb
Expand Up @@ -135,6 +135,7 @@ def create_scores
params = {
:limit => 4,
"shuffle" => true,
"random_seed" => 4, # pass seed in order to get same test result every time
:include_own => false,
:segments => [
{
Expand All @@ -153,8 +154,6 @@ def create_scores
}

results = []
# Every time you run combine_resultsets it should give you a new combination
# ... thus: doing it five times, should be five different sets. Most of the time.
5.times do
results << Score.combine_resultsets(params).map(&:external_uid)
end
Expand Down

0 comments on commit d7a6d8b

Please sign in to comment.