Module: CTAAggregatorClient::API::Client

Extended by:
Utilities
Defined in:
lib/cta_aggregator_client/api/client.rb

Defined Under Namespace

Classes: UnknownInputTypeError

Class Method Summary collapse

Methods included from Utilities

api_key, api_secret, api_version, auth_url, base_url, default_headers, headers_with_auth_creds

Class Method Details

.add_relationship(resource_name, uuid, related_resource_name, related_resource_uuids) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cta_aggregator_client/api/client.rb', line 75

def add_relationship(resource_name, uuid, related_resource_name, related_resource_uuids)
  url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}/relationships/#{related_resource_name}"
  if related_resource_uuids.is_a? Array

    resource_data = related_resource_uuids.map do |related_resource_uuid|
      { 'type': "#{related_resource_name}",
        'id': related_resource_uuid }
    end

  elsif related_resource_uuids.is_a? String
    resource_data = { 'type': "#{related_resource_name}",
                      'id': related_resource_uuids }
  else
    raise UnknownInputTypeError
  end

  payload = { data: resource_data }.to_json

  perform_authenticated_request(:put, url, payload)
end

.create(resource_name, attributes, relationships = {}) ⇒ Object



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

def create(resource_name, attributes, relationships = {})
  url = "#{base_url}/#{api_version}/#{resource_name}s"
  payload = {
    'data': {
      'type': "#{resource_name}s",
      'attributes': attributes,
      'relationships': relationship_params(relationships)
    }.reject{ |k,v| v.empty? }
  }.to_json

  perform_authenticated_request(:post, url, payload)
end

.delete(resource_name, uuid) ⇒ Object



52
53
54
55
56
57
# File 'lib/cta_aggregator_client/api/client.rb', line 52

def delete(resource_name, uuid)
  url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}"

  raw_response = RestClient.delete(url, headers_with_access_token)
  translate_response(raw_response)
end

.find(resource_name, uuid) ⇒ Object



20
21
22
23
24
# File 'lib/cta_aggregator_client/api/client.rb', line 20

def find(resource_name, uuid)
  url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}"
  raw_response = RestClient.get(url, default_headers)
  translate_response(raw_response)
end

.headers_with_access_tokenObject



101
102
103
# File 'lib/cta_aggregator_client/api/client.rb', line 101

def headers_with_access_token
  Authenticator.headers_with_authentication
end

.list(resource_name, filters) ⇒ Object



14
15
16
17
18
# File 'lib/cta_aggregator_client/api/client.rb', line 14

def list(resource_name, filters)
  url = "#{base_url}/#{api_version}/#{resource_name}s"
  raw_response = RestClient.get(url, {headers: default_headers, params: {filter:  filters} })
  translate_response(raw_response)
end

.perform_authenticated_request(http_method, url, payload = {}) ⇒ Object



96
97
98
99
# File 'lib/cta_aggregator_client/api/client.rb', line 96

def perform_authenticated_request(http_method, url, payload={})
  raw_response = RestClient.send(http_method.to_sym, url, payload, headers_with_access_token)
  translate_response(raw_response)
end

.relationship_params(relationships) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cta_aggregator_client/api/client.rb', line 59

def relationship_params(relationships)
  return {} unless relationships && relationships.reject { |k,v| v.nil? }.any?

  relationships.each_with_object({}) do |(resource_name, uuid_data), obj|
    if uuid_data.is_a? Array
      obj[resource_name.to_sym] = {
        data: uuid_data.map {  |uuid| { type: resource_name, id: uuid } }
      }
    else
      obj[resource_name.to_sym] = {
        data: { type: "#{resource_name}s", id: uuid_data }
      }
    end
  end
end

.translate_response(response) ⇒ Object



105
106
107
# File 'lib/cta_aggregator_client/api/client.rb', line 105

def translate_response(response)
  Response.new(response.code, response.body, response.headers)
end

.update(resource_name, uuid, attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cta_aggregator_client/api/client.rb', line 39

def update(resource_name, uuid, attributes)
  url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}"
  payload = {
    'data': {
      'type': "#{resource_name}s",
      'id': uuid,
      'attributes': attributes
    }
  }.to_json

  perform_authenticated_request(:put, url, payload)
end