module CTAAggregatorClient::API::Client

Public Class Methods

add_relationship(resource_name, uuid, related_resource_name, related_resource_uuids) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_token() click to toggle source
# File lib/cta_aggregator_client/api/client.rb, line 101
def headers_with_access_token
  Authenticator.headers_with_authentication
end
list(resource_name, filters) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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