Git Product home page Git Product logo

chatwork-ruby's Introduction

ChatWork

Ruby bindings of ChatWork API

Gem Version Build Status Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'chatwork'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chatwork

Usage

chatwork gem provides class method APIs and instance method APIs

Class method APIs

At first, set ChatWork.api_key or ChatWork.access_token.

ChatWork.api_key = "XXX"
# or
ChatWork.access_token = "XXX"

Another way, you can use ENV["CHATWORK_API_TOKEN"] or ENV["CHATWORK_ACCESS_TOKEN"] instead of ChatWork.api_key or ChatWork.access_token.

Then call class methods.

e.g. post message.

ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
#=> #<Hashie::Mash message_id="1234567890">

All available APIs are followings.

When you want to refresh access token, call ChatWork::Token.refresh_access_token.

ChatWork.client_id = "XXX"
ChatWork.client_secret = "XXX"
refresh_token = "XXX"
token = ChatWork::Token.refresh_access_token(refresh_token)
new_access_token = token["access_token"]

# Create message
ChatWork.access_token = new_access_token
ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")

Another way, you can use ENV["CHATWORK_CLIENT_ID"] or ENV["CHATWORK_CLIENT_SECRET"] instead of ChatWork.api_key or ChatWork.access_token.

Instance method APIs

client = ChatWork::Client.new(api_key: "XXX")
#or
client = ChatWork::Client.new(access_token: "XXX")

Then call instance methods.

e.g. post message.

client.create_message(room_id: 1234, body: "Hello, ChatWork!")
#=> #<Hashie::Mash message_id="1234567890">

All available APIs are followings.

http://www.rubydoc.info/gems/chatwork/ChatWork/Client

When you want to refresh access token, call ChatWork::OAuthClient#refresh_access_token.

refresh_token = "XXX"
oauth_client = ChatWork::OAuthClient.new(client_id: "xxx", client_secret: "xxx")
token = oauth_client.refresh_access_token(refresh_token)
new_access_token = token["access_token"]

# Create message
client = ChatWork::Client.new(access_token: new_access_token)
client.create_message(room_id: 1234, body: "Hello, ChatWork!")

Pros/Cons

Class method APIs are simple, but not threadsafe. Instance method APIs are threadsafe.

If you are using multiple api_key or access_token in an app and using multithreaded server (e.g. puma), we recommend instance method APIs.

Tips

Hashie::Mash

All API methods returns Hashie::Mash instance.

https://github.com/intridea/hashie#mash

Hashie::Mash is very useful!

body = ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
#=> #<Hashie::Mash message_id="1234567890">

body["message_id"]
#=> "1234567890"

body[:message_id]
#=> "1234567890"

body.message_id
#=> "1234567890"

Block arguments

All API methods supports block argument.

If block was given, return response body and response header through block arguments.

ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!") do |body, header|
  body
  #=> #<Hashie::Mash message_id="1234567890">

  header["X-RateLimit-Limit"]
  #=> "100"
  header["X-RateLimit-Remaining"]
  #=> "0"
  header["X-RateLimit-Reset"]
  #=> "1390941626"
end
client.create_message(room_id: 1234, body: "Hello, ChatWork!") do |body, header|
  body
  #=> #<Hashie::Mash message_id="1234567890">

  header["X-RateLimit-Limit"]
  #=> "100"
  header["X-RateLimit-Remaining"]
  #=> "0"
  header["X-RateLimit-Reset"]
  #=> "1390941626"
end

Reference

http://www.rubydoc.info/gems/chatwork

Development

cp .env.example .env
vi .env

./bin/console

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

chatwork-ruby's People

Contributors

amatsuda avatar asonas avatar cy-ken-goto avatar dependabot[bot] avatar eitoball avatar hatappi avatar henteko avatar sue445 avatar tamano avatar tokada avatar yoneapp avatar yuyaan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chatwork-ruby's Issues

api_keyはデフォルトで環境変数を使ってほしい

