Class: Carnivore::Source::HttpEndpoints

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

Instance Attribute Summary collapse

Attributes inherited from Http

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Http

#confirm, #default_args, #transmit

Methods included from Http::Utils::Params

#format_query_args, #format_query_type, included, #parse_query_string

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



51
52
53
# File 'lib/carnivore-http/http_endpoints.rb', line 51

def points
  @points
end

Class Method Details

.buildersObject



24
25
26
# File 'lib/carnivore-http/http_endpoints.rb', line 24

def builders
  @point_builders ||= {}
end

.load_builder(name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/carnivore-http/http_endpoints.rb', line 28

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



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/carnivore-http/http_endpoints.rb', line 10

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!Object



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

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



59
60
61
# File 'lib/carnivore-http/http_endpoints.rb', line 59

def connect
  async.process
end

#process(*process_args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/carnivore-http/http_endpoints.rb', line 63

def process(*process_args)
  srv = Reel::Server.supervise(args[:bind], args[:port]) do |con|
    con.each_request do |req|
      begin
        msg = format(
          :request => req,
          :body => req.body.to_s,
          :connection => con,
          :query => parse_query_string(req.query_string).merge(parse_query_string(req.body.to_s))
        )
        unless(@points.deliver(msg))
          con.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")}"
        con.respond(:bad_request, 'Failed to process request')
      end
    end
  end
end

#set_pointsObject



85
86
87
88
89
90
91
# File 'lib/carnivore-http/http_endpoints.rb', line 85

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



53
54
55
56
57
# File 'lib/carnivore-http/http_endpoints.rb', line 53

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