module WOTC::Request

Defines HTTP request methods

Public Instance Methods

delete(path, options={}) click to toggle source

HTTP DELETE request

# File lib/wotc/request.rb, line 22
def delete(path, options={})
  request(:delete, path, options)
end
get(path, options={}) click to toggle source

HTTP GET request

# File lib/wotc/request.rb, line 7
def get(path, options={})
  request(:get, path, options)
end
post(path, options={}) click to toggle source

HTTP POST request

# File lib/wotc/request.rb, line 12
def post(path, options={})
  request(:post, path, options)
end
put(path, options={}) click to toggle source

HTTP PUT request

# File lib/wotc/request.rb, line 17
def put(path, options={})
  request(:put, path, options)
end

Private Instance Methods

paginate(path, options={}) click to toggle source

Auto-pagination for HTTP get. return all resources when auto_paginate is set true. return just one page when auto_paginate is set false.

# File lib/wotc/request.rb, line 31
def paginate(path, options={})
  per_page = options[:per_page] || options["per_page"] || @per_page
  page = options[:page] || options["page"]
  response = get(path+"?page=1&per_page=#{per_page}")

  # return one page results without pagination.
  return response if !@auto_paginate

  # return all results when enabled auto paginate
  last_response = response.dup
  data = response["data"]
  while last_response["next_page_url"]
    last_response = get(last_response["next_page_url"] + "&per_page=#{per_page}")
    data.concat(last_response["data"]) if last_response["data"].is_a?(Array)
  end

  return data
end
request(method, path, options) click to toggle source
# File lib/wotc/request.rb, line 50
def request(method, path, options)
  response = connection.send(method) do |request|
    case method
    when :get, :delete
      request.url(URI.encode(path), options)
    when :post, :put
      request.path = URI.encode(path)
      request.body = options
    end
  end

  response
end