Sha256: fb39a3c72a232616ac501913073d72f83f60cfbaf8b368b75b77a70dcc3064f0

Contents?: true

Size: 973 Bytes

Versions: 4

Compression:

Stored size: 973 Bytes

Contents

require 'rack'

module Murlsh

  # Serve most recent urls in json and jsonp.
  class JsonServer

    include Murlsh::HeadFromGet

    def initialize(config); @config = config; end

    # Respond to a GET request. Return json of recent urls or jsonp if
    # if callback parameter is sent.
    def get(req)
      conditions = Murlsh::SearchConditions.new(req['q']).conditions
      page = 1
      per_page = @config.fetch('num_posts_feed', 25)

      result_set = Murlsh::UrlResultSet.new(conditions, page, per_page)

      resp = Rack::Response.new

      if req['callback']
        resp['Content-Type'] = 'application/javascript'
        resp.body = Murlsh::JsonpBody.new(@config, req, result_set)
      else
        resp['Content-Type'] = 'application/json'
        resp.body = Murlsh::JsonBody.new(@config, req, result_set)
      end

      resp['Cache-Control'] = 'must-revalidate, max-age=0'
      resp['ETag'] = "\"#{resp.body.md5}\""

      resp
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
murlsh-1.4.1 lib/murlsh/json_server.rb
murlsh-1.4.0 lib/murlsh/json_server.rb
murlsh-1.3.1 lib/murlsh/json_server.rb
murlsh-1.3.0 lib/murlsh/json_server.rb