mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 11:43:44 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
41 lines
914 B
Ruby
41 lines
914 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'base64'
|
|
require 'json'
|
|
|
|
class SocialcastApi
|
|
|
|
attr_accessor :domain, :username, :password
|
|
|
|
def initialize(domain, username, password)
|
|
@domain = domain
|
|
@username = username
|
|
@password = password
|
|
end
|
|
|
|
def base_url
|
|
"https://#{@domain}.socialcast.com/api"
|
|
end
|
|
|
|
def headers
|
|
encoded = Base64.encode64 "#{@username}:#{@password}"
|
|
{ Authorization: "Basic #{encoded.strip!}", Accept: "application/json" }
|
|
end
|
|
|
|
def request(url)
|
|
JSON.parse(Excon.get(url, headers: headers))
|
|
end
|
|
|
|
def list_users(opts = {})
|
|
page = opts[:page] ? opts[:page] : 1
|
|
response = request "#{base_url}/users?page=#{page}"
|
|
response['users'].sort { |u| u['id'] }
|
|
end
|
|
|
|
def list_messages(opts = {})
|
|
page = opts[:page] ? opts[:page] : 1
|
|
response = request "#{base_url}/messages?page=#{page}"
|
|
response['messages'].sort { |m| m['id'] }
|
|
end
|
|
end
|