NameL3ss

rdf

Nov 3rd, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. module Api
  2. module V1
  3. class CurrenciesController < ApplicationController
  4. def index
  5. render json: { data: Rdf::StaticAttributes::Amount.new.all_valid_currencies }, status: :ok
  6. end
  7. end
  8. end
  9. end
  10.  
  11. module Api
  12. module V1
  13. class RdfAttributesController < ApplicationController
  14. before_action :check_param_type
  15.  
  16. def index
  17. render json: {
  18. data: "Rdf::StaticAttributes::#{params[:type].capitalize}".constantize.all
  19. }, status: :ok
  20. end
  21.  
  22. private
  23.  
  24. def check_param_type
  25. render json: { msg: "invalid type" } unless %w[Amount Address].include?(params[:type].capitalize)
  26. end
  27. end
  28. end
  29. end
  30.  
  31. module Rdf
  32. module PrimitiveAttributes
  33. class Base
  34. include Rdf::Utils
  35. include ActiveModel::Validations
  36. attr_accessor :value, :uri
  37.  
  38. validates_presence_of :value
  39.  
  40. def initialize(attributes = {})
  41. attributes.each do |name, value|
  42. send("#{name}=", value)
  43. end
  44. end
  45.  
  46. def save
  47. uuid = SecureRandom.uuid
  48. sparql_insert_query = build_sparql_insert_query(uuid)
  49. result = GraphDBConnector.instance.sparql_api_statement(sparql_insert_query)
  50. return false if result[:success] == false
  51.  
  52. "https://www.webdoxclm.com/instances##{class_name}-#{uuid}"
  53. end
  54.  
  55. def build_sparql_insert_query(uuid)
  56. %(
  57. PREFIX wco: <https://www.webdoxclm.com/ontology#>
  58. PREFIX wci: <https://www.webdoxclm.com/instances#>
  59.  
  60. INSERT DATA {
  61. wci:#{class_name}-#{uuid} a wco:#{class_name} ;
  62. wco:#{class_name.downcase}Value "#{@value}" .
  63. }
  64. )
  65. end
  66.  
  67. class << self
  68. def all
  69. sparql_query = %(
  70. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  71. PREFIX wco: <https://www.webdoxclm.com/ontology#>
  72. PREFIX wci: <https://www.webdoxclm.com/instances#>
  73.  
  74. SELECT ?#{name.demodulize} ?value WHERE {
  75. ?#{name.demodulize} a wco:#{name.demodulize} ;
  76. wco:#{name.demodulize.downcase}Value ?value ;
  77. }
  78. )
  79.  
  80. result = GraphDBConnector.instance.query(sparql_query)
  81. return [] unless result[:success]
  82.  
  83. result[:data].map do |data|
  84. new(
  85. uri: data[name.demodulize].value,
  86. value: data.value.value
  87. )
  88. end
  89. end
  90.  
  91. def find(uri)
  92. sparql_query = %(
  93. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  94. PREFIX wco: <https://www.webdoxclm.com/ontology#>
  95. PREFIX wci: <https://www.webdoxclm.com/instances#>
  96.  
  97. SELECT ?value WHERE {
  98. <#{uri}> a wco:#{name.demodulize} ;
  99. wco:#{name.demodulize.downcase}Value ?value ;
  100. }
  101. )
  102.  
  103. result = GraphDBConnector.instance.query(sparql_query)
  104. return nil unless result[:success] && result[:data].any?
  105.  
  106. data = result[:data].first
  107. new(
  108. uri: uri,
  109. value: data.value.value
  110. )
  111. end
  112.  
  113. def destroy(uri)
  114. return unless uri
  115.  
  116. sparql_delete_query = %(
  117. DELETE WHERE {
  118. <#{uri}> ?p ?o .
  119. })
  120. GraphDBConnector.instance.sparql_api_statement(sparql_delete_query)
  121. end
  122. end
  123.  
  124. def class_name
  125. self.class.name.demodulize
  126. end
  127. end
  128. end
  129. end
  130. module Rdf
  131. module PrimitiveAttributes
  132. class Date < Base
  133. end
  134. end
  135. end
  136.  
  137. module Rdf
  138. module PrimitiveAttributes
  139. class List < Base
  140. end
  141. end
  142. end
  143.  
  144. module Rdf
  145. module PrimitiveAttributes
  146. class Number < Base
  147. validates_numericality_of :value, greater_than: 0.0
  148. end
  149. end
  150. end
  151. module Rdf
  152. module PrimitiveAttributes
  153. class Text < Base
  154. end
  155. end
  156. end
  157. module Rdf
  158. module PrimitiveAttributes
  159. class Time < Base
  160. end
  161. end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment