Sha256: 38bc461a343a5885b3e27bc8b48b0a0dbbf0efe13518b44c74a48d518e4950b0

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

module Access
  class Request
    include HTTParty

    def get(path, api_type, options={}, &block)
      url = set_base(api_type, path)
      results = self.class.get(url, headers: headers(options[:access_token]), query: options, timeout: (options[:access_timeout] || 3))
      if should_return_json?(options[:return_json])
        hashify_results?(options[:hashify]) ? results.hashify : results
      else
        block.call results
      end
    rescue Net::ReadTimeout, Net::OpenTimeout
      # block.call({"message"=>"Request Timeout Error", "status"=>408})
      raise Access::Error::Timeout
    end

    def get_for_filter(path, api_type, filter, options={}, &block)
      url = set_base(api_type, path)
      results = self.class.get(url, headers: headers(options[:access_token]), body: filter, timeout: (options[:access_timeout] || 3))
      if should_return_json?(options[:return_json])
        hashify_results?(options[:hashify]) ? results.hashify : results
      else
        block.call results
      end
    rescue Net::ReadTimeout, Net::OpenTimeout
      raise Access::Error::Timeout
    end

    private

    def set_base(api_type, path)
      "https://#{ api_type }#{ Access::Config::DOMAINS[Access.config.api_environment] }.adcrws.com/v1#{path}"
    end

    def 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
access-2.0.5 lib/access/request.rb
access-2.0.4 lib/access/request.rb