oksky-chat-api 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +33 -17
- data/lib/oksky/chat.rb +2 -2
- data/lib/oksky/chat/api/version.rb +1 -1
- data/lib/oksky/chat/client.rb +182 -5
- data/lib/oksky/chat/object.rb +5 -0
- data/lib/oksky/chat/{event → object}/base.rb +1 -1
- data/lib/oksky/chat/{event/message.rb → object/messages_resource.rb} +5 -7
- data/lib/oksky/chat/{event/room.rb → object/rooms_resource.rb} +10 -21
- data/lib/oksky/chat/{event/support.rb → object/supports_resource.rb} +2 -2
- data/lib/oksky/chat/object/unsupport_resource.rb +15 -0
- data/lib/oksky/chat/request.rb +10 -0
- data/lib/oksky/chat/whclient.rb +15 -2
- metadata +8 -12
- data/lib/oksky/chat/client/base.rb +0 -44
- data/lib/oksky/chat/client/message.rb +0 -97
- data/lib/oksky/chat/client/room.rb +0 -78
- data/lib/oksky/chat/client/suggestion.rb +0 -73
- data/lib/oksky/chat/client/support.rb +0 -96
- data/lib/oksky/chat/event.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d03f9cf52487e8394c6f2de928ec8d138d255b3f
|
4
|
+
data.tar.gz: 5a435341868e8bd5cf25a29ebb81943e31f59a5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5062ada537c0203bc28d9933bd1660f8909a8fd9a0a923127172b805383922f3010b5542cdecfe02239ac5569c014a3891347f3dec37b093287c797a409966b2
|
7
|
+
data.tar.gz: c04c6f2a179340a985fa5fc0ec5e669ab8337fea9d2cbad0433a29a6d936de961411869eb8815675d9e6c00175236b81e46118c199ce394b66bfeba83ec638dd
|
data/README.md
CHANGED
@@ -42,11 +42,11 @@ post '/callback' do
|
|
42
42
|
event = webhook_client.parse_events_from(body)
|
43
43
|
case event
|
44
44
|
when Oksky::Chat::Event::Message
|
45
|
-
client = Oksky::Chat::Client
|
45
|
+
client = Oksky::Chat::Client.new { |config|
|
46
46
|
config.access_token = EVN['OKSKY_CHAT_ACCESS_TOKEN']
|
47
47
|
config.endpoint = EVN['OKSKY_CHAT_ENDPOINT']
|
48
48
|
}
|
49
|
-
result = client.
|
49
|
+
result = client.find("messages", event["id"], {}, false)
|
50
50
|
p JSON.parse(result.body) if !result.nil? && result.kind_of?(Net::HTTPResponse) && !result.body.blank?
|
51
51
|
end
|
52
52
|
|
@@ -54,27 +54,43 @@ post '/callback' do
|
|
54
54
|
end
|
55
55
|
```
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
suggestion_client = Oksky::Chat::Client::Suggestion.new(access_token: "38d9b3ed60587820840857dfdf14561b", endpoint: "http://127.0.0.1:3000/rapi/v1")
|
60
|
-
|
61
|
-
suggestions = [{"kind": "text","content": "test","settings": {}}]
|
62
|
-
room_id = 49
|
63
|
-
|
64
|
-
result = suggestion_client.create(suggestion_messages_data: suggestions, room_id: room_id)
|
57
|
+
#### GET Resource Object
|
65
58
|
|
59
|
+
```ruby
|
60
|
+
client = Oksky::Chat::Client.new(
|
61
|
+
access_token: "38d9b3ed60587820840857dfdf14561b",
|
62
|
+
endpoint: "http://127.0.0.1:3000/rapi/v1"
|
63
|
+
)
|
64
|
+
client.find("messages", 1) #=> Oksky::Chat::Object::MessagesResource
|
66
65
|
```
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
71
|
-
|
72
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
#### GET Resource Objects
|
73
68
|
|
74
|
-
|
69
|
+
```ruby
|
70
|
+
client = Oksky::Chat::Client.new(
|
71
|
+
access_token: "38d9b3ed60587820840857dfdf14561b",
|
72
|
+
endpoint: "http://127.0.0.1:3000/rapi/v1"
|
73
|
+
)
|
74
|
+
client.where("messages", {"filter[room_id]": 1}) #=> Array Oksky::Chat::Object::MessagesResource
|
75
|
+
```
|
75
76
|
|
76
|
-
|
77
|
+
#### CREATE Resource Object
|
78
|
+
```ruby
|
79
|
+
client = Oksky::Chat::Client.new(
|
80
|
+
access_token: "38d9b3ed60587820840857dfdf14561b",
|
81
|
+
endpoint: "http://127.0.0.1:3000/rapi/v1"
|
82
|
+
)
|
83
|
+
client.create("messages", {"data"=>{"type"=>"messages", "attributes"=>{"content"=>"test0001", "kind"=>"text", "settings"=>{}, "status"=>"", "info"=>"", "tags"=>[]}, "relationships"=>{"room"=>{"data"=>{"type"=>"rooms", "id"=>"369249"}}}}})
|
84
|
+
```
|
77
85
|
|
86
|
+
#### EDIT Resource Object
|
87
|
+
```ruby
|
88
|
+
client = Oksky::Chat::Client.new(
|
89
|
+
access_token: "38d9b3ed60587820840857dfdf14561b",
|
90
|
+
endpoint: "http://127.0.0.1:3000/rapi/v1"
|
91
|
+
)
|
92
|
+
client.edit("messages", 1, {"data"=>{"type"=>"messages", "attributes"=>{"content"=>"test0001", "kind"=>"text", "settings"=>{}, "status"=>"", "info"=>"", "tags"=>[]}, "relationships"=>{"room"=>{"data"=>{"type"=>"rooms", "id"=>"369249"}}}}})
|
93
|
+
```
|
78
94
|
|
79
95
|
## License
|
80
96
|
|
data/lib/oksky/chat.rb
CHANGED
@@ -3,6 +3,6 @@ require 'oksky/chat/api/errors'
|
|
3
3
|
require 'oksky/chat/api/version'
|
4
4
|
require 'oksky/chat/request'
|
5
5
|
require 'oksky/chat/httpclient'
|
6
|
-
require 'oksky/chat/event'
|
7
6
|
require 'oksky/chat/client'
|
8
|
-
require 'oksky/chat/whclient'
|
7
|
+
require 'oksky/chat/whclient'
|
8
|
+
require 'oksky/chat/object'
|
data/lib/oksky/chat/client.rb
CHANGED
@@ -1,5 +1,182 @@
|
|
1
|
-
require 'oksky/chat/client/base'
|
2
|
-
require 'oksky/chat/client/message'
|
3
|
-
require 'oksky/chat/client/suggestion'
|
4
|
-
require 'oksky/chat/client/room'
|
5
|
-
require 'oksky/chat/client/support'
|
1
|
+
# require 'oksky/chat/client/base'
|
2
|
+
# require 'oksky/chat/client/message'
|
3
|
+
# require 'oksky/chat/client/suggestion'
|
4
|
+
# require 'oksky/chat/client/room'
|
5
|
+
# require 'oksky/chat/client/support'
|
6
|
+
require 'oksky/chat/request'
|
7
|
+
require 'oksky/chat/api/errors'
|
8
|
+
require 'base64'
|
9
|
+
require 'net/http'
|
10
|
+
require 'openssl'
|
11
|
+
|
12
|
+
module Oksky
|
13
|
+
module Chat
|
14
|
+
class Client
|
15
|
+
|
16
|
+
# @return [String]
|
17
|
+
attr_accessor :access_token, :endpoint
|
18
|
+
|
19
|
+
# @return [Object]
|
20
|
+
attr_accessor :httpclient
|
21
|
+
|
22
|
+
# Initialize a new Bot Client.
|
23
|
+
#
|
24
|
+
# @param options [Hash]
|
25
|
+
#
|
26
|
+
# @return [Oksky::Chat::Message::Client]
|
27
|
+
def initialize(options = {})
|
28
|
+
options.each do |key, value|
|
29
|
+
instance_variable_set("@#{key}", value)
|
30
|
+
end
|
31
|
+
yield(self) if block_given?
|
32
|
+
end
|
33
|
+
|
34
|
+
def httpclient
|
35
|
+
@httpclient ||= Oksky::Chat::HTTPClient.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def endpoint
|
39
|
+
@endpoint ||= Oksky::Chat::API::DEFAULT_ENDPOINT
|
40
|
+
end
|
41
|
+
|
42
|
+
# @return [Hash]
|
43
|
+
def credentials
|
44
|
+
{
|
45
|
+
"X-Access-Token" => access_token
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def credentials?
|
50
|
+
credentials.values.all?
|
51
|
+
end
|
52
|
+
|
53
|
+
# Push messages to OK SKY Chat server and to user.
|
54
|
+
#
|
55
|
+
# @param object_type [String] User's identifiers
|
56
|
+
# @param payload [Hash or Array]
|
57
|
+
#
|
58
|
+
# @return [Net::HTTPResponse]
|
59
|
+
def create(object_type, payload)
|
60
|
+
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
61
|
+
|
62
|
+
# messages = [messages] if messages.is_a?(Hash)
|
63
|
+
|
64
|
+
request = Request.new do |config|
|
65
|
+
config.httpclient = httpclient
|
66
|
+
config.endpoint = endpoint
|
67
|
+
config.endpoint_path = "/#{object_type}"
|
68
|
+
config.credentials = credentials
|
69
|
+
config.payload = payload
|
70
|
+
end
|
71
|
+
|
72
|
+
request.post
|
73
|
+
end
|
74
|
+
|
75
|
+
# Push messages to OK SKY Chat server and to user.
|
76
|
+
#
|
77
|
+
# @param object_type [String] User's identifiers
|
78
|
+
# @param object_id [String] Object's identifier
|
79
|
+
# @param payload [Hash or Array]
|
80
|
+
#
|
81
|
+
# @return [Net::HTTPResponse]
|
82
|
+
def edit(object_type, object_id, payload)
|
83
|
+
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
84
|
+
|
85
|
+
# messages = [messages] if messages.is_a?(Hash)
|
86
|
+
|
87
|
+
request = Request.new do |config|
|
88
|
+
config.httpclient = httpclient
|
89
|
+
config.endpoint = endpoint
|
90
|
+
config.endpoint_path = "/#{object_type}/#{object_id}"
|
91
|
+
config.credentials = credentials
|
92
|
+
config.payload = payload
|
93
|
+
end
|
94
|
+
|
95
|
+
request.put
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# Get object_type content.
|
100
|
+
#
|
101
|
+
# @param object_type [String] Object type
|
102
|
+
# @param object_id [String] Object's identifier
|
103
|
+
#
|
104
|
+
# @return [Oksky::Chat::Object::{object_type}]
|
105
|
+
def find(object_type, object_id, option = {}, is_parse = true)
|
106
|
+
endpoint_path = "/#{object_type}/#{object_id}"
|
107
|
+
result = get(endpoint_path, option)
|
108
|
+
|
109
|
+
if is_parse
|
110
|
+
json = JSON.parse(result.body)
|
111
|
+
begin
|
112
|
+
concrete_class(json["data"]).new(json["data"])
|
113
|
+
rescue => ex
|
114
|
+
p ex
|
115
|
+
return json
|
116
|
+
end
|
117
|
+
else
|
118
|
+
return result
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Get some object_type content.
|
123
|
+
#
|
124
|
+
# @param object_type [String] object_type
|
125
|
+
# @param option [Hash] get params hash
|
126
|
+
# @param is_parse [Boolean] return Object
|
127
|
+
#
|
128
|
+
# @return [Oksky::Chat::Object::{object_type}]
|
129
|
+
def where(object_type, option = {}, is_parse = true)
|
130
|
+
endpoint_path = "/#{object_type}"
|
131
|
+
result = get(endpoint_path, option)
|
132
|
+
|
133
|
+
if is_parse
|
134
|
+
json = JSON.parse(result.body)
|
135
|
+
if json.has_key?("data") && !json["data"].empty? && json["data"].kind_of?(Array)
|
136
|
+
json["data"].map{|d|
|
137
|
+
concrete_class(d).new(d)
|
138
|
+
}
|
139
|
+
else
|
140
|
+
return []
|
141
|
+
end
|
142
|
+
else
|
143
|
+
return result
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# Fetch data, get content of specified URL.
|
148
|
+
#
|
149
|
+
# @param endpoint_path [String]
|
150
|
+
#
|
151
|
+
# @return [Net::HTTPResponse]
|
152
|
+
def get(endpoint_path, option = {})
|
153
|
+
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
154
|
+
|
155
|
+
request = Request.new do |config|
|
156
|
+
config.httpclient = httpclient
|
157
|
+
config.endpoint = endpoint
|
158
|
+
config.endpoint_path = endpoint_path
|
159
|
+
config.credentials = credentials
|
160
|
+
config.payload = option
|
161
|
+
end
|
162
|
+
|
163
|
+
request.get
|
164
|
+
end
|
165
|
+
|
166
|
+
private
|
167
|
+
# class object return, get content of specified URL.
|
168
|
+
#
|
169
|
+
# @param data [Hash]
|
170
|
+
#
|
171
|
+
# @return [Oksky::Chat::Object::]
|
172
|
+
def concrete_class(data)
|
173
|
+
begin
|
174
|
+
Oksky::Chat::Object.const_get("#{data["type"].capitalize}Resource")
|
175
|
+
rescue NameError => e
|
176
|
+
Oksky::Chat::Object::UnsupportResource
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
@@ -1,9 +1,7 @@
|
|
1
|
-
# require 'oksky/chat/client/room'
|
2
|
-
|
3
1
|
module Oksky
|
4
2
|
module Chat
|
5
|
-
module
|
6
|
-
module
|
3
|
+
module Object
|
4
|
+
module MessagesResourceKind
|
7
5
|
Text = "text"
|
8
6
|
Link = "link"
|
9
7
|
Image = "image"
|
@@ -22,7 +20,7 @@ module Oksky
|
|
22
20
|
Unsupport = 'unsupport'
|
23
21
|
end
|
24
22
|
|
25
|
-
class
|
23
|
+
class MessagesResource < Base
|
26
24
|
|
27
25
|
# @return [String<Oksky::Chat::Event::Message>]
|
28
26
|
def content
|
@@ -32,9 +30,9 @@ module Oksky
|
|
32
30
|
# @return [String<Oksky::Chat::Event::MessageKind::>]
|
33
31
|
def kind
|
34
32
|
begin
|
35
|
-
Oksky::Chat::
|
33
|
+
Oksky::Chat::Object::MessagesResourceKind.const_get(content_value("kind").split("_").map{|w| w[0] = w[0].capitalize; w}.join)
|
36
34
|
rescue NameError => e
|
37
|
-
Oksky::Chat::
|
35
|
+
Oksky::Chat::Object::MessagesResourceKind::Unsupport
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
@@ -1,26 +1,15 @@
|
|
1
1
|
module Oksky
|
2
2
|
module Chat
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
Location = "location"
|
11
|
-
Sticker = "sticker"
|
12
|
-
ActionLink = "action_link"
|
13
|
-
Maltiple = "maltiple"
|
14
|
-
RichContent = "rich_content"
|
15
|
-
Stamp = "stamp"
|
16
|
-
Contact = "contact"
|
17
|
-
Form = "form"
|
18
|
-
Item = "item"
|
19
|
-
File = "file"
|
20
|
-
Unsupport = 'unsupport'
|
3
|
+
module Object
|
4
|
+
module RoomsResourceKind
|
5
|
+
MyRoom = "my_room"
|
6
|
+
DirectRoom = "direct_room"
|
7
|
+
GroupRoom = "group_room"
|
8
|
+
SupportRoom = "support_room"
|
9
|
+
Unsupport = 'unsupport'
|
21
10
|
end
|
22
11
|
|
23
|
-
class
|
12
|
+
class RoomsResource < Base
|
24
13
|
|
25
14
|
# @return [String<Oksky::Chat::Event::Room>]
|
26
15
|
def name
|
@@ -30,9 +19,9 @@ module Oksky
|
|
30
19
|
# @return [String<Oksky::Chat::Event::RoomKind::>]
|
31
20
|
def kind
|
32
21
|
begin
|
33
|
-
Oksky::Chat::Event::
|
22
|
+
Oksky::Chat::Event::RoomsResourceKind.const_get(content_value("kind").split("_").map{|w| w[0] = w[0].capitalize; w}.join)
|
34
23
|
rescue NameError => e
|
35
|
-
Oksky::Chat::Event::
|
24
|
+
Oksky::Chat::Event::RoomsResourceKind::Unsupport
|
36
25
|
end
|
37
26
|
end
|
38
27
|
|
data/lib/oksky/chat/request.rb
CHANGED
@@ -50,6 +50,16 @@ module Oksky
|
|
50
50
|
httpclient.post(endpoint + endpoint_path, payload.to_json, header)
|
51
51
|
end
|
52
52
|
|
53
|
+
# Put content of specified URL.
|
54
|
+
#
|
55
|
+
# @raise [ArgumentError]
|
56
|
+
#
|
57
|
+
# @return [Net::HTTPResponse]
|
58
|
+
def put
|
59
|
+
assert_for_posting_message
|
60
|
+
httpclient.put(endpoint + endpoint_path, payload.to_json, header)
|
61
|
+
end
|
62
|
+
|
53
63
|
def assert_for_getting_message
|
54
64
|
raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.is_a?(String)
|
55
65
|
end
|
data/lib/oksky/chat/whclient.rb
CHANGED
@@ -11,12 +11,25 @@ module Oksky
|
|
11
11
|
json = JSON.parse(request_body)
|
12
12
|
|
13
13
|
begin
|
14
|
-
klass = Oksky::Chat::
|
14
|
+
klass = Oksky::Chat::Object.const_get("#{object_type(json['object_name']).capitalize}Resource")
|
15
15
|
klass.new(json['object'])
|
16
16
|
rescue NameError => e
|
17
|
-
Oksky::Chat::
|
17
|
+
Oksky::Chat::Object::UnsupportResource.new(json['object'])
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def object_type(object_name)
|
23
|
+
case object_name
|
24
|
+
when "Message"
|
25
|
+
"messages"
|
26
|
+
when "Room"
|
27
|
+
"rooms"
|
28
|
+
when "User"
|
29
|
+
"users"
|
30
|
+
else
|
31
|
+
end
|
32
|
+
end
|
20
33
|
end
|
21
34
|
end
|
22
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oksky-chat-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- SOLAIRO, INC.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,17 +108,13 @@ files:
|
|
108
108
|
- lib/oksky/chat/api/errors.rb
|
109
109
|
- lib/oksky/chat/api/version.rb
|
110
110
|
- lib/oksky/chat/client.rb
|
111
|
-
- lib/oksky/chat/client/base.rb
|
112
|
-
- lib/oksky/chat/client/message.rb
|
113
|
-
- lib/oksky/chat/client/room.rb
|
114
|
-
- lib/oksky/chat/client/suggestion.rb
|
115
|
-
- lib/oksky/chat/client/support.rb
|
116
|
-
- lib/oksky/chat/event.rb
|
117
|
-
- lib/oksky/chat/event/base.rb
|
118
|
-
- lib/oksky/chat/event/message.rb
|
119
|
-
- lib/oksky/chat/event/room.rb
|
120
|
-
- lib/oksky/chat/event/support.rb
|
121
111
|
- lib/oksky/chat/httpclient.rb
|
112
|
+
- lib/oksky/chat/object.rb
|
113
|
+
- lib/oksky/chat/object/base.rb
|
114
|
+
- lib/oksky/chat/object/messages_resource.rb
|
115
|
+
- lib/oksky/chat/object/rooms_resource.rb
|
116
|
+
- lib/oksky/chat/object/supports_resource.rb
|
117
|
+
- lib/oksky/chat/object/unsupport_resource.rb
|
122
118
|
- lib/oksky/chat/request.rb
|
123
119
|
- lib/oksky/chat/whclient.rb
|
124
120
|
- oksky-chat-api.gemspec
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module Oksky
|
2
|
-
module Chat
|
3
|
-
module Client
|
4
|
-
class Base
|
5
|
-
# @return [String]
|
6
|
-
attr_accessor :access_token, :endpoint
|
7
|
-
|
8
|
-
# @return [Object]
|
9
|
-
attr_accessor :httpclient
|
10
|
-
|
11
|
-
# Initialize a new Bot Client.
|
12
|
-
#
|
13
|
-
# @param options [Hash]
|
14
|
-
#
|
15
|
-
# @return [Oksky::Chat::Message::Client]
|
16
|
-
def initialize(options = {})
|
17
|
-
options.each do |key, value|
|
18
|
-
instance_variable_set("@#{key}", value)
|
19
|
-
end
|
20
|
-
yield(self) if block_given?
|
21
|
-
end
|
22
|
-
|
23
|
-
def httpclient
|
24
|
-
@httpclient ||= Oksky::Chat::HTTPClient.new
|
25
|
-
end
|
26
|
-
|
27
|
-
def endpoint
|
28
|
-
@endpoint ||= Oksky::Chat::API::DEFAULT_ENDPOINT
|
29
|
-
end
|
30
|
-
|
31
|
-
# @return [Hash]
|
32
|
-
def credentials
|
33
|
-
{
|
34
|
-
"X-Access-Token" => access_token
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
def credentials?
|
39
|
-
credentials.values.all?
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'oksky/chat/request'
|
2
|
-
require 'oksky/chat/api/errors'
|
3
|
-
require 'base64'
|
4
|
-
require 'net/http'
|
5
|
-
require 'openssl'
|
6
|
-
|
7
|
-
module Oksky
|
8
|
-
module Chat
|
9
|
-
module Client
|
10
|
-
class Message < Base
|
11
|
-
|
12
|
-
# Push messages to OK SKY Chat server and to user.
|
13
|
-
#
|
14
|
-
# @param user_id [String] User's identifiers
|
15
|
-
# @param messages [Hash or Array]
|
16
|
-
#
|
17
|
-
# @return [Net::HTTPResponse]
|
18
|
-
#
|
19
|
-
# Example:
|
20
|
-
# md = {"content":"test0001","kind":"text","settings":{},"status":"","info":"","tags":[]}
|
21
|
-
# room_id = 3200
|
22
|
-
# messages_data = {
|
23
|
-
# "data":{
|
24
|
-
# "type":"messages",
|
25
|
-
# "attributes":md, # {"content"=>nil, "kind"=>"text", "settings"=>{}}
|
26
|
-
# "relationships":{
|
27
|
-
# "room":{
|
28
|
-
# "data":{
|
29
|
-
# "type":"rooms",
|
30
|
-
# "id": "#{room_id}"
|
31
|
-
# }
|
32
|
-
# }
|
33
|
-
# }
|
34
|
-
# }
|
35
|
-
# }
|
36
|
-
#
|
37
|
-
# message_client = Oksky::Chat::Client::Message.new { |config|
|
38
|
-
# config.access_token = "hogehoge"
|
39
|
-
# config.endpoint = "https://hogehoge.ok-sky.com/rapi/v1"
|
40
|
-
# }
|
41
|
-
# message_client.push_message(messages_data)
|
42
|
-
def push_message(messages)
|
43
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
44
|
-
|
45
|
-
# messages = [messages] if messages.is_a?(Hash)
|
46
|
-
|
47
|
-
request = Request.new do |config|
|
48
|
-
config.httpclient = httpclient
|
49
|
-
config.endpoint = endpoint
|
50
|
-
config.endpoint_path = '/messages'
|
51
|
-
config.credentials = credentials
|
52
|
-
config.payload = messages
|
53
|
-
end
|
54
|
-
|
55
|
-
request.post
|
56
|
-
end
|
57
|
-
|
58
|
-
# Get message content.
|
59
|
-
#
|
60
|
-
# @param identifier [String] Message's identifier
|
61
|
-
#
|
62
|
-
# @return [Oksky::Chat::Event::Message]
|
63
|
-
def get_message_content(message_id, option = {}, is_parse = true)
|
64
|
-
endpoint_path = "/messages/#{message_id}"
|
65
|
-
result = get(endpoint_path, option)
|
66
|
-
|
67
|
-
if is_parse
|
68
|
-
json = JSON.parse(result.body)
|
69
|
-
Oksky::Chat::Event::Message.new(json["data"])
|
70
|
-
else
|
71
|
-
return result
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# Fetch data, get content of specified URL.
|
76
|
-
#
|
77
|
-
# @param endpoint_path [String]
|
78
|
-
#
|
79
|
-
# @return [Net::HTTPResponse]
|
80
|
-
def get(endpoint_path, option = {})
|
81
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
82
|
-
|
83
|
-
request = Request.new do |config|
|
84
|
-
config.httpclient = httpclient
|
85
|
-
config.endpoint = endpoint
|
86
|
-
config.endpoint_path = endpoint_path
|
87
|
-
config.credentials = credentials
|
88
|
-
config.payload = option
|
89
|
-
end
|
90
|
-
|
91
|
-
request.get
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'oksky/chat/request'
|
2
|
-
require 'oksky/chat/api/errors'
|
3
|
-
require 'base64'
|
4
|
-
require 'net/http'
|
5
|
-
require 'openssl'
|
6
|
-
|
7
|
-
module Oksky
|
8
|
-
module Chat
|
9
|
-
module Client
|
10
|
-
class Room < Base
|
11
|
-
|
12
|
-
# Push rooms to OK SKY Chat server and to user.
|
13
|
-
#
|
14
|
-
# @param user_id [String] User's identifiers
|
15
|
-
# @param rooms [Hash or Array]
|
16
|
-
#
|
17
|
-
# @return [Net::HTTPResponse]
|
18
|
-
def push_room(rooms)
|
19
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
20
|
-
|
21
|
-
rooms = [rooms] if rooms.is_a?(Hash)
|
22
|
-
|
23
|
-
request = Request.new do |config|
|
24
|
-
config.httpclient = httpclient
|
25
|
-
config.endpoint = endpoint
|
26
|
-
config.endpoint_path = '/rooms'
|
27
|
-
config.credentials = credentials
|
28
|
-
config.rooms = rooms
|
29
|
-
end
|
30
|
-
|
31
|
-
request.post
|
32
|
-
end
|
33
|
-
|
34
|
-
# Get room content.
|
35
|
-
#
|
36
|
-
# @param identifier [String] Message's identifier
|
37
|
-
#
|
38
|
-
# @return [Oksky::Chat::Event::Message]
|
39
|
-
def get_room_content(room_id, option = {}, is_parse = true)
|
40
|
-
endpoint_path = "/rooms/#{room_id}"
|
41
|
-
result = get(endpoint_path, option)
|
42
|
-
|
43
|
-
if is_parse
|
44
|
-
json = JSON.parse(result.body)
|
45
|
-
Oksky::Chat::Event::Room.new(json["data"])
|
46
|
-
else
|
47
|
-
return result
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Fetch data, get content of specified URL.
|
52
|
-
#
|
53
|
-
# @param endpoint_path [String]
|
54
|
-
#
|
55
|
-
# @return [Net::HTTPResponse]
|
56
|
-
def get(endpoint_path, option = {})
|
57
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
58
|
-
|
59
|
-
unless option.empty?
|
60
|
-
params = URI.encode_www_form(option)
|
61
|
-
endpoint_path = "#{endpoint_path}?#{params}"
|
62
|
-
end
|
63
|
-
|
64
|
-
request = Request.new do |config|
|
65
|
-
config.httpclient = httpclient
|
66
|
-
config.endpoint = endpoint
|
67
|
-
config.endpoint_path = endpoint_path
|
68
|
-
config.credentials = credentials
|
69
|
-
config.payload = option
|
70
|
-
end
|
71
|
-
|
72
|
-
request.get
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'oksky/chat/request'
|
2
|
-
require 'oksky/chat/api/errors'
|
3
|
-
require 'base64'
|
4
|
-
require 'net/http'
|
5
|
-
require 'openssl'
|
6
|
-
|
7
|
-
module Oksky
|
8
|
-
module Chat
|
9
|
-
module Client
|
10
|
-
class Suggestion < Base
|
11
|
-
|
12
|
-
# Push suggestion to OK SKY Chat server
|
13
|
-
#
|
14
|
-
# @param user_id [String] User's identifiers
|
15
|
-
# @param messages [Hash or Array]
|
16
|
-
#
|
17
|
-
# @return [Net::HTTPResponse]
|
18
|
-
def create(suggestion_messages_data:, room_id:)
|
19
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
20
|
-
|
21
|
-
suggestion_payload = {
|
22
|
-
"data": {
|
23
|
-
"type": "suggestions",
|
24
|
-
"attributes": {
|
25
|
-
"suggestion_messages_data": suggestion_messages_data
|
26
|
-
},
|
27
|
-
"relationships": {
|
28
|
-
"room": {
|
29
|
-
"data": {
|
30
|
-
"type": "rooms",
|
31
|
-
"id": room_id
|
32
|
-
}
|
33
|
-
}
|
34
|
-
}
|
35
|
-
}
|
36
|
-
}
|
37
|
-
p suggestion_payload
|
38
|
-
|
39
|
-
request = Request.new do |config|
|
40
|
-
config.httpclient = httpclient
|
41
|
-
config.endpoint = endpoint
|
42
|
-
config.endpoint_path = '/suggestions'
|
43
|
-
config.credentials = credentials
|
44
|
-
config.payload = suggestion_payload
|
45
|
-
end
|
46
|
-
|
47
|
-
request.post
|
48
|
-
end
|
49
|
-
|
50
|
-
# Fetch data, get content of specified URL.
|
51
|
-
#
|
52
|
-
# @param endpoint_path [String]
|
53
|
-
#
|
54
|
-
# @return [Net::HTTPResponse]
|
55
|
-
def get(endpoint_path, option = {})
|
56
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
57
|
-
|
58
|
-
request = Request.new do |config|
|
59
|
-
config.httpclient = httpclient
|
60
|
-
config.endpoint = endpoint
|
61
|
-
config.endpoint_path = endpoint_path
|
62
|
-
config.credentials = credentials
|
63
|
-
config.payload = option
|
64
|
-
end
|
65
|
-
|
66
|
-
request.get
|
67
|
-
end
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
require 'oksky/chat/request'
|
2
|
-
require 'oksky/chat/api/errors'
|
3
|
-
require 'base64'
|
4
|
-
require 'net/http'
|
5
|
-
require 'openssl'
|
6
|
-
|
7
|
-
module Oksky
|
8
|
-
module Chat
|
9
|
-
module Client
|
10
|
-
class Support < Base
|
11
|
-
|
12
|
-
# Push supports to OK SKY Chat server and to user.
|
13
|
-
#
|
14
|
-
# @param user_id [String] User's identifiers
|
15
|
-
# @param supports [Hash or Array]
|
16
|
-
#
|
17
|
-
# @return [Net::HTTPResponse]
|
18
|
-
def push_support(supports)
|
19
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
20
|
-
|
21
|
-
supports = [supports] if supports.is_a?(Hash)
|
22
|
-
|
23
|
-
request = Request.new do |config|
|
24
|
-
config.httpclient = httpclient
|
25
|
-
config.endpoint = endpoint
|
26
|
-
config.endpoint_path = '/supports'
|
27
|
-
config.credentials = credentials
|
28
|
-
config.supports = supports
|
29
|
-
end
|
30
|
-
|
31
|
-
request.post
|
32
|
-
end
|
33
|
-
|
34
|
-
# Get supports content.
|
35
|
-
#
|
36
|
-
# @param identifier [String] Support's identifier
|
37
|
-
#
|
38
|
-
# @return Array [Oksky::Chat::Event::Message]
|
39
|
-
def get_supports_content(option = {}, is_parse = true)
|
40
|
-
endpoint_path = "/supports"
|
41
|
-
result = get(endpoint_path, option)
|
42
|
-
|
43
|
-
if is_parse
|
44
|
-
json = JSON.parse(result.body)
|
45
|
-
if json.has_key?("data") && !json["data"].empty?
|
46
|
-
json["data"].map{|d|
|
47
|
-
Oksky::Chat::Event::Support.new(d)
|
48
|
-
}
|
49
|
-
else
|
50
|
-
return []
|
51
|
-
end
|
52
|
-
else
|
53
|
-
return result
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Get support content.
|
58
|
-
#
|
59
|
-
# @param identifier [String] Message's identifier
|
60
|
-
#
|
61
|
-
# @return [Oksky::Chat::Event::Message]
|
62
|
-
def get_support_content(support_id, option = {}, is_parse = true)
|
63
|
-
endpoint_path = "/supports/#{support_id}"
|
64
|
-
result = get(endpoint_path, option)
|
65
|
-
|
66
|
-
if is_parse
|
67
|
-
json = JSON.parse(result.body)
|
68
|
-
Oksky::Chat::Event::Support.new(json["data"])
|
69
|
-
else
|
70
|
-
return result
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# Fetch data, get content of specified URL.
|
75
|
-
#
|
76
|
-
# @param endpoint_path [String]
|
77
|
-
#
|
78
|
-
# @return [Net::HTTPResponse]
|
79
|
-
def get(endpoint_path, option = {})
|
80
|
-
raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
|
81
|
-
|
82
|
-
request = Request.new do |config|
|
83
|
-
config.httpclient = httpclient
|
84
|
-
config.endpoint = endpoint
|
85
|
-
config.endpoint_path = endpoint_path
|
86
|
-
config.credentials = credentials
|
87
|
-
config.payload = option
|
88
|
-
end
|
89
|
-
|
90
|
-
request.get
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
data/lib/oksky/chat/event.rb
DELETED