Loos
9 years ago
15 changed files with 138 additions and 89 deletions
@ -0,0 +1,3 @@ |
|||||
|
OWNER_EMAIL: 'coolemail@example.com' |
||||
|
BIKE_INDEX_URL: 'http://lvh.me:3000' |
||||
|
BIKE_INDEX_TOKEN: 'asdfklkjasfdljkd3asdfjkl2asdf' |
@ -0,0 +1,6 @@ |
|||||
|
seatTubeIn = $(".seat-tube-in") |
||||
|
seatTubeCm = $(".seat-tube-cm") |
||||
|
|
||||
|
seatTubeIn.on("change", () -> |
||||
|
seatTubeCm.val(parseFloat(seatTubeIn.val())*2.54) |
||||
|
) |
@ -1,2 +0,0 @@ |
|||||
// Place all the behaviors and hooks related to the matching controller here.
|
|
||||
// All this logic will automatically be available in application.js.
|
|
@ -1,13 +0,0 @@ |
|||||
class BikeIndexBikeGenerator |
|
||||
def self.create_bike_index_bike(bike) |
|
||||
{ |
|
||||
serial: bike.serial_number, |
|
||||
manufacturer: bike.brand, |
|
||||
owner_email: ENV["OWNER_EMAIL"], |
|
||||
color: bike.color, |
|
||||
is_for_sale: bike.purpose != "freecyclery", |
|
||||
frame_model: bike.model, |
|
||||
no_notify: true |
|
||||
}.to_json |
|
||||
end |
|
||||
end |
|
@ -0,0 +1,32 @@ |
|||||
|
class BikeIndexLogger |
||||
|
include Sidekiq::Worker |
||||
|
def perform(bike_id) |
||||
|
bike = Bike.find(bike_id) |
||||
|
|
||||
|
conn = Faraday.new(:url => "#{ENV['BIKE_INDEX_URL']}") do |faraday| |
||||
|
faraday.request :url_encoded |
||||
|
faraday.response :logger |
||||
|
faraday.adapter Faraday.default_adapter |
||||
|
end |
||||
|
|
||||
|
response = conn.post do |req| |
||||
|
req.url "/api/v2/bikes?access_token=#{ENV['BIKE_INDEX_TOKEN']}" |
||||
|
req.headers['Content-Type'] = 'application/json' |
||||
|
req.body = BikeIndexLogger.create_bike_index_bike(bike) |
||||
|
end |
||||
|
|
||||
|
bike.update_attribute :bike_index_id, JSON.parse(response.body)['bike']['id'] if response.status == 201 |
||||
|
end |
||||
|
|
||||
|
def self.create_bike_index_bike(bike) |
||||
|
{ |
||||
|
serial: bike.serial_number, |
||||
|
manufacturer: bike.brand, |
||||
|
owner_email: ENV["OWNER_EMAIL"], |
||||
|
color: bike.color, |
||||
|
is_for_sale: bike.purpose != "freecyclery", |
||||
|
frame_model: bike.model, |
||||
|
no_notify: true |
||||
|
}.to_json |
||||
|
end |
||||
|
end |
@ -1,18 +1,15 @@ |
|||||
require 'spec_helper' |
require 'spec_helper' |
||||
|
|
||||
describe Bike do |
describe Bike do |
||||
it "posts to bike index" do |
describe "#post_to_bike_index" do |
||||
|
it "calls BikeIndexLogger if no bike_index_id is present" do |
||||
|
expect(BikeIndexLogger).to receive(:perform_async) |
||||
|
create(:bike, bike_index_id: nil) |
||||
|
end |
||||
|
|
||||
WebMock.stub_request(:post, "http://lvh.me:3000/api/v2/bikes?access_token=asdfklkjasfdljkd3asdfjkl2asdf") |
it "returns true if bike_index_id is present" do |
||||
.and_return(:status => 201, :body => {bike: {id: 1}}.to_json, :headers => {}) |
bike = create(:bike, bike_index_id: 1) |
||||
bike = create(:bike, bike_index_id: nil) |
expect(bike.post_to_bike_index).to be_truthy |
||||
expect(bike.bike_index_id).to eq(1) |
end |
||||
|
|
||||
end |
|
||||
it "assigns bike_index_id after posting to bike index" do |
|
||||
WebMock.stub_request(:post, "http://lvh.me:3000/api/v2/bikes?access_token=asdfklkjasfdljkd3asdfjkl2asdf") |
|
||||
.and_return(:status => 400, :body => {error: "error"}.to_json, :headers => {}) |
|
||||
bike = create(:bike, bike_index_id: nil) |
|
||||
expect(bike.bike_index_id).to_not be_present |
|
||||
end |
end |
||||
end |
end |
||||
|
@ -1,42 +0,0 @@ |
|||||
require 'spec_helper' |
|
||||
|
|
||||
describe BikeIndexBikeGenerator do |
|
||||
it "creates a bike with required attributes" do |
|
||||
bike = create(:bike) |
|
||||
|
|
||||
bikeIndexBikeJson = BikeIndexBikeGenerator.create_bike_index_bike(bike) |
|
||||
|
|
||||
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
|
||||
|
|
||||
expect(bikeIndexBike["serial"]).to eq(bike.serial_number) |
|
||||
expect(bikeIndexBike["manufacturer"]).to eq(bike.brand) |
|
||||
expect(bikeIndexBike["frame_model"]).to eq(bike.model) |
|
||||
expect(bikeIndexBike["owner_email"]).to eq(ENV["OWNER_EMAIL"]) |
|
||||
expect(bikeIndexBike["no_notify"]).to be_truthy |
|
||||
expect(bikeIndexBike["color"]).to eq(bike.color) |
|
||||
|
|
||||
end |
|
||||
|
|
||||
describe "freecyclery bikes" do |
|
||||
it "is_for_sale is false" do |
|
||||
bike = create(:bike, purpose: "freecyclery") |
|
||||
bikeIndexBikeJson = BikeIndexBikeGenerator.create_bike_index_bike(bike) |
|
||||
|
|
||||
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
|
||||
|
|
||||
expect(bikeIndexBike["is_for_sale"]).to be_falsey |
|
||||
end |
|
||||
end |
|
||||
|
|
||||
describe "sale bikes" do |
|
||||
it "is_for_sale is true" do |
|
||||
bike = create(:bike, purpose: "sale") |
|
||||
bikeIndexBikeJson = BikeIndexBikeGenerator.create_bike_index_bike(bike) |
|
||||
|
|
||||
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
|
||||
|
|
||||
expect(bikeIndexBike["is_for_sale"]).to be_truthy |
|
||||
end |
|
||||
end |
|
||||
|
|
||||
end |
|
@ -0,0 +1,63 @@ |
|||||
|
require 'spec_helper' |
||||
|
|
||||
|
describe BikeIndexLogger do |
||||
|
context "#create_bike_index_bike" do |
||||
|
it "creates a bike with required attributes" do |
||||
|
bike = build(:bike) |
||||
|
allow(Bike).to receive(:find).and_return(bike) |
||||
|
|
||||
|
bikeIndexBikeJson = BikeIndexLogger.create_bike_index_bike(bike) |
||||
|
|
||||
|
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
||||
|
|
||||
|
expect(bikeIndexBike["serial"]).to eq(bike.serial_number) |
||||
|
expect(bikeIndexBike["manufacturer"]).to eq(bike.brand) |
||||
|
expect(bikeIndexBike["frame_model"]).to eq(bike.model) |
||||
|
expect(bikeIndexBike["owner_email"]).to eq(ENV["OWNER_EMAIL"]) |
||||
|
expect(bikeIndexBike["no_notify"]).to be_truthy |
||||
|
expect(bikeIndexBike["color"]).to eq(bike.color) |
||||
|
|
||||
|
end |
||||
|
|
||||
|
describe "freecyclery bikes" do |
||||
|
it "is_for_sale is false" do |
||||
|
bike = build(:bike, purpose: "freecyclery") |
||||
|
allow(Bike).to receive(:find).and_return(bike) |
||||
|
bikeIndexBikeJson = BikeIndexLogger.create_bike_index_bike(bike) |
||||
|
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
||||
|
expect(bikeIndexBike["is_for_sale"]).to be_falsey |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
describe "sale bikes" do |
||||
|
it "is_for_sale is true" do |
||||
|
bike = build(:bike, purpose: "sale") |
||||
|
allow(Bike).to receive(:find).and_return(bike) |
||||
|
bikeIndexBikeJson = BikeIndexLogger.create_bike_index_bike(bike) |
||||
|
bikeIndexBike = JSON.parse(bikeIndexBikeJson) |
||||
|
expect(bikeIndexBike["is_for_sale"]).to be_truthy |
||||
|
end |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
it "assigns bike_index_id on successful response from bike index" do |
||||
|
WebMock.stub_request(:post, "http://lvh.me:3000/api/v2/bikes?access_token=asdfklkjasfdljkd3asdfjkl2asdf") |
||||
|
.and_return(:status => 201, :body => {bike: {id: 1}}.to_json, :headers => {}) |
||||
|
allow_any_instance_of(Bike).to receive(:post_to_bike_index) |
||||
|
bike = create(:bike, bike_index_id: nil) |
||||
|
allow(Bike).to receive(:find).and_return(bike) |
||||
|
BikeIndexLogger.new.perform(bike.id) |
||||
|
expect(bike.bike_index_id).to eq(1) |
||||
|
end |
||||
|
|
||||
|
it "does not assign bike_index_id on unsuccessful response from bike index" do |
||||
|
WebMock.stub_request(:post, "http://lvh.me:3000/api/v2/bikes?access_token=asdfklkjasfdljkd3asdfjkl2asdf") |
||||
|
.and_return(:status => 400, :body => {error: "error"}.to_json, :headers => {}) |
||||
|
allow_any_instance_of(Bike).to receive(:post_to_bike_index) |
||||
|
bike = create(:bike, bike_index_id: nil) |
||||
|
allow(Bike).to receive(:find).and_return(bike) |
||||
|
BikeIndexLogger.new.perform(bike.id) |
||||
|
expect(bike.bike_index_id).to_not be_present |
||||
|
end |
||||
|
|
||||
|
end |
Loading…
Reference in new issue