Module: Sinatra::RespondWith::Helpers

Includes:
JSON
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/respond_with.rb

Instance Method Summary collapse

Methods included from JSON

encode, #json

Instance Method Details

#respond_to(&block) ⇒ Object



170
171
172
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/respond_with.rb', line 170

def respond_to(&block)
  Format.new(self).finish(&block)
end

#respond_with(template, object = nil, &block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/respond_with.rb', line 136

def respond_with(template, object = nil, &block)
  unless Symbol === template
    object = template
    template = nil
  end
  format = Format.new(self)
  format.on '*/*' do |type|
    exts = settings.ext_map[type]
    exts << :xml if type.end_with? '+xml'
    if template
      args = template_cache.fetch(type, template) { template_for(template, exts) }
      if args.any?
        locals = { object: object }
        locals.merge! object.to_hash if object.respond_to? :to_hash

        renderer = args.first
        options = args[1..] + [{ locals: locals }]

        halt send(renderer, *options)
      end
    end
    if object
      exts.each do |ext|
        halt json(object) if ext == :json
        next unless object.respond_to? method = "to_#{ext}"

        halt(*object.send(method))
      end
    end
    false
  end
  format.finish(&block)
end