Sha256: 908eda8d89944592f20b26b4b110b5a23e83941d927dd235de2ec5d91e8cd64c

Contents?: true

Size: 1009 Bytes

Versions: 9

Compression:

Stored size: 1009 Bytes

Contents

require 'uri'

module Locomotive
  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 ||= '/'

        # puts "[WebService] consuming #{path}, #{options.inspect}"

        response = self.get(path, options)

        if response.code == 200
          if response.respond_to?(:underscore_keys)
            response.underscore_keys
          else
            response.collect(&:underscore_keys)
          end
        else
          nil
        end

      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotive_cms-2.2.3 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.2.2 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.2.1 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.2.0 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.1.4 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.1.3 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.1.2 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.1.1 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.1.0 lib/locomotive/httparty/webservice.rb