Class: Oksky::Chat::Client
- Inherits:
-
Object
- Object
- Oksky::Chat::Client
- Defined in:
- lib/oksky/chat/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
@return [String].
-
#endpoint ⇒ Object
@return [String].
- #httpclient ⇒ Object
Instance Method Summary collapse
-
#create(object_type, payload) ⇒ Net::HTTPResponse
Push messages to OK SKY Chat server and to user.
- #credentials ⇒ Hash
- #credentials? ⇒ Boolean
-
#edit(object_type, object_id, payload) ⇒ Net::HTTPResponse
Push messages to OK SKY Chat server and to user.
-
#find(object_type, object_id, option = {}, is_parse = true) ⇒ Oksky::Chat::Object::{object_type}
Get object_type content.
-
#get(endpoint_path, option = {}) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
-
#initialize(options = {}) {|_self| ... } ⇒ Oksky::Chat::Message::Client
constructor
Initialize a new Bot Client.
-
#where(object_type, option = {}, is_parse = true) ⇒ Oksky::Chat::Object::{object_type}
Get some object_type content.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Oksky::Chat::Message::Client
Initialize a new Bot Client.
27 28 29 30 31 32 |
# File 'lib/oksky/chat/client.rb', line 27 def initialize( = {}) .each do |key, value| instance_variable_set("@#{key}", value) end yield(self) if block_given? end |
Instance Attribute Details
#access_token ⇒ Object
@return [String]
17 18 19 |
# File 'lib/oksky/chat/client.rb', line 17 def access_token @access_token end |
#endpoint ⇒ Object
@return [String]
17 18 19 |
# File 'lib/oksky/chat/client.rb', line 17 def endpoint @endpoint end |
#httpclient ⇒ Object
20 21 22 |
# File 'lib/oksky/chat/client.rb', line 20 def httpclient @httpclient end |
Instance Method Details
#create(object_type, payload) ⇒ Net::HTTPResponse
Push messages to OK SKY Chat server and to user.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/oksky/chat/client.rb', line 59 def create(object_type, payload) raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? # messages = [messages] if messages.is_a?(Hash) request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = "/#{object_type}" config.credentials = credentials config.payload = payload end request.post end |
#credentials ⇒ Hash
43 44 45 46 47 |
# File 'lib/oksky/chat/client.rb', line 43 def credentials { "X-Access-Token" => access_token } end |
#credentials? ⇒ Boolean
49 50 51 |
# File 'lib/oksky/chat/client.rb', line 49 def credentials? credentials.values.all? end |
#edit(object_type, object_id, payload) ⇒ Net::HTTPResponse
Push messages to OK SKY Chat server and to user.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/oksky/chat/client.rb', line 82 def edit(object_type, object_id, payload) raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? # messages = [messages] if messages.is_a?(Hash) request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = "/#{object_type}/#{object_id}" config.credentials = credentials config.payload = payload end request.put end |
#find(object_type, object_id, option = {}, is_parse = true) ⇒ Oksky::Chat::Object::{object_type}
Get object_type content.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/oksky/chat/client.rb', line 105 def find(object_type, object_id, option = {}, is_parse = true) endpoint_path = "/#{object_type}/#{object_id}" result = get(endpoint_path, option) if is_parse json = JSON.parse(result.body) begin concrete_class(json["data"]).new(json["data"]) rescue => ex p ex return json end else return result end end |
#get(endpoint_path, option = {}) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/oksky/chat/client.rb', line 152 def get(endpoint_path, option = {}) raise Oksky::Chat::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = endpoint_path config.credentials = credentials config.payload = option end request.get end |
#where(object_type, option = {}, is_parse = true) ⇒ Oksky::Chat::Object::{object_type}
Get some object_type content.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/oksky/chat/client.rb', line 129 def where(object_type, option = {}, is_parse = true) endpoint_path = "/#{object_type}" result = get(endpoint_path, option) if is_parse json = JSON.parse(result.body) if json.has_key?("data") && !json["data"].empty? && json["data"].kind_of?(Array) json["data"].map{|d| concrete_class(d).new(d) } else return [] end else return result end end |