Class: SlackChatter::Client

Inherits:
Object
  • Object
show all
Includes:
Api::Namespaces
Defined in:
lib/slack_chatter/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::Namespaces

#api, #auth, #channels, #chat, #emoji, #files, #groups, #im, #oauth, #rtm, #search, #stars, #team, #users

Constructor Details

#initialize(access_token, opts = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slack_chatter/client.rb', line 15

def initialize(access_token, opts={})
  opts = opts.with_indifferent_access.reject{|_,v| v.nil?}
  opts.reverse_merge!(default_slack_configuration)

  @access_token   =     access_token
  @username       =     opts[:username]
  @icon_url       =     opts[:icon_url]
  @uri_scheme     =     opts[:uri_scheme]
  @uri_host       =     opts[:uri_host]
  @uri_base_path  =     opts[:uri_base_path]
  @uri_port       =     opts[:uri_port]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



13
14
15
# File 'lib/slack_chatter/client.rb', line 13

def access_token
  @access_token
end

#icon_urlObject (readonly)

Returns the value of attribute icon_url.



13
14
15
# File 'lib/slack_chatter/client.rb', line 13

def icon_url
  @icon_url
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



13
14
15
# File 'lib/slack_chatter/client.rb', line 13

def namespaces
  @namespaces
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/slack_chatter/client.rb', line 13

def username
  @username
end

Instance Method Details

#api_uri_rootObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slack_chatter/client.rb', line 46

def api_uri_root()
  # Changed this from URI.join because scheme became pointless. It could not
  # override the scheme set in the host and the scheme was required to be set
  # in @uri_host or else URI.join throws an error
  URI.parse('').tap do |uri|
    uri.scheme = @uri_scheme
    uri.host   = @uri_host.gsub(/https?:\/\//, '') # strip the scheme if it is part of the hostname
    uri.path   = [@uri_base_path,nil].join('/')
    uri.port   = @uri_port unless @uri_port.blank?
  end.to_s
end

#default_slack_configurationObject



28
29
30
31
32
33
34
# File 'lib/slack_chatter/client.rb', line 28

def default_slack_configuration
  {
    uri_scheme:     "https",
    uri_host:       "slack.com",
    uri_base_path:  "/api"
  }
end

#get(path, query_params = {}, opts = {}) ⇒ Object

Perform an HTTP GET request



37
38
39
# File 'lib/slack_chatter/client.rb', line 37

def get(path, query_params={}, opts={})
  request(:get, path, query_params, opts)
end

#post(path, query_params = {}, opts = {}) ⇒ Object

Perform an HTTP POST request



42
43
44
# File 'lib/slack_chatter/client.rb', line 42

def post(path, query_params={}, opts={})
  request(:post, path, query_params, opts)
end

#request(method, path, query_params = {}, opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/slack_chatter/client.rb', line 58

def request(method, path, query_params={}, opts={})
  query_params = query_params.merge({token: @access_token})
  uri = full_url(path, query_params)

  SlackChatter.logger.info("SlackChatter::Client   #{method}-ing #{uri} with params #{opts.merge(headers: headers)}")
  response = HTTMultiParty.send(method, uri, opts.merge(headers: headers))

  if !response.code =~ /20[0-9]/
    SlackChatter.logger.fatal("SlackChatter::Client   #{response.code} #{method}-ing #{uri.to_s} #{response.parsed_response}")
  else
    SlackChatter.logger.debug("SlackChatter::Client   #{uri} response: #{response.body}")
  end
  response.body.force_encoding("utf-8") if response.body and response.body.respond_to?(:force_encoding)
  SlackChatter::Response.new(response)
end

#set_logger(level) ⇒ Object



74
75
76
# File 'lib/slack_chatter/client.rb', line 74

def set_logger(level)
  SlackChatter.logger(level)
end