class DingBot::Client

@private

Public Class Methods

decode(response) click to toggle source

Decodes a JSON response into Ruby object.

# File lib/dingbot/client.rb, line 41
def self.decode(response)
  JSON.load response
rescue JSON::ParserError
  raise Error::Parsing.new "The response is not a valid JSON"
end
new(options={}) click to toggle source

Creates a new API. @raise [Error:MissingCredentials]

# File lib/dingbot/client.rb, line 24
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(body) click to toggle source

Parse response body.

# File lib/dingbot/client.rb, line 32
def self.parse(body)
  begin
    decode(body)
  rescue => e
    raise Error::Parsing.new "Couldn't parse a response body"
  end
end

Public Instance Methods

send_markdown(title, text) click to toggle source
# File lib/dingbot/client.rb, line 69
def send_markdown(title, text)
  message = DingBot::Message::Markdown.new(title, text)
  send_msg(message)
end
send_msg(message) click to toggle source
# File lib/dingbot/client.rb, line 47
def send_msg(message)
  query = {
    access_token: @access_token,
  }

  if !@secret.nil? and !@secret.empty?
    timestamp = (Time.now.to_f * 1000).to_i

    query.merge!({
      timestamp: timestamp,
      sign: Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @secret, "#{timestamp}\n#{@secret}")).strip
    })
  end

  validate self.class.post(@endpoint, { query: query, body: message.to_json })
end
send_text(content) click to toggle source
# File lib/dingbot/client.rb, line 64
def send_text(content)
  message = DingBot::Message::Text.new(content)
  send_msg(message)
end
validate(response) click to toggle source

Checks the response code for common errors. Returns parsed response for successful requests.

# File lib/dingbot/client.rb, line 76
def validate(response)
  error_klass = case response.code
                  when 400 then
                    Error::BadRequest
                  when 401 then
                    Error::Unauthorized
                  when 403 then
                    Error::Forbidden
                  when 404 then
                    Error::NotFound
                  when 405 then
                    Error::MethodNotAllowed
                  when 409 then
                    Error::Conflict
                  when 422 then
                    Error::Unprocessable
                  when 500 then
                    Error::InternalServerError
                  when 502 then
                    Error::BadGateway
                  when 503 then
                    Error::ServiceUnavailable
                end

  fail error_klass.new(response) if error_klass
  parsed = response.parsed_response
  
  body = JSON.parse(response.body)
  errcode = body["errcode"]
  fail body["errmsg"] if errcode != 0

  parsed.client = self if parsed.respond_to?(:client=)
  parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
  parsed
end