下記のREADME.mdのUSAGEにも記載されていますが、
本gemを使用する際にはまず初めにChatWork.api_key = "XXX"を記載しないといけません。

この実装だとメソッドを使用する際に毎回定義してあげなければならないです。
使用するケースを考えると、トークンは環境変数にまたせて読み込ませるような下記のコードになるかと思います。

ChatWork.api_key = ENV['CHATWORK_API_TOKEN']

本gemでは下記のコードのようにdefaultがnilになっているので、デフォルトを環境変数にしてあげるとよいかもしれません。
https://github.com/asonas/chatwork-ruby/blob/master/lib/chatwork.rb#L20

他のgemの実装などを見てみると例えばgoogleが出しているauthに使用するgemのgoogle-auth-library-rubyではuserがoptionsとして認証情報を指定しなかった場合に下記のコードのように環境変数を指定するようにしています
https://github.com/google/google-auth-library-ruby/blob/master/lib/googleauth/user_refresh.rb#L61

uninitialized constant ChatWork::AuthenticateError

$ be irb -rchatwork
irb(main):001:0> ChatWork::AuthenticateError
Traceback (most recent call last):
       16: from /Users/sue445/.rbenv/versions/2.5.0/bin/bundle:23:in `load'
       15: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/exe/bundle:22:in `<top (required)>'
       14: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
       13: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/exe/bundle:30:in `block in <top (required)>'
       12: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:18:in `start'
       11: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
       10: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:27:in `dispatch'
        9: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
        8: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
        7: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
        6: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli.rb:424:in `exec'
        5: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:28:in `run'
        4: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `kernel_load'
        3: from /Users/sue445/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `load'
        2: from /Users/sue445/.rbenv/versions/2.5.0/bin/irb:11:in `<top (required)>'
        1: from (irb):1
NameError (uninitialized constant ChatWork::AuthenticateError)
irb(main):002:0> ChatWork::APIError
=> ChatWork::APIError
irb(main):003:0> ChatWork::AuthenticateError
=> ChatWork::AuthenticateError

Why ?

autoload is missing here.
https://github.com/asonas/chatwork-ruby/blob/v0.6.1/lib/chatwork.rb#L6-L8

APIConnectionError#status is always nil

I haven't seen APIConnectionError has happened on the production environment so this is not a bug report.

APIConnectionError#status is always nil actually. This seems to be by design though, I would like to handle errors based on the HTTP status of the responses

def initialize(message, original_error = nil)

It sounds APIConnectionError happens iff a response from the Chatwork server is not a valid format. I'm unsure how frequent it has happened though, I think APIConnectionError must also have the status field because it's originally from HTTP requests, even if that's like an illegal case on the Chatwork domain. What do you think?

refresh_token

I use API to send notification, but I don't know how long refresh_token is and how can I extend refresh_token expiration

NoMethodError: undefined method `[]' for nil:NilClass

https://github.com/asonas/chatwork-ruby/blob/v0.6.1/lib/chatwork/chatwork_error.rb#L12

if body is nil, NoMethodError is raised

Backtrace (in my app)

ChatWork::AuthenticateError: Bearer error="invalid_token", error_description="The access token expired"
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/base_client.rb" line 27 in handle_response
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/base_client.rb" line 38 in block (2 levels) in <class:BaseClient>
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/entity_methods.rb" line 10 in _post
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/task.rb" line 66 in create
File "/app/app/models/user/api_module.rb" line 23 in block in create_task

NoMethodError: undefined method `[]' for nil:NilClass
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/chatwork_error.rb" line 12 in from_response
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/base_client.rb" line 27 in handle_response
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/base_client.rb" line 38 in block (2 levels) in <class:BaseClient>
File "/app/vendor/bundle/ruby/2.5.0/gems/chatwork-0.6.1/lib/chatwork/token.rb" line 24 in refresh_access_token
File "/app/app/models/user/api_module.rb" line 54 in refresh_access_token

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.