lk | posts to bike index in sidekiq job

This commit is contained in:
Loos
2015-09-01 18:25:05 -05:00
parent 3224faab0e
commit 356098b7a6
15 changed files with 138 additions and 89 deletions
+2 -2
View File
@@ -14,6 +14,6 @@
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap
//= require bootstrap
//= require bootstrap-datepicker
//= require bootstrap-select
//= require bootstrap-select
+6
View File
@@ -0,0 +1,6 @@
seatTubeIn = $(".seat-tube-in")
seatTubeCm = $(".seat-tube-cm")
seatTubeIn.on("change", () ->
seatTubeCm.val(parseFloat(seatTubeIn.val())*2.54)
)
-2
View File
@@ -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.
+2 -17
View File
@@ -12,7 +12,7 @@ class Bike < ActiveRecord::Base
end
def client
client = Client.find_by bike_id: self.id
Client.find_by bike_id: self.id
end
def ready_for_pickup?
@@ -35,22 +35,7 @@ class Bike < ActiveRecord::Base
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
BikeIndexLogger.perform_async(self.id)
end
end
-13
View File
@@ -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
+32
View File
@@ -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