Skip to content

Instantly share code, notes, and snippets.

@data-doge
Last active August 29, 2015 14:10
Show Gist options
  • Save data-doge/215075bebc4426406614 to your computer and use it in GitHub Desktop.
Save data-doge/215075bebc4426406614 to your computer and use it in GitHub Desktop.
post '/accounts/orders/bids/create' --- controller + testing
###RSPEC TEST###
describe "POST /account/orders/bids/create" do
let(:account) { Account.create(dollars: 100, josh_coins: 100, user_id: "1234", email: "[email protected]") }
describe "returns correct bid information" do
before do
@account = account
@api_key = @account.api_key
@data = { api_key: @api_key, volume: rand(1..100), price: rand(1..20) }
post "/account/orders/bids/create", @data
@json_response = parse_json(last_response.body)
end
it "returns correct account_id" do
bid = Bid.find(@json_response["id"])
expect(bid.account_id).to eq(@account.id)
end
it "returns correct volume" do
expect(@json_response["volume"]).to eq(@data[:volume])
end
it "returns correct price" do
expect(@json_response["price"]).to eq(@data[:price])
end
end
end
###MODEL###
class Bid < ActiveRecord::Base
belongs_to :account
has_many :trades
validates :raw_price, :volume, :account, presence: true
DECIMAL_FACTOR = 100
def price
self.raw_price / DECIMAL_FACTOR.to_f
end
def price=(price)
(self.raw_price = price * DECIMAL_FACTOR).to_i
end
def time_placed
self.created_at
end
def as_json( options={} )
{ "id" => id, "price" => price, "volume" => volume, "time_placed" => time_placed }
end
end
###CONTROLLER###
post '/account/orders/bids/create' do
api_key = params[:api_key]
volume = params[:volume].to_i
price = params[:price].to_i
account = Account.find_by(api_key: api_key)
bid = account.bids.create(price: price, volume: volume)
json bid
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment