Skip to content
Browse files

Store and return a public ID number for each item

Each item should have its own unique item number. Rather than use the
database ID, hit up Brooklyn Integers for a hand-crafted, guaranteed
artisinal int to use. This is only returned with JSON output.

A class for Mission Integers is also included, but I didn't use
it because the response times were insanely slow. Oh well!
  • Loading branch information...
1 parent 6c9f673 commit 4bd84076f8c93ed30e9834a26ce0ec2b514a0d49 @mistydemeo committed Aug 11, 2012
View
4 app.rb
@@ -1,4 +1,5 @@
$: << "."
+$: << "lib"
require 'sinatra'
require 'sinatra/activerecord'
@@ -7,6 +8,7 @@
require 'models/items'
require 'helpers'
+require 'preeventualist/brooklynt'
set :database, "sqlite://db/preeventualist.db"
@@ -64,6 +66,7 @@
i.location = params["seen"]
i.description = params["desc"]
i.kind = "lost"
+ i.public_id = Brooklynt.new.to_i
end
item.save
@@ -83,6 +86,7 @@
i.location = params["at"]
i.description = params["desc"]
i.kind = "found"
+ i.public_id = Brooklynt.new.to_i
end
item.save
View
1 db/migrate/20120809042234_create_items.rb
@@ -6,6 +6,7 @@ def up
t.string :location
t.string :description
t.string :kind
+ t.string :public_id
end
end
View
2 features/step_definitions/lostfound_steps.rb
@@ -7,6 +7,8 @@
i.location = location
i.description = description
i.kind = kind
+ # Don't hit the Brooklynt API just for this
+ i.public_id = rand(10000)
end
item.save
View
5 helpers.rb
@@ -50,8 +50,11 @@ def search_response_for results
return "No results found :(" if results.empty?
# _why's version didn't have a public "kind" value
+ # or any kind of ID number
result = results.first.to_hash
- result.delete :kind
+ result.delete_if do |k,_|
+ [:kind, :public_id].include? k
+ end
# _why's guide specifies only one result, with unnamed fields
# separated by "--\n"
View
16 lib/preeventualist/brooklynt.rb
@@ -0,0 +1,16 @@
+require 'json'
+require 'net/http'
+require 'uri'
+
+class Brooklynt
+ def initialize
+ uri = URI("http://api.brooklynintegers.com/rest/")
+ opts = {"method" => "brooklyn.integers.create"}
+ response = Net::HTTP.post_form uri, opts
+ @integer = JSON.parse(response.body)["integers"].first["integer"].to_i
+ end
+
+ def to_i
+ @integer
+ end
+end
View
17 lib/preeventualist/missionint.rb
@@ -0,0 +1,17 @@
+require 'json'
+require 'net/http'
+require 'uri'
+
+# This is unused because response times are unbearably slow.
+# But might as well leave the code here, eh?
+class MissionInteger
+ def initialize
+ uri = URI("http://www.missionintegers.com/next-int")
+ response = Net::HTTP.post_form uri, "format" => "json"
+ @integer = JSON.parse(response.body).first.to_i
+ end
+
+ def to_i
+ @integer
+ end
+end
View
4 models/items.rb
@@ -4,14 +4,16 @@ class Items < ActiveRecord::Base
validates_presence_of :location
validates_presence_of :description
validates_presence_of :kind
+ validates_presence_of :public_id
def to_hash
{
:name => name,
:item => item,
:location => location,
:description => description,
- :kind => kind
+ :kind => kind,
+ :public_id => public_id
}
end
end

0 comments on commit 4bd8407

Please sign in to comment.
Something went wrong with that request. Please try again.