Skip to content

Instantly share code, notes, and snippets.

@sethherr
Last active July 27, 2017 21:22
Embed
What would you like to do?
Bikebook schemaing

BikeBook Schema

A little bit of schema information for the Bike Book models

Certain things need to match values on Bike Index - e.g. component types (ctype) and wheel BSD. Find those options on the Bike Index API documentation - under the selections section.

This is not useable as is, the existing code is terrible. But this at least provides some idea of what is going on.

class FrameModel
def components_serialized
components.map do |c|
cgroup = "Additional parts"
cgroup = c.ctype.cgroup if c.ctype.present?
comp = {
component_type: c.name,
description: c.description,
front_or_rear: c.front_or_rear,
year: c.year,
cgroup: cgroup }
comp[:manufacturer] = c.manufacturer.name if c.manufacturer.present?
comp.keys.each { |k| comp.delete(k) unless comp[k].present? }
end
end
def strip_things
self.name = name.gsub(/\A0\d\s/,' ').gsub(/®|\(r\)|®|©|©/i, ' ').gsub(/\s+/, ' ').strip
if msrp.present?
new_msrp = msrp.gsub(/\s+|\u00a0/,' ').gsub(/msrp:?/i,' ').gsub('$','').gsub(/\s+/, ' ').strip
self.msrp = number_to_currency(new_msrp)
end
if self.frame_description.present?
self.frame_description = self.frame_description.gsub(/\s+|\u00a0/,' ').gsub(/\A[,|.]\s/,'').strip
end
self.slug = Slugifyer.book_slug(self.name)
end
def serialized
bike = {
frame_model: name,
manufacturer: manufacturer.name,
year: year,
description: frame_description,
original_msrp: msrp,
paint_description: original_paints,
manufacturers_url: find_manufacturer_url
}
if original_paints.kind_of?(Array)
# They all should be arrays - but they're not (some are just single strings)
# Someday we'll fix this...
bike[:paint_description] = original_paints
else
bike[:paint_description] = [original_paints]
end
# sizes: sizes
# bike[:msrp] = msrp
bike[:cycle_type] = cycle_type.name if cycle_type.present?
bike[:rear_wheel_bsd] = wheel_size.iso_bsd if wheel_size.present?
bike[:rear_tire_narrow] = tire_narrow_response if tire_narrow.present?
bike[:stock_photo_url] = image.url if image.present?
bike[:stock_photo_small] = image_url(:medium) if image.present?
bike.keys.each { |k| bike.delete(k) unless bike[k].present? }
return {
bike: bike,
components: components_serialized
}
end
end
# Same as Bike Index's - https://github.com/bikeindex/bike_index/blob/master/config/initializers/slugifyer.rb
class Slugifyer
def self.slugify(string)
string.to_s.strip.gsub(/\s/, '-').gsub(/([^A-Za-z0-9_\-]+)/, '').downcase
end
def self.book_slug(string)
slug = I18n.transliterate(string.to_s.downcase)
key_hash = {
'\s((bi)?cycles?|bikes?)' => ' ',
'\+' => 'plus',
'([^A-Za-z0-9])' => ' '
}
key_hash.keys.each do |k|
slug.gsub!(/#{k}/i, key_hash[k])
end
slug.strip.gsub(/\s+/, '_') # strip and then turn any length of spaces into underscores
end
def self.manufacturer(string)
return nil unless string
book_slug(string.gsub(/\sco(\.|mpany)/i, ' ')
.gsub(/\s(frame)?works/i, ' ').gsub(/\([^\)]*\)/i, ''))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment