Sha256: ff6499dd6e1c1cd16b084cc30edb85626415a34ee8a767e0d51e256c85f37bd6

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

require 'uri'

module Locomotive
  module Httparty
    class Webservice

      include ::HTTParty

      def self.consume(url, options = {})
        options[:base_uri], path = self.extract_base_uri_and_path(url)

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

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

        self.perform_request_to(path, options)
      end

      def self.extract_base_uri_and_path(url)
        url = HTTParty.normalize_base_uri(url)

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

        [base_uri, path]
      end

      def self.perform_request_to(path, options)
        # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}"

        response        = self.get(path, options)
        parsed_response = response.parsed_response

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

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
locomotive_cms-2.5.2 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.5.1 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.5.0 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.5.0.rc3 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.5.0.rc2 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.5.0.rc1 lib/locomotive/httparty/webservice.rb
locomotive_cms-2.4.1 lib/locomotive/httparty/webservice.rb