Class: DingBot::Client
- Inherits:
-
Object
- Object
- DingBot::Client
- Includes:
- HTTParty
- Defined in:
- lib/dingbot/client.rb
Class Method Summary collapse
-
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
-
.parse(body) ⇒ Object
Parse response body.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Client
constructor
Creates a new API.
- #send_markdown(title, text) ⇒ Object
- #send_msg(message) ⇒ Object
- #send_text(content) ⇒ Object
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Constructor Details
#initialize(options = {}) ⇒ Client
Creates a new API.
24 25 26 27 28 29 |
# File 'lib/dingbot/client.rb', line 24 def initialize(={}) = DingBot..merge() (Configuration::VALID_OPTIONS_KEYS).each do |key| send("#{key}=", [key]) if [key] end end |
Class Method Details
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
41 42 43 44 45 |
# 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 |
.parse(body) ⇒ Object
Parse response body.
32 33 34 35 36 37 38 |
# 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 |
Instance Method Details
#send_markdown(title, text) ⇒ Object
69 70 71 72 |
# File 'lib/dingbot/client.rb', line 69 def send_markdown(title, text) = DingBot::Message::Markdown.new(title, text) send_msg() end |
#send_msg(message) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dingbot/client.rb', line 47 def send_msg() query = { access_token: @access_token, } if !@secret.nil? and !@secret.empty? = (Time.now.to_f * 1000).to_i query.merge!({ 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: .to_json }) end |
#send_text(content) ⇒ Object
64 65 66 67 |
# File 'lib/dingbot/client.rb', line 64 def send_text(content) = DingBot::Message::Text.new(content) send_msg() end |
#validate(response) ⇒ Object
Checks the response code for common errors. Returns parsed response for successful requests.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dingbot/client.rb', line 76 def validate(response) error_klass = case response.code when 400 then Error::BadRequest when 401 then Error:: 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 |