Module: Slack

Defined in:
lib/slack/error.rb,
lib/slack/web.rb,
lib/slack/client.rb,
lib/slack/oauth2.rb,
lib/slack/web/im.rb,
lib/slack/session.rb,
lib/slack/version.rb,
lib/slack/web/api.rb,
lib/slack/web/auth.rb,
lib/slack/web/chat.rb,
lib/slack/web/team.rb,
lib/slack/web/emoji.rb,
lib/slack/web/files.rb,
lib/slack/web/stars.rb,
lib/slack/web/users.rb,
lib/slack/web/groups.rb,
lib/slack/web/search.rb,
lib/slack-api-wrapper.rb,
lib/slack/oauth2/flow.rb,
lib/slack/web/channels.rb,
lib/slack/oauth2/flow_base.rb

Overview

Copyright © 2015 Gustavo Bazan MIT License

Defined Under Namespace

Modules: Oauth2, Web Classes: AuthError, Client, Error, Session

Constant Summary collapse

VERSION =

The current version of the wrapper.

'0.0.6'
WEB_SERVER =

Slack url

'slack.com'
API_SERVER =

Slack api path

'slack.com/api'

Class Method Summary collapse

Class Method Details

.clean_params(params) ⇒ Hash

Removes nil params

Parameters:

  • params (Hash)

    API call arguments

Returns:

  • (Hash)


31
32
33
34
35
36
37
# File 'lib/slack-api-wrapper.rb', line 31

def self.clean_params(params)
  r = {}
  params.each do |k,v|
    r[k] = v.to_s unless v.nil?
  end
  r
end

.do_http(uri, request) ⇒ Net::HTTPResponse

Handle http requests

Parameters:

  • uri (URI::HTTPS)

    API uri

  • request (Object)

    request object

Returns:

  • (Net::HTTPResponse)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/slack-api-wrapper.rb', line 56

def self.do_http(uri, request)

  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true

  # Let then know about us
  request['User-Agent'] =  "SlackRubyAPIWrapper"

  begin
    http.request(request)
  rescue OpenSSL::SSL::SSLError => e
    raise SlackError.new("SSL error connecting to Slack.")
  end
end

.make_query_string(params) ⇒ String

Convert params to query string

Parameters:

  • params (Hash)

    API call arguments

Returns:

  • (String)


44
45
46
47
48
# File 'lib/slack-api-wrapper.rb', line 44

def self.make_query_string(params)
  clean_params(params).collect {|k,v|
    CGI.escape(k) + "=" + CGI.escape(v)
  }.join("&")
end

.parse_response(response, raw = false) ⇒ Object

Parse response. You probably shouldn’t be calling this directly. This takes responses from the server and parses them. It also checks for errors and raises exceptions with the appropriate messages.

Parameters:

  • response (Net::HTTPResponse)
  • raw (Boolean) (defaults to: false)

    if return raw data

Raises:

  • (SlackError)
  • (SlackAuthError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/slack-api-wrapper.rb', line 78

def self.parse_response(response, raw=false)
  if response.kind_of?(Net::HTTPServerError)
    raise SlackError.new("Slack Server Error: #{response} - #{response.body}", response)
  elsif response.kind_of?(Net::HTTPUnauthorized)
    raise SlackAuthError.new("User is not authenticated.", response)
  elsif !response.kind_of?(Net::HTTPSuccess)
    begin
      d = JSON.parse(response.body)
    rescue
      raise SlackError.new("Slack Server Error: body=#{response.body}", response)
    end
    if d['error']
      raise SlackError.new(d['error'], response)
    else
      raise SlackError.new(response.body, response)
    end
  end

  return response.body if raw

  begin
    return JSON.parse(response.body)
  rescue JSON::ParserError
    raise SlackError.new("Unable to parse JSON response: #{response.body}", response)
  end
end

.safe_string_equals(a, b) ⇒ Boolean

A string comparison function that is resistant to timing attacks. If you’re comparing a string you got from the outside world with a string that is supposed to be a secret, use this function to check equality.

Parameters:

  • a (String)
  • b (String)

Returns:

  • (Boolean)

    whether the strings are equal



111
112
113
114
115
116
117
# File 'lib/slack-api-wrapper.rb', line 111

def self.safe_string_equals(a, b)
  if a.length != b.length
    false
  else
    a.chars.zip(b.chars).map {|ac,bc| ac == bc}.all?
  end
end