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

This commit is contained in:
Loos
2015-08-06 20:29:46 -05:00
parent 66c43e57c2
commit 3224faab0e
12 changed files with 137 additions and 2 deletions
+21
View File
@@ -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