Module: InfomemeClient::Communication

Included in:
InfomemeClient, MemeApplication
Defined in:
lib/infomeme_client/communication.rb

Instance Method Summary collapse

Instance Method Details

#extract_from_response(extract, method, path, *args) ⇒ Object



32
33
34
35
36
# File 'lib/infomeme_client/communication.rb', line 32

def extract_from_response(extract, method, path, *args)
  handle_response(method, path, *args) do |resp|
    resp.extract(extract) unless resp.error?
  end
end

#extract_or_response(extract, method, path, options, headers = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/infomeme_client/communication.rb', line 38

def extract_or_response(extract, method, path, options, headers = {})
  no_extract = options[:no_extract] || false
  options.delete(:no_extract)
  if [:put, :post].include?(method.to_sym)
    args = [method, path, options, headers]
  else
    args = [method, finalize_get_url(path, options), headers]
  end
  if no_extract
    handle_response(*args)
  else
    extract_from_response(*args.unshift(extract))
  end
end

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



2
3
4
# File 'lib/infomeme_client/communication.rb', line 2

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

#handle_response(method, path, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/infomeme_client/communication.rb', line 18

def handle_response(method, path, *args, &block)
  response = request(method, path, *args)
  response.raise_error
  if block_given?
    if block.arity == 1
      yield response
    else
      yield unless response.error?
    end
  else
    response
  end
end

#post(path, body = "", headers = {}) ⇒ Object



6
7
8
# File 'lib/infomeme_client/communication.rb', line 6

def post(path, body = "", headers = {})
  request(:post, path, body, headers)
end

#put(path, body = "", headers = {}) ⇒ Object



10
11
12
# File 'lib/infomeme_client/communication.rb', line 10

def put(path, body = "", headers = {})
  request(:put, path, body, headers)
end

#request(method, path, *args) ⇒ Object



14
15
16
# File 'lib/infomeme_client/communication.rb', line 14

def request(method, path, *args)
  token_request(token, method, path, *args)
end