Class: Oksky::Chat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/oksky/chat/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Oksky::Chat::Message::Client

Initialize a new Bot Client.

Parameters:

  • options (Hash) (defaults to: {})

Yields:

  • (_self)

Yield Parameters:



27
28
29
30
31
32
# File 'lib/oksky/chat/client.rb', line 27

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#access_tokenObject

@return [String]



17
18
19
# File 'lib/oksky/chat/client.rb', line 17

def access_token
  @access_token
end

#endpointObject

@return [String]



17
18
19
# File 'lib/oksky/chat/client.rb', line 17

def endpoint
  @endpoint
end

#httpclientObject

Returns:



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.

Parameters:

  • object_type (String)

    User’s identifiers

  • payload (Hash or Array)

Returns:

  • (Net::HTTPResponse)

Raises:



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

#credentialsHash

Returns:

  • (Hash)


43
44
45
46
47
# File 'lib/oksky/chat/client.rb', line 43

def credentials
  {
      "X-Access-Token" => access_token
  }
end

#credentials?Boolean

Returns:

  • (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.

Parameters:

  • object_type (String)

    User’s identifiers

  • object_id (String)

    Object’s identifier

  • payload (Hash or Array)

Returns:

  • (Net::HTTPResponse)

Raises:



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.

Parameters:

  • object_type (String)

    Object type

  • object_id (String)

    Object’s identifier

Returns:



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.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



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.

Parameters:

  • object_type (String)

    object_type

  • option (Hash) (defaults to: {})

    get params hash

  • is_parse (Boolean) (defaults to: true)

    return Object

Returns:



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