Class: Panoptes::Client
- Inherits:
-
Object
- Object
- Panoptes::Client
- 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.5"
Instance Method Summary collapse
- #delete(path, query = {}, etag: nil) ⇒ Object
- #get(path, query = {}) ⇒ Object
-
#initialize(auth: {}, url: "https://panoptes.zooniverse.org") ⇒ Client
constructor
A client is the main interface to the API.
-
#paginate(path, query, resource: nil) ⇒ Object
Get a path and perform automatic depagination.
- #patch(path, body = {}) ⇒ Object
- #post(path, body = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
Methods included from UserGroups
#create_user_group, #delete_user_group, #join_user_group, #remove_user_from_user_group, #user_groups
Methods included from 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
Constructor Details
#initialize(auth: {}, url: "https://panoptes.zooniverse.org") ⇒ Client
A client is the main interface to the API.
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 = {}, etag: nil) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/panoptes/client.rb', line 66 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
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/panoptes/client.rb', line 75 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 |