lib/panoptes/client.rb in panoptes-client-0.1.0 vs lib/panoptes/client.rb in panoptes-client-0.1.1
- old
+ new
@@ -13,10 +13,17 @@
include Panoptes::Client::Me
include Panoptes::Client::Projects
include Panoptes::Client::Subjects
include Panoptes::Client::UserGroups
+ # A client is the main interface to the API.
+ #
+ # @param auth [Hash] Authentication details
+ # * either nothing,
+ # * a hash with +:token+ (an existing OAuth user token),
+ # * or a hash with +:client_id+ and +:client_secret+ (a keypair for an OAuth Application).
+ # @param url [String] Optional override for the API location to use. Defaults to the official production environment.
def initialize(auth: {}, url: "https://panoptes.zooniverse.org")
@conn = Faraday.new(url: url) do |faraday|
case
when auth[:token]
faraday.request :panoptes_access_token, url: url, access_token: token
@@ -38,15 +45,15 @@
def post(path, body = {})
conn.post("/api" + path, body).body
end
# Get a path and perform automatic depagination
- def paginate(path, resource: nil)
+ def paginate(path, query, resource: nil)
resource = path.split("/").last if resource.nil?
- data = last_response = get(path)
+ data = last_response = get(path, query)
while next_path = last_response["meta"][resource]["next_href"]
- last_response = get(next_path)
+ last_response = get(next_path, query)
if block_given?
yield data, last_response
else
data[resource].concat(last_response[resource]) if data[resource].is_a?(Array)
data["meta"][resource].merge!(last_response["meta"][resource])