module Access class Request include HTTParty attr_reader :url, :headers, :timeout, :return_json, :use_hasify def base_setup(path, api_type, options) @url = set_base(api_type, path, options.delete(:api_environment)) @headers = set_headers options.delete(:access_token) @timeout = options.delete(:access_timeout) || Access.config.access_timeout @return_json = should_return_json?(options.delete(:return_json)) @use_hasify = hashify_results?(options.delete(:hashify)) end def get(path, api_type, options={}, &block) base_setup(path, api_type, options) results = self.class.get(url, headers: headers, query: options, timeout: timeout) if return_json use_hasify ? results.hashify : results else block.call results end rescue Net::ReadTimeout, Net::OpenTimeout raise Access::Error::Timeout rescue EOFError raise Access::Error::NoData end def post(path, api_type, options={}, &block) base_setup(path, api_type, options) results = self.class.post(url, headers: headers, body: options.to_json, timeout: timeout) if return_json use_hasify ? results.hashify : results else block.call results end rescue Net::ReadTimeout, Net::OpenTimeout raise Access::Error::Timeout rescue EOFError raise Access::Error::NoData end def delete(path, api_type, options={}, &block) base_setup(path, api_type, options) results = self.class.delete(url, headers: headers, body: options.to_json, timeout: timeout) if return_json use_hasify ? results.hashify : results else block.call results end rescue Net::ReadTimeout, Net::OpenTimeout raise Access::Error::Timeout rescue EOFError raise Access::Error::NoData end def post_for_filter(path, api_type, filter, options={}, &block) base_setup(path, api_type, options) results = self.class.post(url, headers: headers, body: filter, timeout: timeout) if return_json use_hasify ? results.hashify : results else block.call results end rescue Net::ReadTimeout, Net::OpenTimeout raise Access::Error::Timeout rescue EOFError raise Access::Error::NoData end private def set_base(api_type, path, environment) if environment "https://#{ api_type }#{ Access::Config::DOMAINS[environment] }.adcrws.com/v1#{path}" else "https://#{ api_type }#{ Access::Config::DOMAINS[Access.config.api_environment] }.adcrws.com/v1#{path}" end end def set_headers(token_param) raise Access::Error::NoAccessToken if (access_token = token_param || Access.config.access_token).nil? {"Access-Token" => access_token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'} end def should_return_json?(return_json_param) return_json_param == 'true' || (return_json_param != 'false' && Access.config.return_json == 'true') end def hashify_results?(hashify_param) hashify_param == 'true' || (hashify_param != 'false' && Access.config.hashify == 'true') end end end