Class: Carnivore::Source::Http
- Inherits:
-
Carnivore::Source
- Object
- Carnivore::Source
- Carnivore::Source::Http
- Includes:
- Http::Utils::Params
- Defined in:
- lib/carnivore-http/http.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
- #confirm(message, args = {}) ⇒ Object
- #default_args(args) ⇒ Object
- #process(*process_args) ⇒ Object
- #setup(args = {}) ⇒ Object
-
#transmit(message, *extra) ⇒ Object
Transmit can be one of two things: 1.
Methods included from Http::Utils::Params
#format_query_args, #format_query_type, included, #parse_query_string
Instance Attribute Details
#args ⇒ Object (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(, args={}) if(args[:response_body] || args[:code]) debug "Confirming #{message} with: #{args.inspect}" [: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:
-
Response back to an open connection
-
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(, *extra) = extra.detect{|x| x.is_a?(Hash)} || {} orig = extra.detect{|x| x.is_a?(Carnivore::Message)} con = [:connection] if(orig && con.nil?) con = orig[:message][:connection] end if(con) # response payload = .is_a?(String) ? : MultiJson.dump() # TODO: add `options` options for marshaling: json/xml/etc debug "Transmit response type with payload: #{payload}" con.respond([:code] || :ok, payload) else # request url = File.join("http://#{args[:bind]}:#{args[:port]}", [:path].to_s) method = ([:method] || :post).to_sym if([:headers]) base = HTTP.with_headers([:headers]) else base = HTTP end payload = .is_a?(String) ? : MultiJson.dump() debug "Transmit request type with payload: #{payload}" base.send(method, url, :body => payload) end end |