Sha256: 708e8497c4d3f13eaec05dcb76f526107d14e2126839c3dd67223cbf853423bd
Contents?: true
Size: 1.82 KB
Versions: 2
Compression:
Stored size: 1.82 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 # authorization header ? header_auth = options.delete(:header_auth) options[:headers] = { 'Authorization' => header_auth } if header_auth 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) if options[:with_user_agent] user_agent = { 'User-Agent' => 'LocomotiveCMS' } options[:headers] ? options[:headers].merge!(user_agent) : options[:headers] = user_agent end 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
locomotivecms_steam-1.2.0.rc2 | lib/locomotive/steam/services/external_api_service.rb |
locomotivecms_steam-1.2.0.rc1 | lib/locomotive/steam/services/external_api_service.rb |