Class: Carnivore::Source::HttpEndpoints

Inherits:
Http show all
Defined in:
lib/carnivore-http/http_endpoints.rb

Overview

Carnivore HTTP end points source

Constant Summary

Constants inherited from Http

Carnivore::Source::Http::BODY_TO_FILE_SIZE

Instance Attribute Summary collapse

Attributes inherited from Http

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Http

#build_message, #confirm, #default_args, #transmit

Methods included from Http::Utils::Params

#format_query_args, #format_query_type, included, #parse_query_string

Instance Attribute Details

#pointsHash (readonly)

Returns point builders.

Returns:

  • (Hash)

    point builders



67
68
69
# File 'lib/carnivore-http/http_endpoints.rb', line 67

def points
  @points
end

Class Method Details

.buildersHash

Returns point builders registered.

Returns:

  • (Hash)

    point builders registered



32
33
34
# File 'lib/carnivore-http/http_endpoints.rb', line 32

def builders
  @point_builders ||= {}
end

.load_builder(name) ⇒ self

Load the named builder

Parameters:

  • name (String)

    name of builder

Returns:

  • (self)


40
41
42
43
44
45
46
47
# File 'lib/carnivore-http/http_endpoints.rb', line 40

def load_builder(name)
  if(builders[name.to_sym])
    require File.join(builders[name.to_sym], name)
  else
    raise NameError.new("Requested end point builder not found (#{name})")
  end
  self
end

.register(args = {}) ⇒ Object

Register endpoint

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :name (String)
  • :base_path (String)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carnivore-http/http_endpoints.rb', line 17

def register(args={})
  args = Hash[*(
      args.map do |k,v|
        [k.to_sym, v]
      end.flatten
  )]
  builder = {:name => args[:name], :base_path => args[:base_path]}
  if(res = builder.find_all{|x,y| y.nil})
    raise ArgumentError.new("Missing required argument! (#{res.map(&:first).join(',')})")
  end
  builders[builder[:name].to_sym] = builder[:base_path]
  self
end

.setup!TrueClass

Setup the builders

Returns:

  • (TrueClass)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/carnivore-http/http_endpoints.rb', line 52

def setup!
  only = Carnivore::Config.get(:http_endpoints, :only)
  except = Carnivore::Config.get(:http_endpoints, :except)
  # NOTE: Except has higher precedence than only
  builders.keys.each do |name|
    next if only && !only.include?(name.to_s)
    next if except && except.include?(name.to_s)
    load_builder(name)
  end
  true
end

Instance Method Details

#connectObject

Start processing



80
81
82
# File 'lib/carnivore-http/http_endpoints.rb', line 80

def connect
  async.process
end

#process(*process_args) ⇒ Object

Process requests



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/carnivore-http/http_endpoints.rb', line 85

def process(*process_args)
  srv = Reel::Server::HTTP.supervise(args[:bind], args[:port]) do |con|
    con.each_request do |req|
      begin
        msg = build_message(con, req)
        unless(@points.deliver(msg))
          req.respond(:ok, 'So long, and thanks for all the fish!')
        end
      rescue => e
        error "Failed to process message: #{e.class} - #{e}"
        debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
        req.respond(:bad_request, 'Failed to process request')
      end
    end
  end
end

#set_pointsself

Build the endpoints and set

Returns:

  • (self)


105
106
107
108
109
110
111
# File 'lib/carnivore-http/http_endpoints.rb', line 105

def set_points
  @points = PointBuilder.new(
    :only => Carnivore::Config.get(@conf_key, :only),
    :except => Carnivore::Config.get(@conf_key, :except)
  )
  self
end

#setup(args = {}) ⇒ Object

Setup the registered endpoints

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :config_key (String, Symbol)


73
74
75
76
77
# File 'lib/carnivore-http/http_endpoints.rb', line 73

def setup(args={})
  super
  @conf_key = (args[:config_key] || :http_endpoints).to_sym
  set_points
end