module Goodreads::Request

Constants

API_FORMAT
API_URL

Protected Instance Methods

http_request(path, params) click to toggle source

Perform an API request using API key

path - Request path params - Parameters hash

# File lib/goodreads/request.rb, line 31
def http_request(path, params)
  token = api_key || Goodreads.configuration[:api_key]

  fail(Goodreads::ConfigurationError, "API key required.") if token.nil?

  params.merge!(format: API_FORMAT, key: token)
  url = "#{API_URL}#{path}"

  resp = RestClient.get(url, params: params) do |response, request, result, &block|
    case response.code
    when 200
      response.return!(&block)
    when 401
      fail(Goodreads::Unauthorized)
    when 403
      fail(Goodreads::Forbidden)
    when 404
      fail(Goodreads::NotFound)
    when 500..599
      fail(Goodreads::ServerError.new(response.code))
    else
      fail(Goodreads::UnknownError.new(response.code))
    end
  end

  parse(resp)
end
oauth_request(path, params = {}) click to toggle source

Perform an OAuth API GET request. Goodreads must have been initialized with a valid OAuth access token.

path - Request path params - Parameters hash

# File lib/goodreads/request.rb, line 64
def oauth_request(path, params = {})
  oauth_request_method(:get, path, params)
end
oauth_request_method(http_method, path, params = {}) click to toggle source

Perform an OAuth API request. Goodreads must have been initialized with a valid OAuth access token.

http_method - HTTP verb supported by OAuth gem (one of :get, :post, :delete, etc.) path - Request path params - Parameters hash

# File lib/goodreads/request.rb, line 74
def oauth_request_method(http_method, path, params = {})
  fail "OAuth access token required!" unless @oauth_token

  headers = { "Accept" => "application/xml" }

  resp = if http_method == :get || http_method == :delete
    if params
      url_params = params.map { |k, v| "#{k}=#{v}" }.join("&")
      path = "#{path}?#{url_params}"
    end
    @oauth_token.request(http_method, path, headers)
  else
    @oauth_token.request(http_method, path, params || {}, headers)
  end

  case resp
  when Net::HTTPUnauthorized
    fail Goodreads::Unauthorized
  when Net::HTTPNotFound
    fail Goodreads::NotFound
  end

  parse(resp)
end
parse(resp) click to toggle source
# File lib/goodreads/request.rb, line 99
def parse(resp)
  hash = Hash.from_xml(resp.body)["GoodreadsResponse"]
  hash.delete("Request")
  hash
end
request(path, params = {}) click to toggle source

Perform an API request using API key or OAuth token

path - Request path params - Parameters hash

Will make a request with the configured API key (application authentication) or OAuth token (user authentication) if available.

# File lib/goodreads/request.rb, line 19
def request(path, params = {})
  if oauth_configured?
    oauth_request(path, params)
  else
    http_request(path, params)
  end
end