2016-05-26 14:54:38 +08:00
|
|
|
require 'base64'
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
class SocialcastApi
|
|
|
|
|
|
|
|
attr_accessor :domain, :username, :password
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(domain, username, password)
|
2016-05-26 14:54:38 +08:00
|
|
|
@domain = domain
|
|
|
|
@username = username
|
|
|
|
@password = password
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_url
|
|
|
|
"https://#{@domain}.socialcast.com/api"
|
|
|
|
end
|
|
|
|
|
|
|
|
def headers
|
|
|
|
encoded = Base64.encode64 "#{@username}:#{@password}"
|
2017-07-28 09:20:09 +08:00
|
|
|
{ Authorization: "Basic #{encoded.strip!}", Accept: "application/json" }
|
2016-05-26 14:54:38 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def request(url)
|
|
|
|
JSON.parse(Excon.get(url, headers: headers))
|
2016-05-26 14:54:38 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def list_users(opts = {})
|
2016-05-26 14:54:38 +08:00
|
|
|
page = opts[:page] ? opts[:page] : 1
|
|
|
|
response = request "#{base_url}/users?page=#{page}"
|
2017-07-28 09:20:09 +08:00
|
|
|
response['users'].sort { |u| u['id'] }
|
2016-05-26 14:54:38 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def list_messages(opts = {})
|
2016-05-26 14:54:38 +08:00
|
|
|
page = opts[:page] ? opts[:page] : 1
|
|
|
|
response = request "#{base_url}/messages?page=#{page}"
|
2017-07-28 09:20:09 +08:00
|
|
|
response['messages'].sort { |m| m['id'] }
|
2016-05-26 14:54:38 +08:00
|
|
|
end
|
|
|
|
end
|