class Camdram::HTTP
Attributes
api_token[W]
base_url[W]
user_agent[W]
Public Instance Methods
api_token?()
click to toggle source
base_url()
click to toggle source
get(url_slug, max_redirects = 10)
click to toggle source
Sends a HTTP-get request to the Camdram
API
@param url_slug [String] The URL slug to send the request to. @param max_redirects [Integer] The maximum number of redirects. @raise [Camdram::Error::RedirectError] Error
raised when too many redirects occur. @return [String]
# File lib/camdram/http.rb, line 17 def get(url_slug, max_redirects = 10) url = base_url + url_slug uri = URI(url) inner_get(uri, max_redirects) end
user_agent()
click to toggle source
Private Instance Methods
blank?(string)
click to toggle source
Returns true if a given string is blank
@param string [String] The string to test. @return [Boolean] True if blank, false otherwise.
# File lib/camdram/http.rb, line 57 def blank?(string) string.respond_to?(:empty?) ? string.empty? : false end
inner_get(uri, max_redirects)
click to toggle source
# File lib/camdram/http.rb, line 61 def inner_get(uri, max_redirects) # Raise an exception if we enter a redirect loop if max_redirects == 0 new Error::RedirectError(310, 'Too many redirects', nil) end response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{@api_token}" if api_token? request['User-Agent'] = @user_agent if user_agent? http.request(request) } case response when Net::HTTPSuccess then response.body when Net::HTTPRedirection then location = response['location'] warn "redirected to #{location}" if location.start_with?('http') # Handles full URL and external redirects inner_get(URI(location), max_redirects - 1) else # Handles slug-based redirects get(location, max_redirects - 1) end else Error.for(response) end end