Module: CF::Client::ClassMethods

Defined in:
lib/cf/client.rb

Instance Method Summary collapse

Instance Method Details

#default_paramsObject



7
8
9
# File 'lib/cf/client.rb', line 7

def default_params
  {:api_key => CF.api_key}
end

#delete(*args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cf/client.rb', line 31

def delete(*args)
  if args.last == {:forced=>true}
    handle_response  RestClient.delete("#{CF.api_url}#{CF.api_version}#{args.first}?api_key=#{CF.api_key}&&forced=true", :accept => 'json'){ |response, request, result| response }
  else
    handle_response  RestClient.delete("#{CF.api_url}#{CF.api_version}#{args.first}?api_key=#{CF.api_key}", :accept => 'json'){ |response, request, result| response }
  end
end

#get(*args) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/cf/client.rb', line 11

def get(*args)
  if args.length > 1
    handle_response RestClient.get("#{CF.api_url}#{CF.api_version}#{args.first}", :params => default_params.merge!(args.last), :accept => 'json'){ |response, request, result| response }
  else
    handle_response RestClient.get("#{CF.api_url}#{CF.api_version}#{args.first}", :params => default_params, :accept => 'json'){ |response, request, result| response }
  end
end

#handle_response(response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cf/client.rb', line 39

def handle_response(response)
  unless response.length == 2
    parsed_response = JSON.load(response)
    if parsed_response.is_a?(Array)
      return parsed_response
    else
      parsed_resp = parsed_response.merge("code" => response.code)
      new_response = parsed_resp.inject({ }) do |x, (k,v)|
        x[k.sub(/\A_/, '')] = v
        x
      end
      return new_response
    end
  else
    JSON.load(response)
  end
end

#post(*args) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cf/client.rb', line 19

def post(*args)
  if args.length > 1
    handle_response  RestClient.post("#{CF.api_url}#{CF.api_version}#{args.first}", args.last.merge!(default_params), :accept => 'json'){ |response, request, result| response }
  else
    handle_response  RestClient.post("#{CF.api_url}#{CF.api_version}#{args.first}", default_params, :accept => 'json'){ |response, request, result| response }
  end
end

#put(*args) ⇒ Object



27
28
29
# File 'lib/cf/client.rb', line 27

def put(*args)
  handle_response  RestClient.put("#{CF.api_url}#{CF.api_version}#{args.first}", args.last.merge!(default_params), :accept => 'json'){ |response, request, result| response }
end