Class: Thron::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/thron/route.rb

Defined Under Namespace

Modules: Types, Verbs Classes: UnsupportedTypeError, UnsupportedVerbError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Route

Returns a new instance of Route.


52
53
54
55
56
57
# 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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.


50
51
52
# File 'lib/thron/route.rb', line 50

def url
  @url
end

#verbObject (readonly)

Returns the value of attribute verb.


50
51
52
# File 'lib/thron/route.rb', line 50

def verb
  @verb
end

Class Method Details

.factory(options = {}) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object


32
33
34
35
# File 'lib/thron/route.rb', line 32

def self.lazy_factory(options)
  options.delete(:params)
  ->(params) { factory(options.merge({ params: params })) }
end

Instance Method Details

#call(*args) ⇒ Object


59
60
61
# File 'lib/thron/route.rb', line 59

def call(*args)
  self
end

#formatObject


67
68
69
70
# File 'lib/thron/route.rb', line 67

def format
  return {} unless @format
  { format: @format }
end

#headers(options = {}) ⇒ Object


72
73
74
75
76
77
78
79
# 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?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/thron/route.rb', line 63

def json?
  @type == Types::JSON
end