Module: Lolp::Connection

Included in:
Client
Defined in:
lib/lolp/connection.rb

Instance Method Summary collapse

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/lolp/connection.rb', line 23

def authenticated?
  !!@token
end

#auto_loginable?(url) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lolp/connection.rb', line 31

def auto_loginable?(url)
  url !~ /\/authenticate/ && @username && @password
end

#delete(path, params = {}) ⇒ Object



19
20
21
# File 'lib/lolp/connection.rb', line 19

def delete(path, params = {})
  request(:delete, path, params)
end

#get(path, params = {}) ⇒ Object



7
8
9
# File 'lib/lolp/connection.rb', line 7

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

#last_responseObject



27
28
29
# File 'lib/lolp/connection.rb', line 27

def last_response
  @last_response if defined? @last_response
end

#post(path, params = {}) ⇒ Object



15
16
17
# File 'lib/lolp/connection.rb', line 15

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

#put(path, params = {}) ⇒ Object



11
12
13
# File 'lib/lolp/connection.rb', line 11

def put(path, params = {})
  request(:put, path, params)
end

#request(method, url = nil, data = nil, headers = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lolp/connection.rb', line 35

def request(method, url = nil, data = nil, headers = nil, &block)
   if auto_loginable?(url)

  @last_response = if %i(post put patch).include?(method)
      connection.run_request(method, url, data, headers, &block)
    else
      connection.run_request(method, url, nil, headers) { |r|
        r.params.update(data) if data
        yield(r) if block_given?
      }
    end

  if error = Error.from_response(@last_response)
    raise error
  end
  @last_response.body
end