Module: Authful::Endpoint::ClassMethods

Defined in:
lib/authful/endpoint.rb

Instance Method Summary collapse

Instance Method Details

#capture_common_errors(error) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/authful/endpoint.rb', line 33

def capture_common_errors(error)
  if error.http_code == 400
    JSON.parse($!.response)
  elsif error.http_code == 401
    raise Authful::Errors::IncorrectApiToken
  elsif error.http_code == 403
    JSON.parse($!.response)
  elsif error.http_code == 404
    raise Authful::Errors::InvalidUserToken
    JSON.parse($!.response)
  elsif error.http_code == 409
    JSON.parse($!.response)
  else
    raise error
  end
end

#configObject



15
16
17
# File 'lib/authful/endpoint.rb', line 15

def config
  @config
end

#configure(&block) ⇒ Object



11
12
13
# File 'lib/authful/endpoint.rb', line 11

def configure(&block)
  block.call(@config)
end

#headersObject



54
55
56
# File 'lib/authful/endpoint.rb', line 54

def headers
  {"Api-Token" => Authful.config.api_key}
end

#send_request(method, path, data = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/authful/endpoint.rb', line 19

def send_request(method, path, data = {})
  if %w(get delete).include?(method.to_s)
    unless data.empty?
      path << "?" << data.map{|k,v| "#{k}=#{v}"}.join("&")
    end
    r = RestClient.send(method, url_for(path), headers)
  else
    r = RestClient.send(method, url_for(path), data, headers)
  end
  JSON.parse(r)
rescue
  capture_common_errors($!)
end

#url_for(path) ⇒ Object



50
51
52
# File 'lib/authful/endpoint.rb', line 50

def url_for(path)
  [Authful.config.endpoint, path].join("")
end