Sha256: b0eba32a1067177fec21c7ae301da6070fda362af9a3ecd1a3cabe1ea5abc985

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require 'cgi'
require "converse/interaction"

module Converse
  class RESTInteraction < Interaction
    def ask_broker_about(broker, action, substance)
      ask_broker_concerning(broker, "")
      about(action)
      detailed_by(substance)
    end

    def tell_broker_to(broker, action, substance)
      tell_broker_concerning(broker, "")
      about(action)
      detailed_by(substance)
    end

    def ask
      @conversation.ask(path_with_params(@conversation.path, @substance))
    end

    def say
      @conversation.say(@conversation.path, compile_params(@substance))
    end

    def compile_params(params)
      params.map {|k,v| CGI.escape(k.to_s)+'='+CGI.escape(v.to_s) }.join("&")
    end

    def path_with_params(path, params)
      return path if params.nil? or params.empty?
      path + "?" + compile_params(params)
    end

    def response_ok?(response)
      test_value = "#{response.code}"
      return test_value == "200"
    end

    def interpret_conversation(response)
      format_error(response) if not response_ok?(response)
      return format_response(response.body)
    end

    def format_error(response)
      result = []
      result << response.code
      result << response.body
      return result
    end

    def format_response(response_body)
      response_body
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
converse-1.0.22 lib/converse/rest/rest_interaction.rb
converse-1.0.21 lib/converse/rest/rest_interaction.rb