mirror of
https://github.com/fspc/bike-database.git
synced 2026-09-16 08:31:39 -04:00
lk & sh | posts to bike index in development when creating new bike
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user