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 HttpSource

Carnivore::Source::HttpSource::BODY_TO_FILE_SIZE

Instance Attribute Summary collapse

Attributes inherited from HttpSource

#args, #auth_allowed_origins, #auth_htpasswd

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HttpSource

#allowed_credentials?, #allowed_htpasswd?, #allowed_origin?, #authorized?, #build_listener, #build_message, #confirm, #default_args, #perform_transmission, #retry_delivery, #retry_directory, #retry_write_directory, #terminate, #transmit, #write_for_retry

Methods included from Http::Utils::Params

#dump_query_string, #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

#auto_process?Boolean

Always auto start

Returns:

  • (Boolean)


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

def auto_process?
  true
end

#process(*process_args) ⇒ Object

Process requests



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/carnivore-http/http_endpoints.rb', line 85

def process(*process_args)
  unless(processing)
    @processing = true
    srv = build_listener do |req|
      begin
        msg = build_message(req)
        msg = format(msg)
        if(authorized?(msg))
          unless(@points.deliver(msg))
            warn "No match found for request: #{msg} (path: #{msg[:message][:request].url})"
            debug "Unmatched message (#{msg}): #{msg.inspect}"
            req.respond(:not_found, 'So long, and thanks for all the fish!')
          end
        else
          req.respond(:unauthorized, 'You are not authorized to perform requested action!')
        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
    true
  else
    false
  end
end

#set_pointsself

Build the endpoints and set

Returns:

  • (self)


116
117
118
119
120
121
122
# File 'lib/carnivore-http/http_endpoints.rb', line 116

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