Class: Panoptes::Client

Inherits:
Object
  • Object
show all
Includes:
Me, Projects, Subjects, UserGroups
Defined in:
lib/panoptes/client.rb,
lib/panoptes/client/me.rb,
lib/panoptes/client/version.rb,
lib/panoptes/client/projects.rb,
lib/panoptes/client/subjects.rb,
lib/panoptes/client/user_groups.rb

Defined Under Namespace

Modules: Me, Projects, Subjects, UserGroups Classes: ConnectionFailed, GenericError, ResourceNotFound, ServerError

Constant Summary collapse

VERSION =
"0.2.2"

Instance Method Summary collapse

Methods included from UserGroups

#create_user_group, #join_user_group, #user_groups

Methods included from Subjects

#retire_subject

Methods included from Projects

#projects

Methods included from Me

#me

Constructor Details

#initialize(auth: {}, url: "https://panoptes.zooniverse.org") ⇒ Client

A client is the main interface to the API.

Parameters:

  • auth (Hash) (defaults to: {})

    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).

  • url (String) (defaults to: "https://panoptes.zooniverse.org")

    Optional override for the API location to use. Defaults to the official production environment.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/panoptes/client.rb', line 30

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: auth[:token]
    when auth[:client_id] && auth[:client_secret]
      faraday.request :panoptes_client_credentials, url: url, client_id: auth[:client_id], client_secret: auth[:client_secret]
    end

    faraday.request :panoptes_api_v1
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Method Details

#delete(path, query = {}) ⇒ Object



66
67
68
69
# File 'lib/panoptes/client.rb', line 66

def delete(path, query = {})
  response = conn.delete("/api" + path, query)
  handle_response(response)
end

#get(path, query = {}) ⇒ Object



46
47
48
49
# File 'lib/panoptes/client.rb', line 46

def get(path, query = {})
  response = conn.get("/api" + path, query)
  handle_response(response)
end

#paginate(path, query, resource: nil) ⇒ Object

Get a path and perform automatic depagination



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/panoptes/client.rb', line 72

def paginate(path, query, resource: nil)
  resource = path.split("/").last if resource.nil?
  data = last_response = get(path, query)

  while next_path = last_response["meta"][resource]["next_href"]
    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])
      data["links"].merge!(last_response["links"])
    end
  end

  data
end

#patch(path, body = {}) ⇒ Object



61
62
63
64
# File 'lib/panoptes/client.rb', line 61

def patch(path, body = {})
  response = conn.patch("/api" + path, body)
  handle_response(response)
end

#post(path, body = {}) ⇒ Object



51
52
53
54
# File 'lib/panoptes/client.rb', line 51

def post(path, body = {})
  response = conn.post("/api" + path, body)
  handle_response(response)
end

#put(path, body = {}) ⇒ Object



56
57
58
59
# File 'lib/panoptes/client.rb', line 56

def put(path, body = {})
  response = conn.put("/api" + path, body)
  handle_response(response)
end