Skip to content

Commit

Permalink
Merge f2230c5 into a1e221d
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Jan 10, 2018
2 parents a1e221d + f2230c5 commit 8480b5f
Show file tree
Hide file tree
Showing 26 changed files with 446 additions and 169 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CHATWORK_API_TOKEN=
CHATWORK_ACCESS_TOKEN=
CHATWORK_CLIENT_ID=
CHATWORK_CLIENT_SECRET=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ test/version_tmp
tmp
.ruby-gemset
.ruby-version
.env
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
$ CHATWORK_CLIENT_ID=xxx CHATWORK_CLIENT_SECRET=xxx REFRESH_TOKEN=xxx ruby refresh_access_token.rb
```

## Development
```bash
cp .env.example .env
vi .env

./bin/console
```

## Contributing

1. Fork it
Expand Down
10 changes: 10 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "chatwork"
require "dotenv"

Dotenv.load

require "pry"
Pry.start
3 changes: 2 additions & 1 deletion chatwork.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
spec.license = "MIT"

spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

Expand All @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "activesupport"
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "dotenv"
spec.add_development_dependency "onkcop", "0.52.1.0"
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "rake"
Expand Down
4 changes: 2 additions & 2 deletions lib/chatwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module ChatWork
autoload :ChatWorkError, "chatwork/chatwork_error"
autoload :Client, "chatwork/client"
autoload :Contacts, "chatwork/contacts"
autoload :Entity, "chatwork/entity"
autoload :EntityMethods, "chatwork/entity_methods"
autoload :Me, "chatwork/me"
autoload :Member, "chatwork/member"
autoload :Message, "chatwork/message"
autoload :MyStatus, "chatwork/my_status"
autoload :MyTask, "chatwork/my_task"
autoload :OAuthClient, "chatwork/oauth_client"
autoload :Operations, "chatwork/operations"
autoload :Room, "chatwork/room"
autoload :Task, "chatwork/task"
autoload :Token, "chatwork/token"
Expand Down
3 changes: 2 additions & 1 deletion lib/chatwork/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def initialize(api_base, api_version, header)
def handle_response(response)
case response.status
when 204
ChatWork::ChatWorkError.from_response(response.status, response.body, response.headers)
# HTTP status 204 doesn't return json
response.body
when 200..299
begin
JSON.parse(response.body)
Expand Down
3 changes: 0 additions & 3 deletions lib/chatwork/chatwork_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
module ChatWork
class ChatWorkError < StandardError
def self.from_response(status, body, headers)
# HTTP status 204 don't have body.
return APIError.new(status, "") if status == 204

hash =
begin
JSON.parse(body)
Expand Down
14 changes: 3 additions & 11 deletions lib/chatwork/contacts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChatWork
class Contacts < Entity
install_class_operations :_get
class Contacts
extend EntityMethods

# Get the list of your contacts
#
Expand All @@ -22,15 +22,7 @@ class Contacts < Entity
# }
# ]
def self.get
_get
end

def self.path
"/contacts"
end

def path
"/contacts"
_get("/contacts")
end
end
end
33 changes: 0 additions & 33 deletions lib/chatwork/entity.rb

This file was deleted.

25 changes: 25 additions & 0 deletions lib/chatwork/entity_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module ChatWork
module EntityMethods
private

def _get(path, params = {}, &block)
ChatWork.client.get(path, hash_compact(params), &block)
end

def _post(path, params = {}, &block)
ChatWork.client.post(path, hash_compact(params), &block)
end

def _put(path, params = {}, &block)
ChatWork.client.put(path, hash_compact(params), &block)
end

def _delete(path, params = {}, &block)
ChatWork.client.delete(path, hash_compact(params), &block)
end

def hash_compact(hash)
hash.reject { |_k, v| v.nil? }
end
end
end
14 changes: 3 additions & 11 deletions lib/chatwork/me.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChatWork
class Me < Entity
install_class_operations :_get
class Me
extend EntityMethods

# Get your account information
#
Expand Down Expand Up @@ -31,15 +31,7 @@ class Me < Entity
# "login_mail": "account@example.com"
# }
def self.get
_get
end

def self.path
"/me"
end

def path
"/me"
_get("/me")
end
end
end
34 changes: 29 additions & 5 deletions lib/chatwork/member.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChatWork
class Member < Entity
install_class_operations :_get
class Member
extend EntityMethods

# Get the list of all chat members associated with the specified chat
#
Expand All @@ -24,11 +24,35 @@ class Member < Entity
# }
# ]
def self.get(room_id:)
_get(room_id: room_id)
_get("/rooms/#{room_id}/members")
end

def self.path
"/rooms/%d/members"
# Change associated members of group chat at once
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-members
#
# @param room_id [Integer]
# @param members_admin_ids [Array<Integer>, String] List of user IDs who will be given administrator permission for the group chat.
# At least one user must be specified as an administrator.
# @param members_member_ids [Array<Integer>, String] List of user IDs who will be given member permission for the group chat.
# @param members_readonly_ids [Array<Integer>, String] List of user IDs who will be given read-only permission for the group chat.
#
# @return [Hash]
#
# @example response format
# {
# "admin": [123, 542, 1001],
# "member": [10, 103],
# "readonly": [6, 11]
# }
def self.update_all(room_id:, members_admin_ids:, members_member_ids: nil, members_readonly_ids: nil)
params = {
members_admin_ids: Array(members_admin_ids).join(","),
}
params[:members_member_ids] = Array(members_member_ids).join(",") if members_member_ids
params[:members_readonly_ids] = Array(members_readonly_ids).join(",") if members_readonly_ids

_put("/rooms/#{room_id}/members", params)
end
end
end
106 changes: 97 additions & 9 deletions lib/chatwork/message.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChatWork
class Message < Entity
install_class_operations :_create, :_get
class Message
extend EntityMethods

# Get all messages associated with the specified chat (returns up to 100 entries).
#
Expand Down Expand Up @@ -28,7 +28,7 @@ class Message < Entity
# }
# ]
def self.get(room_id:, force: nil)
params = { room_id: room_id }
params = {}

case force
when 1, true
Expand All @@ -37,7 +37,7 @@ def self.get(room_id:, force: nil)
params[:force] = 0
end

_get(params)
_get("/rooms/#{room_id}/messages", params)
end

# Add new message to the chat
Expand All @@ -54,15 +54,103 @@ def self.get(room_id:, force: nil)
# "message_id": "1234"
# }
def self.create(room_id:, body:)
_create(room_id: room_id, body: body)
_post("/rooms/#{room_id}/messages", body: body)
end

def self.path
"/rooms/%d/messages"
# Mark messages as read
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-read
#
# @param room_id [Integer]
# @param message_id [String]
#
# @return [Hash]
#
# @example response format
# {
# "unread_num": 461,
# "mention_num": 0
# }
def self.read(room_id:, message_id: nil)
_put("/rooms/#{room_id}/messages/read", message_id: message_id)
end

def path
"/rooms/%d/messages"
# Mark messages as unread
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-unread
#
# @param room_id [Integer]
# @param message_id [String]
#
# @return [Hash]
#
# @example response format
# {
# "unread_num": 3,
# "mention_num": 0
# }
def self.unread(room_id:, message_id:)
_put("/rooms/#{room_id}/messages/unread", message_id: message_id)
end

# Get information about the specified message
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages-message_id
#
# @param room_id [Integer]
# @param message_id [String]
#
# @return [Hash]
#
# @example response format
# {
# "message_id": "5",
# "account": {
# "account_id": 123,
# "name": "Bob",
# "avatar_image_url": "https://example.com/ico_avatar.png"
# },
# "body": "Hello Chatwork!",
# "send_time": 1384242850,
# "update_time": 0
# }
def self.find(room_id:, message_id:)
_get("/rooms/#{room_id}/messages/#{message_id}")
end

# Update the specified message
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id
#
# @param room_id [Integer]
# @param message_id [String]
# @param body [String] message body
#
# @return [Hash]
#
# @example response format
# {
# "message_id": "1234"
# }
def self.update(room_id:, message_id:, body:)
_put("/rooms/#{room_id}/messages/#{message_id}", body: body)
end

# Destroy the specified message
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id
#
# @param room_id [Integer]
# @param message_id [String]
#
# @return [Hash]
#
# @example response format
# {
# "message_id": "1234"
# }
def self.destroy(room_id:, message_id:)
_delete("/rooms/#{room_id}/messages/#{message_id}")
end
end
end
Loading

0 comments on commit 8480b5f

Please sign in to comment.