Class: BotMetrics::Client
- Inherits:
-
Object
- Object
- BotMetrics::Client
- Defined in:
- lib/botmetrics.rb
Constant Summary collapse
- DEFAULT_API_HOST =
"https://www.getbotmetrics.com".freeze
Instance Method Summary collapse
-
#initialize(api_key: nil, bot_id: nil, api_host: nil) ⇒ Client
constructor
A new instance of Client.
- #message(team_id, opts = {}) ⇒ Object
- #register_bot!(token, opts = {}) ⇒ Object
- #track(event) ⇒ Object
Constructor Details
#initialize(api_key: nil, bot_id: nil, api_host: nil) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/botmetrics.rb', line 8 def initialize(api_key: nil, bot_id: nil, api_host: nil) @api_key = api_key || ENV['BOTMETRICS_API_KEY'] @bot_id = bot_id || ENV['BOTMETRICS_BOT_ID'] @api_host = api_host || ENV['BOTMETRICS_API_HOST'] || DEFAULT_API_HOST if blank?(@api_key) raise ArgumentError.new("Missing argument api_key. Please pass api_key in as an argument.") end if blank?(@bot_id) raise ArgumentError.new("Missing argument bot_id. Please pass bot_id in as an argument.") end end |
Instance Method Details
#message(team_id, opts = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/botmetrics.rb', line 55 def (team_id, opts = {}) channel = opts[:channel] user = opts[:user] text = opts[:text] = opts[:attachments] if blank?(channel) && blank?(user) raise ArgumentError.new("Missing argument channel and user. Please provide at least one.") end if blank?(text) && blank?() raise ArgumentError.new("Missing argument text and attachments. Please provide at least one.") end params = { "message[team_id]" => team_id, "message[channel]" => channel, "message[user]" => user, "message[text]" => text, "message[attachments]" => () }.delete_if { |_, v| v.nil? } response = HTTP.auth(api_key).post("#{api_url}/messages", params: params) response.code == 202 end |
#register_bot!(token, opts = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/botmetrics.rb', line 22 def register_bot!(token, opts = {}) params = { "format" => "json", "instance[token]" => token } created_at = opts[:created_at] || opts["created_at"] params["instance[created_at]"] = created_at.to_i if created_at.to_i != 0 response = HTTP.auth(api_key).post("#{api_url}/instances", params: params) response.code == 201 end |
#track(event) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/botmetrics.rb', line 33 def track(event) event_json = nil if event.is_a?(Hash) event_json = event.to_json elsif event.is_a?(String) begin event_json = JSON.parse(event) rescue JSON::ParserError raise ArgumentError.new("event is not a valid JSON string or Hash") end event_json = event end if event_json.nil? raise ArgumentError.new("event is not a valid JSON string or Hash") end response = HTTP.auth(api_key).post("#{api_url}/events", params: {event: event_json, format: 'json'}) response.code == 202 end |