diff --git a/lib/glassfrog/accountability.rb b/lib/glassfrog/accountability.rb new file mode 100644 index 0000000..da68572 --- /dev/null +++ b/lib/glassfrog/accountability.rb @@ -0,0 +1,12 @@ +require 'glassfrog/base' + +module Glassfrog + # + # Encapsulates GlassFrog Accountability + # + class Accountability < Glassfrog::Base + # @return [String] + attr_accessor :description + end +end + diff --git a/lib/glassfrog/base.rb b/lib/glassfrog/base.rb index fe3bb0f..c055d76 100644 --- a/lib/glassfrog/base.rb +++ b/lib/glassfrog/base.rb @@ -39,5 +39,20 @@ def hashify self.instance_variables.each { |var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) } symbolize_keys(hash) end + + def build_link_objects(response) + link_types.each do |type| + build_link_objects_with_type(response, type) + end + end + + def build_link_objects_with_type(response, type) + link_objects = links[type].map do |link_id| + links = response[:linked][type] + attributes = links.detect { |link| link[:id] == link_id } + LinkFactory.build(type, attributes) + end + self.send("#{type}=", link_objects) + end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/circle.rb b/lib/glassfrog/circle.rb index bf523b4..e26f76b 100644 --- a/lib/glassfrog/circle.rb +++ b/lib/glassfrog/circle.rb @@ -1,5 +1,7 @@ require 'glassfrog/base' require 'glassfrog/rest/get' +require 'glassfrog/role' +require 'glassfrog/link_factory' module Glassfrog # @@ -9,12 +11,18 @@ class Circle < Glassfrog::Base # @return [String] attr_accessor :name, :short_name, :strategy # @return [Hash] - attr_accessor :links + attr_accessor :links, :roles # @return [Array] The array of Circle(s) fetched from GlassFrog. def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) - response[TYPE].map { |object| self.new(object) } + response[TYPE].map do |object| + circle = self.new(object) + circle.build_link_objects(response) + circle + end end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/domain.rb b/lib/glassfrog/domain.rb new file mode 100644 index 0000000..1535d12 --- /dev/null +++ b/lib/glassfrog/domain.rb @@ -0,0 +1,11 @@ +require 'glassfrog/base' + +module Glassfrog + # + # Encapsulates GlassFrog Accountability + # + class Domain < Glassfrog::Base + # @return [String] + attr_accessor :description + end +end diff --git a/lib/glassfrog/link_factory.rb b/lib/glassfrog/link_factory.rb new file mode 100644 index 0000000..1f561ee --- /dev/null +++ b/lib/glassfrog/link_factory.rb @@ -0,0 +1,16 @@ +module Glassfrog + class LinkFactory + @@config = {} + + def self.register(key, klass) + @@config[key] = klass + end + + def self.build(link_type, attributes) + klass = @@config[link_type] + object = klass.new(attributes) + object + end + end +end + diff --git a/lib/glassfrog/person.rb b/lib/glassfrog/person.rb index 72b8e6d..39ce318 100644 --- a/lib/glassfrog/person.rb +++ b/lib/glassfrog/person.rb @@ -81,4 +81,4 @@ def self.parse_options(options) params_hash end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/role.rb b/lib/glassfrog/role.rb index 68de8cb..0d0da5c 100644 --- a/lib/glassfrog/role.rb +++ b/lib/glassfrog/role.rb @@ -1,6 +1,10 @@ require 'glassfrog/base' require 'glassfrog/rest/get' require 'glassfrog/rest/patch' +require 'glassfrog/link_factory' +require 'glassfrog/accountability' +require 'glassfrog/domain' +require 'glassfrog/person' module Glassfrog # @@ -10,11 +14,19 @@ class Role < Glassfrog::Base # @return [String] attr_accessor :name, :purpose # @return [Hash] - attr_accessor :links + attr_accessor :links, :accountabilities, :domains, :people PATH = '/roles' PATCH_PATH = '/roles/0/links/people/' TYPE = :roles + LinkFactory.register(:accountabilities, Accountability) + LinkFactory.register(:domains, Domain) + LinkFactory.register(:people, Person) + + def link_types + [:accountabilities, :domains, :people] + end + # # Sends a GET request for Role(s) to GlassFrog. # @param client [Glassfrog::Client] The client that will send the request. Contains the API key. @@ -23,7 +35,11 @@ class Role < Glassfrog::Base # @return [Array] The array of Role(s) fetched from GlassFrog. def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) - response[TYPE].map { |object| self.new(object) } + response[TYPE].map do |object| + role = self.new(object) + role.build_link_objects(response) + role + end end # @@ -91,4 +107,4 @@ def self.formify_role_patch(options, operation) end end end -end \ No newline at end of file +end