Class: Synapsis::APIResource

Inherits:
Object
  • Object
show all
Defined in:
lib/synapsis_v3/api_resource.rb

Direct Known Subclasses

Node, Subscription, Transaction, User

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_nameObject



27
28
29
# File 'lib/synapsis_v3/api_resource.rb', line 27

def self.class_name
  name.partition('::').last.downcase
end

.client_credentialsObject



56
57
58
59
60
61
62
63
# File 'lib/synapsis_v3/api_resource.rb', line 56

def self.client_credentials
  {
    "client" => {
      "client_id" => Synapsis.client_id,
      "client_secret" => Synapsis.client_secret
    }
  }
end

.parse_as_synapse_resource(response) ⇒ Object



50
51
52
# File 'lib/synapsis_v3/api_resource.rb', line 50

def self.parse_as_synapse_resource(response)
  return JSON.parse(response.body, object_class: Synapsis::Response)
end

.request(method, url, params, oauth_key: nil, fingerprint: nil, ip_address: nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/synapsis_v3/api_resource.rb', line 2

def self.request(method, url, params, oauth_key: nil, fingerprint: nil, ip_address: nil)
  Synapsis.connection.send(method) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-SP-LANG'] = 'EN' # Set language to English
    req.headers['X-SP-GATEWAY'] = "#{Synapsis.client_id}|#{Synapsis.client_secret}"

    if oauth_key && fingerprint
      req.headers['X-SP-USER'] = "#{oauth_key}|#{fingerprint}"
    end

    if ip_address
      req.headers['X-SP-USER-IP'] = ip_address
    end

    if Synapsis.environment == 'production'
      req.headers['X-SP-PROD'] = 'YES'
    else
      req.headers['X-SP-PROD'] = 'NO'
    end

    req.url url
    req.body = JSON.generate(params)
  end
end

.return_response(response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/synapsis_v3/api_resource.rb', line 35

def self.return_response(response)
  parsed_response = JSON.parse(response.body, object_class: Synapsis::Response)

  if response.success?
    return parsed_response
  else
    raise Synapsis::Error.new(
      error: parsed_response.error,
      http_code: parsed_response.http_code,
      error_code: parsed_response.error_code,
      success: parsed_response.success
    )
  end
end

Instance Method Details

#class_nameObject



31
32
33
# File 'lib/synapsis_v3/api_resource.rb', line 31

def class_name
  self.class.name.partition('::').last.downcase
end