Class: DingBot::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dingbot/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new API.

Raises:

  • (Error:MissingCredentials)


22
23
24
25
26
27
# File 'lib/dingbot/client.rb', line 22

def initialize(options={})
  options = DingBot.options.merge(options)
  (Configuration::VALID_OPTIONS_KEYS).each do |key|
    send("#{key}=", options[key]) if options[key]
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



15
16
17
# File 'lib/dingbot/client.rb', line 15

def access_token
  @access_token
end

Class Method Details

.decode(response) ⇒ Object

Decodes a JSON response into Ruby object.



39
40
41
42
43
# File 'lib/dingbot/client.rb', line 39

def self.decode(response)
  JSON.load response
rescue JSON::ParserError
  raise Error::Parsing.new "The response is not a valid JSON"
end

.parse(body) ⇒ Object

Parse response body.



30
31
32
33
34
35
36
# File 'lib/dingbot/client.rb', line 30

def self.parse(body)
  begin
    decode(body)
  rescue => e
    raise Error::Parsing.new "Couldn't parse a response body"
  end
end

Instance Method Details

#send_markdown(title, text) ⇒ Object



54
55
56
57
# File 'lib/dingbot/client.rb', line 54

def send_markdown(title, text)
  message = DingBot::Message::Markdown.new(title, text)
  send_msg(message)
end

#send_msg(message) ⇒ Object



45
46
47
# File 'lib/dingbot/client.rb', line 45

def send_msg(message)
  validate self.class.post(@endpoint, {query: {access_token: @access_token}, body: message.to_json})
end

#send_text(content) ⇒ Object



49
50
51
52
# File 'lib/dingbot/client.rb', line 49

def send_text(content)
  message = DingBot::Message::Text.new(content)
  send_msg(message)
end

#validate(response) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dingbot/client.rb', line 61

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
  parsed.client = self if parsed.respond_to?(:client=)
  parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
  parsed
end