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

#method_missing

Constructor Details

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

Returns a new instance of Client.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SlackChatter::Api::Namespaces

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#icon_urlObject (readonly)

Returns the value of attribute icon_url.



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

def icon_url
  @icon_url
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



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

def namespaces
  @namespaces
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#api_uri_rootObject



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

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



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

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



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

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

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

Perform an HTTP POST request



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

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

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



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

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



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

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