Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Forked from ismasan/bootic_oat_adapter.rb
Created August 30, 2016 16:00
Show Gist options
  • Save cheeyeo/a1a47a68f0b3c9dd217ee9ca7d099476 to your computer and use it in GitHub Desktop.
Save cheeyeo/a1a47a68f0b3c9dd217ee9ca7d099476 to your computer and use it in GitHub Desktop.
require 'oat/adapters/hal'
class BooticAdapter < Oat::Adapters::HAL
def type(*types)
property :_class, *types
end
def curie(link_opts)
data[:_links][:curies] ||= []
data[:_links][:curies] << link_opts
end
def items(collection, serializer_class = nil, &block)
entities :items, collection, serializer_class, &block
end
def action(name, &block)
data[:_actions][name] = yield_props(&block)
end
def search_action(schema, prompt, url)
action :search do |props|
props.prompt prompt
props.method 'GET'
props.type 'application/json'
props.href url
props.fields(item.schema.map do |k,q|
{
name: k,
prompt: q[:label],
value: q[:value],
options: q[:options]
}
end)
end
end
end
class BooticSerializer < Oat::Serializer
adapter BooticAdapter
protected
def product_image_size_url(shop_id, image, size)
"#{Bootic::APP_CONFIG['cdn_host']}/#{shop_id}/#{size}/#{image.id}-#{image.file_name}"
end
def controller
context[:controller]
end
def auth
controller.oauth
end
def curies!
curie name: 'btc', href: 'https://developers.bootic.net/rels/{rel}', templated: true
end
def templated(url, search, opts = {}, segments = {})
param_keys = search ? search.params.keys : []
query_keys = param_keys
href = Bootic::APP_CONFIG['default_host'] + Zooplankton.path_template_for(url, query_keys, segments)
opts.merge(templated: true, href: href)
end
def date(d)
return d if d.blank?
d = (d.is_a?(String) ? Time.parse(d) : d)
d.iso8601
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment