Skip to content

Commit

Permalink
Merge 79a5f22 into a1e221d
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Jan 11, 2018
2 parents a1e221d + 79a5f22 commit 8091fd9
Show file tree
Hide file tree
Showing 34 changed files with 758 additions and 177 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
4 changes: 3 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,11 +22,13 @@ 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"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-its"
spec.add_development_dependency "rspec-parameterized"
spec.add_development_dependency "rubocop", "0.52.1"
spec.add_development_dependency "rubocop-rspec", "1.21.0"
spec.add_development_dependency "webmock"
Expand Down
6 changes: 4 additions & 2 deletions lib/chatwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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 :File, "chatwork/file"
autoload :IncomingRequest, "chatwork/incoming_request"
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
15 changes: 4 additions & 11 deletions lib/chatwork/contacts.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module ChatWork
class Contacts < Entity
install_class_operations :_get
module Contacts
extend EntityMethods

# Get the list of your contacts
#
# @see http://developer.chatwork.com/ja/endpoint_contacts.html#GET-contacts
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @return [Array<Hash>]
#
Expand All @@ -22,15 +23,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.

36 changes: 36 additions & 0 deletions lib/chatwork/entity_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

def boolean_to_integer(value)
case value
when true
1
when false
0
else
value
end
end
end
end
63 changes: 63 additions & 0 deletions lib/chatwork/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module ChatWork
module File
extend EntityMethods

# Get the list of files associated with the specified chat
#
# @param room_id [Integer]
# @param account_id [Integer]
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @return [Array<Hash>]
#
# @example response format
# [
# {
# "file_id": 3,
# "account": {
# "account_id": 123,
# "name": "Bob",
# "avatar_image_url": "https://example.com/ico_avatar.png"
# },
# "message_id": "22",
# "filename": "README.md",
# "filesize": 2232,
# "upload_time": 1384414750
# }
# ]
def self.get(room_id:, account_id:)
_get("/rooms/#{room_id}/files", account_id: account_id)
end

# Get information about the specified file
#
# @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files-file_id
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @param room_id [Integer]
# @param file_id [Integer]
# @param create_download_url [Boolean] whether or not to create a download link.
# If set to true, download like will be created for 30 seconds
#
# @return [Array<Hash>]
#
# @example response format
# {
# "file_id":3,
# "account": {
# "account_id":123,
# "name":"Bob",
# "avatar_image_url": "https://example.com/ico_avatar.png"
# },
# "message_id": "22",
# "filename": "README.md",
# "filesize": 2232,
# "upload_time": 1384414750
# }
def self.find(room_id:, file_id:, create_download_url: nil)
_get("/rooms/#{room_id}/files/#{file_id}", create_download_url: boolean_to_integer(create_download_url))
end
end
end
71 changes: 71 additions & 0 deletions lib/chatwork/incoming_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module ChatWork
module IncomingRequest
extend EntityMethods

# You can get the list of contact approval request you received
#
# (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval)
#
# @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#GET-incoming_requests
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @return [Array<Hash>]
#
# @example response format
# [
# {
# "request_id": 123,
# "account_id": 363,
# "message": "hogehoge",
# "name": "John Smith",
# "chatwork_id": "tarochatworkid",
# "organization_id": 101,
# "organization_name": "Hello Company",
# "department": "Marketing",
# "avatar_image_url": "https://example.com/abc.png"
# }
# ]
def self.get
_get("/incoming_requests")
end

# You can approve a contact approval request you received
#
# @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#PUT-incoming_requests-request_id
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @param request_id [Integer]
#
# @return [Hash]
#
# @example response format
# {
# "account_id": 363,
# "room_id": 1234,
# "name": "John Smith",
# "chatwork_id": "tarochatworkid",
# "organization_id": 101,
# "organization_name": "Hello Company",
# "department": "Marketing",
# "avatar_image_url": "https://example.com/abc.png"
# }
def self.update(request_id:)
_put("/incoming_requests/#{request_id}")
end

# You can decline a contact approval request you received
#
# @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#DELETE-incoming_requests-request_id
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @param request_id [Integer]
def self.destroy(request_id:)
_delete("/incoming_requests/#{request_id}")
end

class << self
alias_method :approve, :update
alias_method :decline, :destroy
end
end
end
15 changes: 4 additions & 11 deletions lib/chatwork/me.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module ChatWork
class Me < Entity
install_class_operations :_get
module Me
extend EntityMethods

# Get your account information
#
# @see http://developer.chatwork.com/ja/endpoint_me.html#GET-me
# @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
#
# @return [Hash]
#
Expand All @@ -31,15 +32,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

0 comments on commit 8091fd9

Please sign in to comment.