Class: Slack::OAuth2Session

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/session.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(oauth2_access_token) ⇒ OAuth2Session



5
6
7
8
9
10
# File 'lib/slack/session.rb', line 5

def initialize(oauth2_access_token)
  unless oauth2_access_token.is_a?(String)
    raise "bad type for oauth2_access_token (expecting String)"
  end
  @access_token = oauth2_access_token
end

Instance Method Details

#do_get(path, params = nil, headers = nil) ⇒ Object

:nodoc:



34
35
36
37
38
39
# File 'lib/slack/session.rb', line 34

def do_get(path, params=nil, headers=nil)  # :nodoc:
  params ||= {}
  params["token"] = @access_token
  uri = build_url_with_params(path, params)
  do_http(uri, Net::HTTP::Get.new(uri.request_uri))
end

#do_http_with_body(uri, request, body) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/slack/session.rb', line 41

def do_http_with_body(uri, request, body)
  if body != nil
    if body.is_a?(Hash)
      request.set_form_data(Slack::clean_params(body))
    elsif body.respond_to?(:read)
      if body.respond_to?(:length)
        request["Content-Length"] = body.length.to_s
      elsif body.respond_to?(:stat) && body.stat.respond_to?(:size)
        request["Content-Length"] = body.stat.size.to_s
      else
        raise ArgumentError, "Don't know how to handle 'body' (responds to 'read' but not to 'length' or 'stat.size')."
      end
      request.body_stream = body
    else
      s = body.to_s
      request["Content-Length"] = s.length
      request.body = s
    end
  end
  do_http(uri, request)
end

#do_post(path, params = nil, headers = nil) ⇒ Object

:nodoc:



63
64
65
66
67
68
# File 'lib/slack/session.rb', line 63

def do_post(path, params=nil, headers=nil)  # :nodoc:
  params ||= {}
  params["token"] = @access_token
  uri = build_url(path)
  do_http_with_body(uri, Net::HTTP::Post.new(uri.request_uri, headers), params)
end

#do_put(path, params = nil, headers = nil, body = nil) ⇒ Object

:nodoc:



70
71
72
73
74
75
# File 'lib/slack/session.rb', line 70

def do_put(path, params=nil, headers=nil, body=nil)  # :nodoc:
  params ||= {}
  params["token"] = @access_token
  uri = build_url_with_params(path, params)
  do_http_with_body(uri, Net::HTTP::Put.new(uri.request_uri, headers), body)
end