Sha256: 95794cebd24426474a6dc32568aeb838b79cdb6a41a097d866f274f2110eea82

Contents?: true

Size: 1.14 KB

Versions: 19

Compression:

Stored size: 1.14 KB

Contents

module MxHero
  module API
    module Communication
      
      # Make a HTTP call 
      # @param method [Symbol] indicate the HTTP verb (:get, :post, :put, etc)
      # @param url [String]
      # @param body [String] (default: nil)
      # @param [Hash] more_options
      # @option more_options [Boolean] :throw_exception (default: true) throw exception if the response status are between 500 and 600
      def call(method, url, body = nil, more_options = {})
        unless @client
          @client ||= HTTPClient.new
        end
        @client.set_auth(url, @username, @password)
        response = @client.request(method, url, nil, body, headers)
        unless more_options[:throw_exception] == false
          raise "An error ocurred when try to communicate with the API\nError: #{response.inspect}" if (500..600).include?(response.status)
        end
        response
      end

      # Default headers
      def headers
        @headers ||= { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
      end
      
      # @return [Hash]
      def json_parse(json)
        JSON.parse(json, symbolize_names: true)
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mxhero-api-0.1.30 lib/communication.rb
mxhero-api-0.1.29 lib/communication.rb
mxhero-api-0.1.28 lib/communication.rb
mxhero-api-0.1.27 lib/communication.rb
mxhero-api-0.1.26 lib/communication.rb
mxhero-api-0.1.25 lib/communication.rb
mxhero-api-0.1.24 lib/communication.rb
mxhero-api-0.1.23 lib/communication.rb
mxhero-api-0.1.22 lib/communication.rb
mxhero-api-0.1.21 lib/communication.rb
mxhero-api-0.1.20 lib/communication.rb
mxhero-api-0.1.19 lib/communication.rb
mxhero-api-0.1.18 lib/communication.rb
mxhero-api-0.1.17 lib/communication.rb
mxhero-api-0.1.16 lib/communication.rb
mxhero-api-0.1.15 lib/communication.rb
mxhero-api-0.1.14 lib/communication.rb
mxhero-api-0.1.13 lib/communication.rb
mxhero-api-0.1.12 lib/communication.rb