Class: Carnivore::Source::Http

Inherits:
Carnivore::Source show all
Includes:
Http::Utils::Params
Defined in:
lib/carnivore-http/http.rb

Direct Known Subclasses

HttpEndpoints

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Http::Utils::Params

#format_query_args, #format_query_type, included, #parse_query_string

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



12
13
14
# File 'lib/carnivore-http/http.rb', line 12

def args
  @args
end

Instance Method Details

#confirm(message, args = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/carnivore-http/http.rb', line 55

def confirm(message, args={})
  if(args[:response_body] || args[:code])
    debug "Confirming #{message} with: #{args.inspect}"
    message[:message][:connection].respond(
      args[:code] || :ok, args[:response_body] || 'Thanks!'
    )
  else
    debug "Confirming #{message}. No arguments provided so no response taken."
  end
end

#default_args(args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/carnivore-http/http.rb', line 18

def default_args(args)
  {
    :bind => '0.0.0.0',
    :port => '3000',
    :auto_respond => true
  }.merge(args)
end

#process(*process_args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/carnivore-http/http.rb', line 66

def process(*process_args)
  srv = Reel::Server.supervise(args[:bind], args[:port]) do |con|
    while(req = con.request)
      begin
        msg = format(
          :request => req,
          :body => req.body.to_s,
          :connection => con,
          :query => parse_query_string(req.query_string)
        )
        callbacks.each do |name|
          c_name = callback_name(name)
          debug "Dispatching #{msg} to callback<#{name} (#{c_name})>"
          callback_supervisor[c_name].async.call(msg)
        end
        con.respond(:ok, 'So long, and thanks for all the fish!') if args[:auto_respond]
      rescue => e
        con.respond(:bad_request, 'Failed to process request')
      end
    end
  end
end

#setup(args = {}) ⇒ Object



14
15
16
# File 'lib/carnivore-http/http.rb', line 14

def setup(args={})
  @args = default_args(args)
end

#transmit(message, *extra) ⇒ Object

Transmit can be one of two things:

  1. Response back to an open connection

  2. Request to a remote source



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/carnivore-http/http.rb', line 29

def transmit(message, *extra)
  options = extra.detect{|x| x.is_a?(Hash)} || {}
  orig = extra.detect{|x| x.is_a?(Carnivore::Message)}
  con = options[:connection]
  if(orig && con.nil?)
    con = orig[:message][:connection]
  end
  if(con) # response
    payload = message.is_a?(String) ? message : MultiJson.dump(message)
    # TODO: add `options` options for marshaling: json/xml/etc
    debug "Transmit response type with payload: #{payload}"
    con.respond(options[:code] || :ok, payload)
  else # request
    url = File.join("http://#{args[:bind]}:#{args[:port]}", options[:path].to_s)
    method = (options[:method] || :post).to_sym
    if(options[:headers])
      base = HTTP.with_headers(options[:headers])
    else
      base = HTTP
    end
    payload = message.is_a?(String) ? message : MultiJson.dump(message)
    debug "Transmit request type with payload: #{payload}"
    base.send(method, url, :body => payload)
  end
end