Sha256: c3fb0feb850621d367022b5f375e1669088b6a454a5069c59a4c58071c96f8be

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require "material_service_client/version"

module MaterialServiceClient
	def self.post(data)
		conn = get_connection
		JSON.parse(conn.post('/materials', data.to_json).body)
	end

	def self.put(data)
		uuid = data[:uuid]
		data_to_send = data.reject{|k,v| k.to_sym == :uuid}

		conn = get_connection
		JSON.parse(conn.put('/materials/'+uuid, data_to_send.to_json).body)
	end

	def self.get(uuid)
		return nil if uuid.nil?
		conn = get_connection
		JSON.parse(conn.get('/materials/'+uuid).body)
	end

	def self.valid?(uuids)
		conn = get_connection
		data = { materials: uuids }

		response = conn.post('/materials/validate', data.to_json)
		
		# response = conn.post do |req|
		# 	req.url '/materials/validate'
		# 	req.headers['Content-Type'] = 'application/json'
		# 	req.headers['Accept'] = 'application/json'
		# 	req.body = { materials: uuids }.to_json
		# end
		response.body == 'ok'
  	end

	private

	def self.get_connection
		conn = Faraday.new(:url => Rails.application.config.material_url) do |faraday|
		  # faraday.use ZipkinTracer::FaradayHandler, 'eve'
		  faraday.proxy Rails.application.config.material_url
		  faraday.request  :url_encoded
		  faraday.response :logger
		  faraday.adapter  Faraday.default_adapter 
		end
		conn.headers = {'Content-Type' => 'application/json'} 
		conn
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
material_service_client-0.1.11 lib/material_service_client.rb