Sha256: 6a476901c120676055224eb20cc22efcd9aa6e2aa60dd013fdde6d3cc4bdcb71

Contents?: true

Size: 919 Bytes

Versions: 3

Compression:

Stored size: 919 Bytes

Contents

require 'rack'

module Murlsh

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

    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)

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

      Rack::Response.new body, 200,
        'Cache-Control' => 'must-revalidate, max-age=0',
        'Content-Type' => content_type
    end

    attr_reader :config
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
murlsh-1.6.1 lib/murlsh/json_server.rb
murlsh-1.6.0 lib/murlsh/json_server.rb
murlsh-1.5.0 lib/murlsh/json_server.rb