A simple ruby/http client for Basecamp 4.
Getting an oauth 2 token for basecamp is no fun.
Try this: https://github.com/pcreux/doorkeeper-sinatra-client-for-basecamp
A simple ruby/http client for Basecamp 4.
Getting an oauth 2 token for basecamp is no fun.
Try this: https://github.com/pcreux/doorkeeper-sinatra-client-for-basecamp
#!/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 |