Sha256: 00f07014c8038e5364321f02c05f9abc9a9dd53ea18a51d07cc51c2a0e1ab7d5
Contents?: true
Size: 1.54 KB
Versions: 6
Compression:
Stored size: 1.54 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}" # 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.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
6 entries across 6 versions & 1 rubygems