Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Api
- module V1
- class CurrenciesController < ApplicationController
- def index
- render json: { data: Rdf::StaticAttributes::Amount.new.all_valid_currencies }, status: :ok
- end
- end
- end
- end
- module Api
- module V1
- class RdfAttributesController < ApplicationController
- before_action :check_param_type
- def index
- render json: {
- data: "Rdf::StaticAttributes::#{params[:type].capitalize}".constantize.all
- }, status: :ok
- end
- private
- def check_param_type
- render json: { msg: "invalid type" } unless %w[Amount Address].include?(params[:type].capitalize)
- end
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class Base
- include Rdf::Utils
- include ActiveModel::Validations
- attr_accessor :value, :uri
- validates_presence_of :value
- def initialize(attributes = {})
- attributes.each do |name, value|
- send("#{name}=", value)
- end
- end
- def save
- uuid = SecureRandom.uuid
- sparql_insert_query = build_sparql_insert_query(uuid)
- result = GraphDBConnector.instance.sparql_api_statement(sparql_insert_query)
- return false if result[:success] == false
- "https://www.webdoxclm.com/instances##{class_name}-#{uuid}"
- end
- def build_sparql_insert_query(uuid)
- %(
- PREFIX wco: <https://www.webdoxclm.com/ontology#>
- PREFIX wci: <https://www.webdoxclm.com/instances#>
- INSERT DATA {
- wci:#{class_name}-#{uuid} a wco:#{class_name} ;
- wco:#{class_name.downcase}Value "#{@value}" .
- }
- )
- end
- class << self
- def all
- sparql_query = %(
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
- PREFIX wco: <https://www.webdoxclm.com/ontology#>
- PREFIX wci: <https://www.webdoxclm.com/instances#>
- SELECT ?#{name.demodulize} ?value WHERE {
- ?#{name.demodulize} a wco:#{name.demodulize} ;
- wco:#{name.demodulize.downcase}Value ?value ;
- }
- )
- result = GraphDBConnector.instance.query(sparql_query)
- return [] unless result[:success]
- result[:data].map do |data|
- new(
- uri: data[name.demodulize].value,
- value: data.value.value
- )
- end
- end
- def find(uri)
- sparql_query = %(
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
- PREFIX wco: <https://www.webdoxclm.com/ontology#>
- PREFIX wci: <https://www.webdoxclm.com/instances#>
- SELECT ?value WHERE {
- <#{uri}> a wco:#{name.demodulize} ;
- wco:#{name.demodulize.downcase}Value ?value ;
- }
- )
- result = GraphDBConnector.instance.query(sparql_query)
- return nil unless result[:success] && result[:data].any?
- data = result[:data].first
- new(
- uri: uri,
- value: data.value.value
- )
- end
- def destroy(uri)
- return unless uri
- sparql_delete_query = %(
- DELETE WHERE {
- <#{uri}> ?p ?o .
- })
- GraphDBConnector.instance.sparql_api_statement(sparql_delete_query)
- end
- end
- def class_name
- self.class.name.demodulize
- end
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class Date < Base
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class List < Base
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class Number < Base
- validates_numericality_of :value, greater_than: 0.0
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class Text < Base
- end
- end
- end
- module Rdf
- module PrimitiveAttributes
- class Time < Base
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment