Sha256: 6d8be4e41e9e024c66b8a62395fc62d2c6004ae4ebadbfecd07a39d55f1c0d3c

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require_relative "set_service_client/version"
require 'faraday'
require 'json'

module SetServiceClient
	def self.post(submission_id)
		data = {:data=>{:type=>"sets", :attributes=>{:name=>submission_id}}}
		conn = get_connection
		JSON.parse(conn.post('/api/v1/sets', data.to_json).body)
	end

	def self.add_materials(set_uuid, materials)
		data = {:data => materials.compact.map{|m| {:id => m.uuid, :type => 'materials'}}}
		conn = get_connection
		conn.post('/api/v1/sets/'+set_uuid+'/relationships/materials', data.to_json)
	end

	def self.get_with_materials(set_uuid)
		conn = get_connection
		conn.headers = {'Accept' => 'application/vnd.api+json'}
		JSON.parse(conn.get('/api/v1/sets/'+set_uuid+'/relationships/materials').body)
	end

	def self.get_set(set_uuid)
		conn = get_connection
		conn.headers = {'Accept' => 'application/vnd.api+json'}
		JSON.parse(conn.get('/api/v1/sets/'+set_uuid).body)
	end

	def self.get_sets
		conn = get_connection
		conn.headers = {'Accept' => 'application/vnd.api+json'}
		p filter(JSON.parse(conn.get('/api/v1/sets').body))
	end

	private

	def self.get_connection
		conn = Faraday.new(:url => ENV['SET_URL']) do |faraday|
			# faraday.use ZipkinTracer::FaradayHandler, 'Set Service'
			faraday.proxy ENV['SET_URL']
			faraday.request  :url_encoded
			faraday.response :logger
			faraday.adapter  Faraday.default_adapter
		end
		conn.headers = {'Content-Type' => 'application/vnd.api+json'}
		conn
	end

	def self.filter(obj)
		obj["data"].map { |item| filter_attrs(item) }
  	end

	def self.filter_attrs(h)
		{id: h["id"], name: h["attributes"]["name"]}
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
set_service_client-0.1.6 lib/set_service_client.rb