Module: RestApiClient

Defined in:
lib/rest/api/client.rb,
lib/rest/api/client/config.rb,
lib/rest/api/client/logger.rb,
lib/rest/api/client/version.rb,
lib/rest/api/client/json_parser.rb,
lib/rest/api/client/request_handler.rb,
lib/rest/api/exceptions/service_url_exception.rb,
lib/rest/api/exceptions/model_errors_exception.rb,
lib/rest/api/exceptions/unauthorized_exception.rb

Defined Under Namespace

Classes: ModelErrorsException, RequestsHandler, RestModel, ServiceUrlException, UnauthorizedException

Constant Summary collapse

VERSION =
'0.1.11'

Class Method Summary collapse

Class Method Details

.configObject



39
40
41
# File 'lib/rest/api/client/config.rb', line 39

def self.config
  @config
end

.configure(opts = {}) ⇒ Object

Configure through hash



11
12
13
# File 'lib/rest/api/client/config.rb', line 11

def self.configure(opts = {})
  opts.each { |k, v| @config[k.to_sym] = v }
end

.configure_authorization(client_name, auth_key) ⇒ Object

Configure the authorization_key for one client



16
17
18
19
# File 'lib/rest/api/client/config.rb', line 16

def self.configure_authorization(client_name, auth_key)
  @config['authorization'] ||= {}
  @config['authorization'][client_name] = auth_key
end

.configure_with(path_to_yaml_file) ⇒ Object

Configure through yaml file



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rest/api/client/config.rb', line 27

def self.configure_with(path_to_yaml_file)
  begin
    config = YAML::load(IO.read(path_to_yaml_file))
    configure(config)
  rescue Errno::ENOENT
    RestApiClient.logger.warn("YAML configuration file couldn't be found. Using defaults.");
  rescue Psych::SyntaxError
    RestApiClient.logger.warn('YAML configuration file contains invalid syntax. Using defaults.');
  end

end

.get_auth_key(client_name) ⇒ Object



21
22
23
24
# File 'lib/rest/api/client/config.rb', line 21

def self.get_auth_key(client_name)
  @config['authorization'] ||= {}
  @config['authorization'][client_name]
end

.loggerObject



3
4
5
# File 'lib/rest/api/client/logger.rb', line 3

def self.logger
  @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

.logger=(logger) ⇒ Object



7
8
9
# File 'lib/rest/api/client/logger.rb', line 7

def self.logger=(logger)
  @@logger = logger
end

.parse_json(json, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rest/api/client/json_parser.rb', line 5

def self.parse_json(json, opts = {})
  begin
    json_response = JSON.parse json

    data_type = opts[:type]

    json_data = {}
    if json_response.kind_of?(Hash) && json_response.has_key?('data')
      json_data = json_response['data']
    end

    if json_data.kind_of?(Array)
      return json_data.map { |data| self.build_instance data_type, data } if data_type
      return json_data unless json_data.empty?

    elsif json_data.kind_of?(Hash)
      return self.build_instance data_type, json_data if data_type
      return json_data unless json_data.empty?

    else
      return json_data
    end
    return json_response

  rescue Exception => e
    return (opts[:default_return] || nil)
  end
end