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
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #get(path, query = {}) ⇒ Object
-
#initialize(auth: {}, url: "https://panoptes.zooniverse.org") ⇒ Client
constructor
A new instance of Client.
-
#paginate(path, resource: nil) ⇒ Object
Get a path and perform automatic depagination.
- #post(path, body = {}) ⇒ Object
Methods included from UserGroups
Methods included from Subjects
Methods included from Projects
Methods included from Me
Constructor Details
#initialize(auth: {}, url: "https://panoptes.zooniverse.org") ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/panoptes/client.rb', line 18 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 when auth[:client_id] && auth[:client_secret] faraday.request :panoptes_client_credentials, url: url, client_id: client_id, client_secret: client_secret end faraday.request :panoptes_api_v1 faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end end |
Instance Method Details
#get(path, query = {}) ⇒ Object
34 35 36 |
# File 'lib/panoptes/client.rb', line 34 def get(path, query = {}) conn.get("/api" + path, query).body end |
#paginate(path, resource: nil) ⇒ Object
Get a path and perform automatic depagination
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/panoptes/client.rb', line 43 def paginate(path, resource: nil) resource = path.split("/").last if resource.nil? data = last_response = get(path) while next_path = last_response["meta"][resource]["next_href"] last_response = get(next_path) 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 |
#post(path, body = {}) ⇒ Object
38 39 40 |
# File 'lib/panoptes/client.rb', line 38 def post(path, body = {}) conn.post("/api" + path, body).body end |