Class: Synapsis::APIResource

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

Direct Known Subclasses

Node, Subscription, Transaction, User, V01::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.split('::').last.downcase
end

.class_name_pluralizedObject



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

def self.class_name_pluralized
  "#{class_name}s"
end

.parse_as_synapse_resource(response) ⇒ Object



55
56
57
# File 'lib/synapsis_v3/api_resource.rb', line 55

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

.request(method, url, params, headers = {}) ⇒ 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, headers = {})
  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 headers[:oauth_key] && headers[:fingerprint]
      req.headers['X-SP-USER'] = "#{headers[:oauth_key]}|#{headers[:fingerprint]}"
    end

    if headers[:ip_address]
      req.headers['X-SP-USER-IP'] = headers[: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



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

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

  if response.success?
    return parsed_response
  else
    puts parsed_response
    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



35
36
37
# File 'lib/synapsis_v3/api_resource.rb', line 35

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