Class: Webceo::Api::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/webceo/api/client.rb

Overview

Class Client provides methods to make api requests and handle exceptions

Author:

Instance Method Summary collapse

Constructor Details

#initializeClient

Intializes an instance of Client to make api calls



17
# File 'lib/webceo/api/client.rb', line 17

def initialize; end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, data = {}) ⇒ Array<Hash>

This method validates if the method invoked is a valid API Method else raises Error For a valid API Method it builds the request, makes the api call and parses the response and check for any errors in response returned

Parameters:

  • method_name (String)

    Name of the api method invoke

  • data (Hash) (defaults to: {})

    Any arguments required to sent along with the method

Returns:

  • (Array<Hash>)

    Array of Response hashes for the request made



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

def method_missing(method_name, data = {})
  if list_methods.include? method_name.to_s
    request = Webceo::Api::Request.new method_name, data
    response = make_request(request)
    response.check_for_errors

    begin
      ::MultiJson.load(response.body, :symbolize_keys => true)
    rescue ::MultiJson::ParseError => e
      e.cause
    end
  else
    raise Webceo::Api::ClientError.new(400, nil, {"errormsg" => "Unknown command","method" => method_name.to_s,"result" => 10})
  end
end

Instance Method Details

#list_methodsArray<String>

It lists names of all the available API methods on the Webceo API Reference

Returns:

  • (Array<String>)

    List of API Methods

See Also:



26
27
28
# File 'lib/webceo/api/client.rb', line 26

def list_methods
  API_METHODS.values.flatten
end

#make_request(request) ⇒ Webceo::Api::Response

This method makes the actual hit to the Webceo API Server and returns the response wrapped with our Response class.

It also raises any errors occurred when making the request

Parameters:

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/webceo/api/client.rb', line 66

def make_request(request)
  result = self.class.post('/', body: request.to_json)

  if result.code >= 500
    raise Webceo::Api::ServerError.new(result.code, result.body)
  end

  if result.code >= 400
    raise Webceo::Api::ClientError.new(result.code, result.body)
  end

  Webceo::Api::Response.new(result.code, result.body, result.headers, result.message)
end