Browse Source

lk & sh | posts to bike index in development when creating new bike

master
Loos 9 years ago
parent
commit
3224faab0e
  1. 3
      .env.test
  2. 5
      Gemfile
  3. 29
      Gemfile.lock
  4. 21
      app/models/bike.rb
  5. 13
      app/services/bike_index_bike_generator.rb
  6. 5
      db/migrate/20150807011518_add_bike_index_id_to_bikes.rb
  7. 3
      db/schema.rb
  8. 2
      notes.txt
  9. 1
      spec/factories/bikes.rb
  10. 14
      spec/models/bike_spec.rb
  11. 42
      spec/services/bike_index_bike_generator_spec.rb
  12. 1
      spec/spec_helper.rb

3
.env.test

@ -0,0 +1,3 @@
OWNER_EMAIL: 'coolemail@example.com'
BIKE_INDEX_URL: 'http://lvh.me:3000'
BIKE_INDEX_TOKEN: 'asdfklkjasfdljkd3asdfjkl2asdf'

5
Gemfile

@ -4,12 +4,14 @@ gem 'rails', '4.0.1'
gem 'pg', '0.15.1'
gem 'bcrypt-ruby', '3.1.2'
gem 'devise'
gem 'faraday', '0.9.1'
gem 'oauth2', '1.0.0'
gem 'dotenv-rails'
#SCSS & Bootstrap
gem 'bootstrap-sass', '2.3.2.0'
gem 'sass-rails', '~> 4.0.0'
gem 'haml'
gem 'faker'
group :test, :development do
@ -25,6 +27,7 @@ group :test, :development do
gem 'better_errors'
gem 'assert_difference'
gem 'binding_of_caller'
gem 'webmock', '1.21.0'
# Uncomment this line on OS X.
gem 'growl', '1.0.3'

29
Gemfile.lock

@ -25,6 +25,7 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.8)
arel (4.0.2)
assert_difference (1.0.0)
activesupport (>= 3.0.0)
@ -55,6 +56,8 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.9.1.1)
crack (0.4.2)
safe_yaml (~> 1.0.0)
debug_inspector (0.0.2)
devise (3.4.1)
bcrypt (~> 3.0)
@ -64,6 +67,10 @@ GEM
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.2.5)
dotenv (2.0.2)
dotenv-rails (2.0.2)
dotenv (= 2.0.2)
railties (~> 4.0)
erubis (2.7.0)
execjs (2.5.2)
factory_girl (4.5.0)
@ -73,6 +80,8 @@ GEM
railties (>= 3.0.0)
faker (1.4.3)
i18n (~> 0.5)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.8)
growl (1.0.3)
haml (4.0.6)
@ -88,6 +97,7 @@ GEM
railties (>= 3.1.0)
turbolinks
json (1.8.2)
jwt (1.5.1)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
@ -96,8 +106,16 @@ GEM
mini_portile (0.6.2)
minitest (4.7.5)
multi_json (1.11.0)
multi_xml (0.5.5)
multipart-post (2.0.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
orm_adapter (0.5.0)
pg (0.15.1)
polyglot (0.3.5)
@ -155,6 +173,7 @@ GEM
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
rubyzip (0.9.9)
safe_yaml (1.0.4)
sass (3.2.19)
sass-rails (4.0.5)
railties (>= 4.0.0, < 5.0)
@ -193,6 +212,9 @@ GEM
multi_json (~> 1.0, >= 1.0.2)
warden (1.2.3)
rack (>= 1.0)
webmock (1.21.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
websocket (1.0.7)
xpath (2.0.0)
nokogiri (~> 1.3)
@ -208,14 +230,17 @@ DEPENDENCIES
bootstrap-sass (= 2.3.2.0)
capybara (= 2.2.0)
devise
dotenv-rails
factory_girl
factory_girl_rails (~> 4.0)
faker
faraday (= 0.9.1)
growl (= 1.0.3)
haml
jbuilder (= 1.0.2)
jquery-rails (= 3.0.4)
jquery-turbolinks
oauth2 (= 1.0.0)
pg (= 0.15.1)
pry
pry-rails
@ -229,3 +254,7 @@ DEPENDENCIES
selenium-webdriver (= 2.35.1)
turbolinks (= 1.1.1)
uglifier (= 2.1.1)
webmock (= 1.21.0)
BUNDLED WITH
1.10.6

21
app/models/bike.rb

@ -5,6 +5,7 @@ class Bike < ActiveRecord::Base
validates :bike_type, presence: true
validates :color, presence: true
validates :serial_number, presence: true
after_save :post_to_bike_index
def name
self.brand + ' ' + self.model
@ -32,4 +33,24 @@ class Bike < ActiveRecord::Base
self.update_attribute(:date_sold, current_date)
end
def post_to_bike_index
return true if self.bike_index_id.present?
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 = BikeIndexBikeGenerator.create_bike_index_bike(self)
end
self.update_attribute :bike_index_id, JSON.parse(response.body)['bike']['id'] if response.status == 201
end
end

13
app/services/bike_index_bike_generator.rb

@ -0,0 +1,13 @@
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

5
db/migrate/20150807011518_add_bike_index_id_to_bikes.rb

@ -0,0 +1,5 @@
class AddBikeIndexIdToBikes < ActiveRecord::Migration
def change
add_column :bikes, :bike_index_id, :integer
end
end

3
db/schema.rb

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140923000417) do
ActiveRecord::Schema.define(version: 20150807011518) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -48,6 +48,7 @@ ActiveRecord::Schema.define(version: 20140923000417) do
t.text "new_parts"
t.text "work_done"
t.date "date_sold"
t.integer "bike_index_id"
end
create_table "clients", force: true do |t|

2
notes.txt

@ -1,3 +1,5 @@
Integrate with autocomplete to get manufacturers & models
Freecyclery Receipts
Reports

1
spec/factories/bikes.rb

@ -6,5 +6,6 @@ FactoryGirl.define do
bike_type "Fixed Gear"
color "Black"
sequence(:serial_number)
sequence(:bike_index_id)
end
end

14
spec/models/bike_spec.rb

@ -1,4 +1,18 @@
require 'spec_helper'
describe Bike do
it "posts to 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 => {})
bike = create(:bike, bike_index_id: nil)
expect(bike.bike_index_id).to eq(1)
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

42
spec/services/bike_index_bike_generator_spec.rb

@ -0,0 +1,42 @@
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

1
spec/spec_helper.rb

@ -14,4 +14,5 @@ RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include FactoryGirl::Syntax::Methods
config.order = "random"
WebMock.disable_net_connect!(allow_localhost: true)
end

Loading…
Cancel
Save