Class: Syncano::Clients::Rest

Inherits:
Base
  • Object
show all
Defined in:
lib/syncano/clients/rest.rb

Overview

Client used for communication with the JSON-RPC endpoint

Instance Attribute Summary collapse

Attributes inherited from Base

#api_key, #auth_key, #instance_name

Instance Method Summary collapse

Methods inherited from Base

#admins, #api_keys, #collections, #data_objects, #folders, #logout, #projects, #roles, #users

Constructor Details

#initialize(instance_name, api_key, auth_key = nil) ⇒ Rest

Constructor for Syncano::Clients::Rest object

Parameters:

  • instance_name (String)
  • api_key (String)


10
11
12
13
# File 'lib/syncano/clients/rest.rb', line 10

def initialize(instance_name, api_key, auth_key = nil)
  super(instance_name, api_key, auth_key)
  self.client = ::Jimson::Client.new(json_rpc_url)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/syncano/clients/rest.rb', line 5

def client
  @client
end

Instance Method Details

#batch {|queue| ... } ⇒ Array

Gets block in which Syncano::BatchQueue object is provided and batch requests can be executed

Parameters:

  • (Block)

Yields:

  • (queue)

Returns:

  • (Array)

    collection of parsed responses



51
52
53
54
55
56
57
58
59
60
# File 'lib/syncano/clients/rest.rb', line 51

def batch
  queue = ::Syncano::BatchQueue.new(client)
  yield(queue)
  queue.prune!

  queue.responses.collect do |response|
    resource_name = response.first.method.split('.').first
    self.class.parse_response(resource_name, response.last.result)
  end
end

#login(username, password) ⇒ TrueClass, FalseClass

Gets auth_key based on username and password

Returns:

  • (TrueClass, FalseClass)


17
18
19
20
21
# File 'lib/syncano/clients/rest.rb', line 17

def (username, password)
  logout
  self.auth_key = users.(username, password)
  !self.auth_key.nil?
end

#make_batch_request(batch_client, resource_name, method_name, params = {}) ⇒ Object

Performs batch request to Syncano api

Parameters:

  • batch_client (Jimson::BatchClient)
  • resource_name (String)
  • method_name (String)
  • params (Hash) (defaults to: {})

    additional params sent in the request



44
45
46
# File 'lib/syncano/clients/rest.rb', line 44

def make_batch_request(batch_client, resource_name, method_name, params = {})
  batch_client.send("#{resource_name}.#{method_name}", request_params.merge(params))
end

#make_request(resource_name, method_name, params = {}, response_key = nil) ⇒ Syncano::Response

Performs request to Syncano api

Parameters:

  • resource_name (String)
  • method_name (String)
  • params (Hash) (defaults to: {})

    additional params sent in the request

  • response_key (String) (defaults to: nil)

    for cases when response from api is incompatible with the convention

Returns:



29
30
31
32
33
34
35
36
37
# File 'lib/syncano/clients/rest.rb', line 29

def make_request(resource_name, method_name, params = {}, response_key = nil)
  params.merge!(auth_key: auth_key) if auth_key.present?

  response_key ||= resource_name
  response = client.send("#{resource_name}.#{method_name}", request_params.merge(params))
  response = self.class.parse_response(response_key, response)

  response.errors.present? ? raise(Syncano::ApiError.new(response.errors)) : response
end