Sha256: 6ec22555fa582c76a751cd393b6d31e00321499e5b2f44401b5604aaf453d384

Contents?: true

Size: 1.55 KB

Versions: 24

Compression:

Stored size: 1.55 KB

Contents

require 'uri'
require 'httparty'

module Locomotive
  module Steam
    class ExternalAPIService

      include ::HTTParty

      def consume(url, options = {})
        options[:base_uri], path = 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

        perform_request_to(path, options)
      end

      private

      def 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 perform_request_to(path, options)
        # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}"

        # sanitize the options
        options[:format]  = options[:format].gsub(/[\'\"]/, '').to_sym if options.has_key?(:format)
        options[:headers] = { 'User-Agent' => 'LocomotiveCMS' } if options[:with_user_agent]

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

        if response.code == 200
          HashConverter.to_underscore parsed_response
        else
          Locomotive::Common::Logger.error "[WebService] consumed #{path}, #{options.inspect}, response = #{response.inspect}"
          nil
        end
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
locomotivecms_steam-1.1.2 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.1.0.rc2 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.1.0.rc1 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.1 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.pre.beta.2 lib/locomotive/steam/services/external_api_service.rb
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/services/external_api_service.rb