Class: Broach::Session

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
lib/broach/session.rb

Overview

Represents a session with Campfire

Instance Method Summary collapse

Methods included from Attributes

#id, #initialize, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Broach::Attributes

Instance Method Details

#credentialsObject

Returns the credentials to authenticate the current user



33
34
35
# File 'lib/broach/session.rb', line 33

def credentials
  { :username => token, :password => 'x' }
end

#get(path) ⇒ Object

Gets a resource with a certain path on the server. When the GET is succesful the parsed body is returned, otherwise an exception is raised.



39
40
41
42
43
44
45
46
# File 'lib/broach/session.rb', line 39

def get(path)
  response = REST.get(url_for(path), headers_for(:get), credentials)
  if response.ok?
    MultiJson.load(response.body)
  else
    handle_response(:get, path, response)
  end
end

#headers_for(method) ⇒ Object

Returns the headers to send for a specific HTTP method

session.headers_for(:get) #=> { 'Accept' => 'application/json' }


26
27
28
29
30
# File 'lib/broach/session.rb', line 26

def headers_for(method)
  headers = { 'Accept' => 'application/json', 'User-Agent' => 'Broach' }
  headers['Content-type'] = 'application/json' if method == :post
  headers
end

#post(path, payload) ⇒ Object

Posts a resource to a certain path on the server. When the POST is successful the parsed body is returned, otherwise an exception is raised.



50
51
52
53
54
55
56
57
# File 'lib/broach/session.rb', line 50

def post(path, payload)
  response = REST.post(url_for(path), MultiJson.dump(payload), headers_for(:post), credentials)
  if response.created?
    MultiJson.load(response.body)
  else
    handle_response(:post, path, response)
  end
end

#schemeObject

Returns either http or https depending on whether we should use SSL or not.



12
13
14
# File 'lib/broach/session.rb', line 12

def scheme
  use_ssl? ? 'https' : 'http'
end

#url_for(path) ⇒ Object

Returns the full URL for a certain path

session.url_for('rooms') #=> "http://example.campfirenow.com/rooms"


19
20
21
# File 'lib/broach/session.rb', line 19

def url_for(path)
  ["#{scheme}:/", "#{}.campfirenow.com", path].join('/')
end

#use_ssl?Boolean

Returns true when the connection should use SSL and false otherwise.

Returns:

  • (Boolean)


7
8
9
# File 'lib/broach/session.rb', line 7

def use_ssl?
  @attributes['use_ssl'] || false
end