lib/dingbot/client.rb in dingbot-0.1.1 vs lib/dingbot/client.rb in dingbot-0.2.0
- old
+ new
@@ -1,7 +1,10 @@
require 'httparty'
+require 'dingbot/configuration'
require 'dingbot/message/base'
+require 'dingbot/message/text'
+require 'dingbot/message/markdown'
module DingBot
# @private
class Client
include HTTParty
@@ -9,12 +12,20 @@
format :json
headers "Content-Type" => "application/json"
attr_accessor :access_token
- def initialize(access_token='')
- @access_token = access_token
+ # @private
+ attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
+
+ # Creates a new API.
+ # @raise [Error:MissingCredentials]
+ def initialize(options={})
+ options = DingBot.options.merge(options)
+ (Configuration::VALID_OPTIONS_KEYS).each do |key|
+ send("#{key}=", options[key]) if options[key]
+ end
end
# Parse response body.
def self.parse(body)
begin
@@ -30,10 +41,20 @@
rescue JSON::ParserError
raise Error::Parsing.new "The response is not a valid JSON"
end
def send_msg(message)
- validate self.class.post(DingBot::ENDPOINT, {query: {access_token: @access_token}, body: message.to_json})
+ validate self.class.post(@endpoint, {query: {access_token: @access_token}, body: message.to_json})
+ end
+
+ def send_text(content)
+ message = DingBot::Message::Text.new(content)
+ send_msg(message)
+ end
+
+ def send_markdown(title, text)
+ message = DingBot::Message::Markdown.new(title, text)
+ send_msg(message)
end
# Checks the response code for common errors.
# Returns parsed response for successful requests.
def validate(response)