Class: DingBot::Client
- Inherits:
-
Object
- Object
- DingBot::Client
- Includes:
- HTTParty
- Defined in:
- lib/dingbot/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
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(access_token = '') ⇒ Client
constructor
A new instance of Client.
- #send_msg(message) ⇒ Object
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Constructor Details
#initialize(access_token = '') ⇒ Client
Returns a new instance of Client.
14 15 16 |
# File 'lib/dingbot/client.rb', line 14 def initialize(access_token='') @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
12 13 14 |
# File 'lib/dingbot/client.rb', line 12 def access_token @access_token end |
Class Method Details
Instance Method Details
#send_msg(message) ⇒ Object
34 35 36 |
# File 'lib/dingbot/client.rb', line 34 def send_msg() validate self.class.post(DingBot::ENDPOINT, {query: {access_token: @access_token}, body: .to_json}) end |
#validate(response) ⇒ Object
Checks the response code for common errors. Returns parsed response for successful requests.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dingbot/client.rb', line 40 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 |