class Thron::Route

Attributes

url[R]
verb[R]

Public Class Methods

factory(options = {}) click to toggle source
# File lib/thron/route.rb, line 20
def self.factory(options = {})
  name = options[:name]
  package = options[:package]
  params = options[:params].to_a
  verb = options.fetch(:verb) { Verbs::POST }
  type = options.fetch(:type) { Types::JSON }
  accept = options.fetch(:accept) { Types::JSON }
  url = "/#{package}/#{name}"
  url << "/#{params.join('/')}" unless params.empty?
  Route::new(verb: verb, url: url, type: type, accept: accept)
end
header_type(type) click to toggle source
# File lib/thron/route.rb, line 37
def self.header_type(type)
  case type.to_s
  when Types::JSON
    'application/json'
  when Types::XML
    'application/xml'
  when Types::MULTIPART
    'multipart/form-data'
  when Types::PLAIN
    'text/plain'
  end
end
lazy_factory(options) click to toggle source
# File lib/thron/route.rb, line 32
def self.lazy_factory(options)
  options.delete(:params)
  ->(params) { factory(options.merge({ params: params })) }
end
new(options = {}) click to toggle source
# File lib/thron/route.rb, line 52
def initialize(options = {})
  @verb   = check_verb(options[:verb])
  @url    = options[:url]
  @type   = check_type(options[:type])
  @accept = check_type(options[:accept])
end

Public Instance Methods

call(*args) click to toggle source
# File lib/thron/route.rb, line 59
def call(*args)
  self
end
format() click to toggle source
# File lib/thron/route.rb, line 67
def format
  return {} unless @format
  { format: @format }
end
headers(options = {}) click to toggle source
# File lib/thron/route.rb, line 72
def headers(options = {})
  @headers = { 
    'Accept' => self.class.header_type(@accept), 
    content_type_key(options[:dash]) => self.class.header_type(@type)
  }.tap do |headers|
    headers.merge!({ 'X-TOKENID' => options[:token_id] }) if options[:token_id]
  end
end
json?() click to toggle source
# File lib/thron/route.rb, line 63
def json?
  @type == Types::JSON
end

Private Instance Methods

check_type(type) click to toggle source
# File lib/thron/route.rb, line 95
def check_type(type)
  type.tap do |type|
    fail UnsupportedTypeError, "#{type} is not supported" unless Types::ALL.include?(type)
  end
end
check_verb(verb) click to toggle source
# File lib/thron/route.rb, line 89
def check_verb(verb)
  verb.tap do |verb|
    fail UnsupportedVerbError, "#{verb} is not supported" unless Verbs::ALL.include?(verb)
  end
end
content_type_key(dash = nil) click to toggle source
# File lib/thron/route.rb, line 83
def content_type_key(dash = nil)
  "Content_Type".tap do |key|
    key.sub!('_', '-') if dash
  end
end