Sha256: 5aa35a508d8b7985c3538d57f87674af5e0bc030b534287d4f5da38c1fd9cee6

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

require 'uri'

module Locomotive
  module Builder
    module Httparty
      class Webservice

        include ::HTTParty

        def self.consume(url, options = {})
          url = ::HTTParty.normalize_base_uri(url)

          uri = URI.parse(url)
          options[:base_uri] = "#{uri.scheme}://#{uri.host}"
          options[:base_uri] += ":#{uri.port}" if uri.port != 80
          path = uri.request_uri

          options.delete(:format) if options[:format] == 'default'

          username, password = options.delete(:username), options.delete(:password)
          options[:basic_auth] = { username: username, password: password } if username

          path ||= '/'

          # Locomotive::Builder::Logger.debug "[WebService] consuming #{path}, #{options.inspect}"

          response = self.get(path, options)

          if response.code == 200
            _response = response.parsed_response
            if _response.respond_to?(:underscore_keys)
              _response.underscore_keys
            else
              _response.collect(&:underscore_keys)
            end
          else
            Locomotive::Builder::Logger.error "[WebService] consumed #{path}, #{options.inspect}, response = #{response.inspect}"
            nil
          end

        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
locomotivecms_builder-1.0.0.alpha8 lib/locomotive/builder/misc/httparty.rb
locomotivecms_builder-1.0.0.alpha7 lib/locomotive/builder/misc/httparty.rb
locomotivecms_builder-1.0.0.alpha6 lib/locomotive/builder/misc/httparty.rb
locomotivecms_builder-1.0.0.alpha5 lib/locomotive/builder/misc/httparty.rb
locomotivecms_builder-1.0.0.alpha4 lib/locomotive/builder/misc/httparty.rb
locomotivecms_builder-1.0.0.alpha3 lib/locomotive/builder/misc/httparty.rb