Class: Panoptes::Client

Inherits:
Object
  • Object
show all
Includes:
Me, Projects, SubjectSets, Subjects, UserGroups, Workflows
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/workflows.rb,
lib/panoptes/client/user_groups.rb,
lib/panoptes/client/subject_sets.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.2.6"

Instance Method Summary collapse

Methods included from Workflows

#create_workflow, #workflow

Methods included from UserGroups

#create_user_group, #delete_user_group, #join_user_group, #remove_user_from_user_group, #user_groups

Methods included from SubjectSets

#add_subjects_to_subject_set, #create_subject_set, #subject_set, #update_subject_set

Methods included from Subjects

#retire_subject, #subjects

Methods included from Projects

#create_aggregations_export, #create_classifications_export, #create_subjects_export, #create_workflow_contents_export, #create_workflows_export, #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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/panoptes/client.rb', line 34

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 = {}, etag: nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/panoptes/client.rb', line 76

def delete(path, query = {}, etag: nil)
  headers = {}
  headers["If-Match"] = etag if etag

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

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



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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/panoptes/client.rb', line 85

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 = {}, etag: nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/panoptes/client.rb', line 68

def patch(path, body = {}, etag: nil)
  headers = {}
  headers["If-Match"] = etag if etag

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

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



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

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

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



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

def put(path, body = {}, etag: nil)
  headers = {}
  headers["If-Match"] = etag if etag

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