Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active July 3, 2023 09:46
Show Gist options
  • Save pcreux/4ab8264bd83263852593b8ebab5e3e87 to your computer and use it in GitHub Desktop.
Save pcreux/4ab8264bd83263852593b8ebab5e3e87 to your computer and use it in GitHub Desktop.
A client for Basecamp 4
#!/usr/bin/env ruby
require 'bundler/inline'
require 'json'
require 'logger'
# Remove all users from a project except one.
def run
account = BC4::Account.new(ENV.fetch('ACCESS_TOKEN'), ENV.fetch('ACCOUNT_ID'))
project = account.project(30211378)
people_ids = project.people.map { _1.fetch("id") }
ids_to_delete = people_ids - [31106252]
project.revoke(ids_to_delete)
end
gemfile do
source 'https://rubygems.org'
gem 'http'
end
module BC4
Account = Struct.new(:access_token, :account_id) do
def project(project_id)
Project.new(client, project_id)
end
def client
Client.new(access_token, account_id)
end
end
Project = Struct.new(:client, :project_id) do
def people
client.get("/projects/#{project_id}/people.json")
end
def grant(people_ids)
client.put("/projects/#{project_id}/people/users.json", grant: people_ids)
end
def revoke(people_ids)
client.put("/projects/#{project_id}/people/users.json", revoke: people_ids)
end
def add(details)
client.put("/projects/#{project_id}/people/users.json", create: details)
end
end
Client = Struct.new(:access_token, :account_id) do
def get(path)
response = http.get "https://3.basecampapi.com/#{account_id}#{path}"
raise response.status.to_s if response.status.client_error?
JSON.parse(response.to_s)
end
def put(path, args)
response = http.put "https://3.basecampapi.com/#{account_id}#{path}", json: args
raise response.status.to_s if response.status.client_error?
JSON.parse(response.to_s)
end
def http
HTTP
.use(logging: {logger: Logger.new(STDOUT)})
.headers(accept: "application/json", "User-Agent": "bc4-client.rb")
.auth("Bearer #{access_token}")
end
end
end
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment