global_registry_models 0.1.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/lib/global_registry_models/.DS_Store +0 -0
  4. data/lib/global_registry_models/api_operations/delete.rb +22 -0
  5. data/lib/global_registry_models/api_operations/finders.rb +37 -0
  6. data/lib/global_registry_models/api_operations/persistence.rb +58 -0
  7. data/lib/global_registry_models/api_operations/search.rb +44 -0
  8. data/lib/global_registry_models/collection.rb +74 -0
  9. data/lib/global_registry_models/common_base.rb +50 -0
  10. data/lib/global_registry_models/entity/.DS_Store +0 -0
  11. data/lib/global_registry_models/entity/base.rb +9 -60
  12. data/lib/global_registry_models/entity/entity_collection.rb +66 -0
  13. data/lib/global_registry_models/entity/ministry.rb +37 -5
  14. data/lib/global_registry_models/entity/target_area.rb +5 -1
  15. data/lib/global_registry_models/entity_type/.DS_Store +0 -0
  16. data/lib/global_registry_models/entity_type/base.rb +21 -0
  17. data/lib/global_registry_models/entity_type/entity_type.rb +49 -0
  18. data/lib/global_registry_models/entity_type/field.rb +34 -0
  19. data/lib/global_registry_models/errors/record_invalid.rb +4 -0
  20. data/lib/global_registry_models/measurement_type/.DS_Store +0 -0
  21. data/lib/global_registry_models/measurement_type/base.rb +19 -0
  22. data/lib/global_registry_models/measurement_type/measurement_type.rb +19 -0
  23. data/lib/global_registry_models/relationship_type/.DS_Store +0 -0
  24. data/lib/global_registry_models/relationship_type/base.rb +19 -0
  25. data/lib/global_registry_models/relationship_type/involved_type.rb +10 -0
  26. data/lib/global_registry_models/relationship_type/relationship_type.rb +26 -0
  27. data/lib/global_registry_models/response_parser.rb +15 -6
  28. data/lib/global_registry_models/version.rb +1 -1
  29. data/lib/global_registry_models.rb +20 -4
  30. metadata +23 -6
  31. data/lib/global_registry_models/entity/api_operations/delete.rb +0 -24
  32. data/lib/global_registry_models/entity/api_operations/finders.rb +0 -39
  33. data/lib/global_registry_models/entity/api_operations/search.rb +0 -37
  34. data/lib/global_registry_models/entity/collection.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e29c0de9cfad74c834e9cddc425fea1f7c0f8382
4
- data.tar.gz: 47ba8ada08097a44a5f0fd68f99866bae37cf320
3
+ metadata.gz: 7895360323ba09f09b55f58dea945f55788c670f
4
+ data.tar.gz: a6271ef03712e14a098d51b9da697bd6c19cd59e
5
5
  SHA512:
6
- metadata.gz: f01b912964d9e43f8006cd54c7f945609a9b3a28b4afcbcb002aca8b6703d47b802d1b05ecaac4aff3f5aefe2753bd4348f6211eb964f0c90607771edc613b59
7
- data.tar.gz: 02a2d24e3df1c23789d9ac2e54a7bc975fa2409e6d2d3e2440cb1c12816ac1953c81eebe2a600940899851cf736c95444952c67b9a66dc6d6c0e41fa3999cf6a
6
+ metadata.gz: a14f13b4bbf2dbc55752e938a9763afdb266840030d808b64cdb3a0a1467381416103af25bb166a629580105dc5addaa4de9ef26ca332700cc47f91ae2e7d9b2
7
+ data.tar.gz: d61dcaba0c28ebb714e7a02a8d378c5ad73e06d229cdc1c7b51c95a8f4109d4772131c036a5cb0cc50c1c860df9372f55c2b0419423181c54a3f88ccd0a88a3c
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /global_registry_models-*.gem
11
+ .DS-Store
@@ -0,0 +1,22 @@
1
+ module GlobalRegistryModels
2
+ module APIOperations
3
+ module Delete
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def delete(id)
8
+ GlobalRegistry::Entity.delete id
9
+ end
10
+ end
11
+
12
+ def delete
13
+ if id.present?
14
+ self.class.delete id
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_support'
2
+ require 'global_registry'
3
+ require 'global_registry_models/retryer'
4
+
5
+ module GlobalRegistryModels
6
+ module APIOperations
7
+ module Finders
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+
12
+ def all!(filters: nil, start_page: 1, per_page: nil, order: nil, fields: nil, ruleset: nil, max_attempts: 1)
13
+ GlobalRegistryModels::Collection.new(meta: {}, list: []).tap do |collection|
14
+ page_num = start_page
15
+ loop do
16
+ sub_collection = GlobalRegistryModels::Retryer.new(RestClient::InternalServerError, max_attempts: max_attempts).try do
17
+ self.search(filters: filters, page: page_num, per_page: per_page, order: order, fields: fields, ruleset: ruleset)
18
+ end
19
+ break if sub_collection.blank?
20
+ collection.concat sub_collection.all
21
+ page_num += 1
22
+ end
23
+ end
24
+ end
25
+
26
+ def find(id)
27
+ new GlobalRegistry::Entity.find(id)['entity'][name]
28
+ end
29
+
30
+ def page(page_number = 1)
31
+ search page: page_number
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,58 @@
1
+ module GlobalRegistryModels
2
+ module APIOperations
3
+ module Persistence
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def create!(attributes)
8
+ entity = new(attributes.with_indifferent_access.except(:id))
9
+ if entity.valid?
10
+ attribute_keys_to_create = attributes.keys.collect(&:to_sym) & writeable_attributes
11
+ create_attributes = entity.attributes.with_indifferent_access.slice(*attribute_keys_to_create)
12
+ new GlobalRegistry::Entity.post({ entity: { name => create_attributes }})['entity'][name]
13
+ else
14
+ raise GlobalRegistryModels::RecordInvalid.new
15
+ end
16
+ end
17
+
18
+ def create(attributes)
19
+ create! attributes
20
+ rescue GlobalRegistryModels::RecordInvalid
21
+ false
22
+ end
23
+
24
+ def update!(id, attributes)
25
+ entity = new(attributes)
26
+ if entity.valid?
27
+ attribute_keys_to_update = (attributes.keys.collect(&:to_sym) << :client_integration_id) & writeable_attributes
28
+ update_attributes = entity.attributes.with_indifferent_access.slice(*attribute_keys_to_update)
29
+ new GlobalRegistry::Entity.put(id, { entity: { name => update_attributes }})['entity'][name]
30
+ else
31
+ raise GlobalRegistryModels::RecordInvalid.new
32
+ end
33
+ end
34
+
35
+ def update(id, attributes)
36
+ update! id, attributes
37
+ rescue GlobalRegistryModels::RecordInvalid
38
+ false
39
+ end
40
+ end
41
+
42
+ def save
43
+ if valid?
44
+ result = id.present? ? self.class.update(id, attributes) : self.class.create(attributes)
45
+ result ? self.id = result.id : false
46
+ else
47
+ false
48
+ end
49
+ end
50
+
51
+ def update!(update_attributes)
52
+ self.class.update! id, { client_integration_id: client_integration_id }.with_indifferent_access.merge(update_attributes)
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,44 @@
1
+ module GlobalRegistryModels
2
+ module APIOperations
3
+ module Search
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+
8
+ def search(filters: nil, page: nil, per_page: nil, order: nil, fields: nil, ruleset: nil)
9
+ #params = search_params(filters, name, page, per_page, order, fields, ruleset)
10
+
11
+ params = search_params.merge({
12
+ page: page,
13
+ per_page: per_page,
14
+ order: order,
15
+ fields: fields,
16
+ ruleset: ruleset
17
+ }).delete_if { |_, v| v.blank? }
18
+
19
+ params = clean_params(filters, params)
20
+
21
+ response = GlobalRegistryModels::ResponseParser.new(global_registry_resource.get(params))
22
+
23
+ Collection.new meta: response.meta, list: response.objects
24
+ end
25
+
26
+ def clean_params( filters, params )
27
+ filters.try(:reject!) { |_, v| v.blank? }
28
+ if filters.present?
29
+ # We need to generate a hash like this: { 'filters[name]' => 'name query', 'filters[attribute][nested_attribute]' => 'nested_attribute query' }
30
+ # It just so happens we can use CGI::parse to do it.
31
+ filter_params_hash = CGI::parse({ filters: filters }.to_query)
32
+ filter_params_hash.each { |k, v| filter_params_hash[k] = v.first if v.is_a?(Array) } # CGI::parse returns values as arrays, we just want string values
33
+
34
+ params.merge! filter_params_hash
35
+ end
36
+
37
+ params
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,74 @@
1
+ require 'csv'
2
+
3
+ module GlobalRegistryModels
4
+ class Collection
5
+ include Enumerable
6
+ extend Forwardable
7
+
8
+ def_delegators :@list, :[], :size, :concat, :blank?, :present?
9
+
10
+ def initialize(meta:, list:)
11
+ @meta = meta
12
+ @list = list
13
+ end
14
+
15
+ def each
16
+ @list.each { |object| yield object }
17
+ end
18
+
19
+ def first
20
+ @list.first
21
+ end
22
+
23
+ def last
24
+ @list.last
25
+ end
26
+
27
+ def all
28
+ @list
29
+ end
30
+
31
+ def page
32
+ @meta['page']
33
+ end
34
+
35
+ def last_page?
36
+ !@meta['next_page']
37
+ end
38
+
39
+ def first_page?
40
+ page == 1
41
+ end
42
+
43
+ def next_page
44
+ last_page? ? nil : page + 1
45
+ end
46
+
47
+ def prev_page
48
+ first_page? ? nil : page - 1
49
+ end
50
+
51
+ def from
52
+ @meta['from']
53
+ end
54
+
55
+ def to
56
+ @meta['to']
57
+ end
58
+
59
+ def per
60
+ (to - from) + 1
61
+ end
62
+
63
+ def to_csv
64
+ CSV.generate do |csv|
65
+ attributes = @list.first.attributes.collect(&:first).sort
66
+ csv << attributes
67
+ @list.each do |entity|
68
+ csv << attributes.collect { |attribute| entity.attributes[attribute] }
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ # A base class providing CRUD for GlobalRegistry Entities.
2
+ # API doc at https://github.com/CruGlobal/global_registry_docs/wiki/Entities
3
+
4
+ require 'active_model'
5
+ require 'virtus'
6
+
7
+ module GlobalRegistryModels
8
+ class CommonBase
9
+ include ActiveModel::Model
10
+ include ActiveModel::Validations
11
+ include Virtus.model
12
+
13
+ include GlobalRegistryModels::APIOperations::Persistence
14
+ include GlobalRegistryModels::APIOperations::Finders
15
+ include GlobalRegistryModels::APIOperations::Search
16
+ include GlobalRegistryModels::APIOperations::Delete
17
+
18
+
19
+ attribute :id, String
20
+ attribute :client_integration_id, String
21
+
22
+ validates_presence_of :client_integration_id
23
+
24
+ def self.title
25
+ name.titleize
26
+ end
27
+
28
+ def self.attribute_names
29
+ attribute_set.collect(&:name)
30
+ end
31
+
32
+ def self.filterable_attributes
33
+ attribute_names - [:id]
34
+ end
35
+
36
+ def self.identifying_attributes
37
+ [:id]
38
+ end
39
+
40
+ def self.writeable_attributes
41
+ attribute_names - [:id]
42
+ end
43
+
44
+ # The name of the entity class. The entity name is required in the api responses and requests, hence the need for this class method.
45
+ def self.name
46
+ to_s.gsub(/.*::/, '').underscore
47
+ end
48
+
49
+ end
50
+ end
@@ -1,73 +1,22 @@
1
1
  # A base class providing CRUD for GlobalRegistry Entities.
2
2
  # API doc at https://github.com/CruGlobal/global_registry_docs/wiki/Entities
3
3
 
4
- require 'active_model'
5
- require 'virtus'
4
+
6
5
 
7
6
  module GlobalRegistryModels
8
7
  module Entity
9
- class Base
10
- include ActiveModel::Model
11
- include ActiveModel::Validations
12
- include Virtus.model
13
-
14
- include GlobalRegistryModels::Entity::APIOperations::Finders
15
- include GlobalRegistryModels::Entity::APIOperations::Search
16
- include GlobalRegistryModels::Entity::APIOperations::Delete
17
-
18
- attribute :id, String
19
- attribute :client_integration_id, String
8
+ class Base < CommonBase
20
9
 
21
- validates_presence_of :client_integration_id
22
-
23
- def self.title
24
- name.titleize
10
+ def self.search_params
11
+ {
12
+ entity_type: name
13
+ }
25
14
  end
26
15
 
27
- def self.attribute_names
28
- attribute_set.collect(&:name)
16
+ def self.global_registry_resource
17
+ GlobalRegistry::Entity
29
18
  end
30
-
31
- def self.filterable_attributes
32
- attribute_names
33
- end
34
-
35
- def self.identifying_attributes
36
- [:id]
37
- end
38
-
39
- def self.create(attributes)
40
- attributes = attributes.except(:id)
41
- if new(attributes).valid?
42
- new GlobalRegistry::Entity.post({ entity: { name => attributes }})['entity'][name]
43
- else
44
- false
45
- end
46
- end
47
-
48
- def self.update(id, attributes)
49
- attributes = attributes.except(:id)
50
- if new(attributes).valid?
51
- new GlobalRegistry::Entity.put(id, { entity: { name => attributes }})['entity'][name]
52
- else
53
- false
54
- end
55
- end
56
-
57
- # The name of the entity class. The entity name is required in the api responses and requests, hence the need for this class method.
58
- def self.name
59
- to_s.gsub(/.*::/, '').underscore
60
- end
61
-
62
- def save
63
- if valid?
64
- result = id.present? ? self.class.update(id, attributes) : self.class.create(attributes)
65
- result ? self.id = result.id : false
66
- else
67
- false
68
- end
69
- end
70
-
19
+
71
20
  end
72
21
  end
73
22
  end
@@ -0,0 +1,66 @@
1
+ require 'csv'
2
+
3
+ module GlobalRegistryModels
4
+ class EntityCollection < Collection
5
+ include Enumerable
6
+ extend Forwardable
7
+
8
+ def_delegators :list, :[], :size, :concat, :blank?, :present?
9
+
10
+ def initialize(meta:, list:)
11
+ @meta = meta
12
+ @list = list
13
+ end
14
+
15
+ def each
16
+ @entities.each { |entity| yield entity }
17
+ end
18
+
19
+ def all
20
+ @entities
21
+ end
22
+
23
+ def page
24
+ @meta['page']
25
+ end
26
+
27
+ def last_page?
28
+ !@meta['next_page']
29
+ end
30
+
31
+ def first_page?
32
+ page == 1
33
+ end
34
+
35
+ def next_page
36
+ last_page? ? nil : page + 1
37
+ end
38
+
39
+ def prev_page
40
+ first_page? ? nil : page - 1
41
+ end
42
+
43
+ def from
44
+ @meta['from']
45
+ end
46
+
47
+ def to
48
+ @meta['to']
49
+ end
50
+
51
+ def per
52
+ (to - from) + 1
53
+ end
54
+
55
+ def to_csv
56
+ CSV.generate do |csv|
57
+ attributes = @entities.first.attributes.collect(&:first).sort
58
+ csv << attributes
59
+ @entities.each do |entity|
60
+ csv << attributes.collect { |attribute| entity.attributes[attribute] }
61
+ end
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -1,13 +1,47 @@
1
1
  module GlobalRegistryModels
2
2
  module Entity
3
3
  class Ministry < Base
4
- attribute :country, String
4
+ attribute :abbreviation, String
5
+ attribute :address1, String
6
+ attribute :address2, String
5
7
  attribute :city, String
6
- attribute :is_active, String
8
+ attribute :country, String
9
+ attribute :currency_code, String
10
+ attribute :currency_symbol, String
11
+ attribute :dataserver_url, String
12
+ attribute :dta, String
13
+ attribute :fiscal_start_month, Integer
14
+ attribute :gma_status, String
15
+ attribute :has_ds, Boolean
16
+ attribute :has_gcm, Boolean
17
+ attribute :has_lim, Boolean
18
+ attribute :has_slm, Boolean
19
+ attribute :is_active, Boolean
20
+ attribute :is_fcx, Boolean
21
+ attribute :is_secure, Boolean
22
+ attribute :lane, String
23
+ attribute :last_dataserver_donation, String
24
+ attribute :last_dataserver_transaction, String
25
+ attribute :last_financial_report, String
26
+ attribute :lmi_hide, String
27
+ attribute :lmi_show, String
7
28
  attribute :location, String
29
+ attribute :location_zoom, Integer
30
+ attribute :logo_url, String
31
+ attribute :measurement_status, String
8
32
  attribute :min_code, String
9
- attribute :name, String
33
+ attribute :ministry_brand, String
34
+ attribute :ministry_scope, String
10
35
  attribute :ministry_type, String
36
+ attribute :name, String
37
+ attribute :note, String
38
+ attribute :phone, String
39
+ attribute :registered_name, String
40
+ attribute :sp_phone, String
41
+ attribute :stage, Integer
42
+ attribute :state, String
43
+ attribute :url, String
44
+ attribute :zip, String
11
45
 
12
46
  def self.identifying_attributes
13
47
  [:name, :country, :city, :min_code]
@@ -19,5 +53,3 @@ module GlobalRegistryModels
19
53
  end
20
54
  end
21
55
  end
22
-
23
-
@@ -35,7 +35,11 @@ module GlobalRegistryModels
35
35
  attribute :zip, String
36
36
 
37
37
  def self.identifying_attributes
38
- [:name, :country, :state, :state]
38
+ [:name, :country, :state]
39
+ end
40
+
41
+ def self.writeable_attributes
42
+ super - [:gate, :highest_offering, :institution_type, :is_closed]
39
43
  end
40
44
 
41
45
  def to_s
@@ -0,0 +1,21 @@
1
+ # A base class providing CRUD for GlobalRegistry Entities.
2
+ # API doc at https://github.com/CruGlobal/global_registry_docs/wiki/Entities
3
+
4
+
5
+ module GlobalRegistryModels
6
+ module EntityType
7
+ class Base < CommonBase
8
+
9
+ def self.search_params
10
+ {
11
+ field_type: "entity"
12
+ }
13
+ end
14
+
15
+ def self.global_registry_resource
16
+ GlobalRegistry::EntityType
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ module GlobalRegistryModels
2
+ module EntityType
3
+ class EntityType < Base
4
+ attribute :id, String
5
+ attribute :name, String
6
+ attribute :description, Boolean
7
+ attribute :is_editable, Boolean
8
+ attribute :field_type, String
9
+ attribute :data_visibility, String
10
+ attribute :enum_values, Array
11
+ attribute :unique_value, Boolean
12
+
13
+ def initialize(params = {})
14
+ super(params)
15
+ create_fields(params["fields"]) if params["fields"]
16
+ create_relationships(params["relationships"]) if params["relationships"]
17
+ end
18
+
19
+ def fields
20
+ @fields
21
+ end
22
+
23
+ def relationships
24
+ @relationships
25
+ end
26
+
27
+ def self.identifying_attributes
28
+ [:name, :description, :is_editable, :field_type, :data_visibility]
29
+ end
30
+
31
+ private
32
+
33
+ def create_fields fields
34
+ @fields=[]
35
+ fields.collect do |field|
36
+ @fields << Field.new(field)
37
+ end
38
+ end
39
+
40
+ def create_relationships relationships
41
+ @relationships=[]
42
+ relationships.collect do |relationship|
43
+ @relationships << GlobalRegistryModels::RelationshipType::RelationshipType.new(relationship["relationship_type"])
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,34 @@
1
+ module GlobalRegistryModels
2
+ module EntityType
3
+ class Field < Base
4
+ attribute :id, String
5
+ attribute :name, String
6
+ attribute :description, String
7
+ attribute :is_editable, Boolean
8
+ attribute :field_type, String
9
+
10
+ def initialize(params = {})
11
+ super(params)
12
+ create_children(params["fields"]) if params["fields"]
13
+ end
14
+
15
+ def fields
16
+ @fields
17
+ end
18
+
19
+ def self.identifying_attributes
20
+ [:name, :description, :is_editable, :field_type]
21
+ end
22
+
23
+ private
24
+
25
+ def create_children fields
26
+ @fields=[]
27
+ fields.collect do |field|
28
+ @fields << Field.new(field)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module GlobalRegistryModels
2
+ class RecordInvalid < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ # A base class providing CRUD for GlobalRegistry Entities.
2
+ # API doc at https://github.com/CruGlobal/global_registry_docs/wiki/Entities
3
+
4
+
5
+ module GlobalRegistryModels
6
+ module MeasurementType
7
+ class Base < CommonBase
8
+
9
+ def self.search_params
10
+ {}
11
+ end
12
+
13
+ def self.global_registry_resource
14
+ GlobalRegistry::MeasurementType
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module GlobalRegistryModels
2
+ module MeasurementType
3
+ class MeasurementType < Base
4
+ attribute :id, String
5
+ attribute :name, String
6
+ attribute :description, String
7
+ attribute :related_entity_type_id, String
8
+ attribute :frequency, String
9
+ attribute :perm_link, String
10
+ attribute :unit, String
11
+
12
+ def self.identifying_attributes
13
+ [:name, :description, :frequency, :perm_link, :unit]
14
+ end
15
+
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # A base class providing CRUD for GlobalRegistry Entities.
2
+ # API doc at https://github.com/CruGlobal/global_registry_docs/wiki/Entities
3
+
4
+
5
+ module GlobalRegistryModels
6
+ module RelationshipType
7
+ class Base < CommonBase
8
+
9
+ def self.search_params
10
+ {}
11
+ end
12
+
13
+ def self.global_registry_resource
14
+ GlobalRegistry::RelationshipType
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module GlobalRegistryModels
2
+ module RelationshipType
3
+ class InvolvedType < Base
4
+
5
+ attribute :entity_type, String
6
+ attribute :relationship_name, String
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ module GlobalRegistryModels
2
+ module RelationshipType
3
+ class RelationshipType < Base
4
+ attribute :id, String
5
+
6
+ def initialize(params = {})
7
+ super(params)
8
+ create_involved_types([params["relationship1"],params["relationship2"]]) if params["relationship1"] && params["relationship2"]
9
+ end
10
+
11
+ def involved_types
12
+ @involved_types
13
+ end
14
+
15
+ private
16
+
17
+ def create_involved_types relationships
18
+ @involved_types=[]
19
+ relationships.each do |relationship|
20
+ @involved_types << InvolvedType.new(relationship)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -9,20 +9,29 @@ module GlobalRegistryModels
9
9
  @response_hash['meta']
10
10
  end
11
11
 
12
- def entities
13
- @entities ||= build_entities
12
+ def objects
13
+ @objects ||= build_entities if @response_hash['entities']
14
+ @objects ||= build_other_types unless @response_hash['entities']
15
+ @objects
14
16
  end
15
17
 
16
18
  private
17
19
 
18
20
  def build_entities
19
- @response_hash['entities'].collect(&:flatten).collect do |entity_type, entity_attributes|
20
- entity_class(entity_type).new(entity_attributes)
21
+ @response_hash['entities'].collect(&:flatten).collect do |object_type, object_attributes|
22
+ entity_class('Entity', object_type).new(object_attributes)
21
23
  end
22
24
  end
23
25
 
24
- def entity_class(entity_type_string)
25
- "GlobalRegistryModels::Entity::#{ entity_type_string.classify }".constantize
26
+ def build_other_types
27
+ objects_type, objects = @response_hash.first
28
+ objects.collect do |object_attributes|
29
+ entity_class(objects_type, objects_type).new(object_attributes)
30
+ end
31
+ end
32
+
33
+ def entity_class(module_type, object_type)
34
+ return "GlobalRegistryModels::#{ module_type.classify }::#{ object_type.classify }".constantize
26
35
  end
27
36
  end
28
37
  end
@@ -1,3 +1,3 @@
1
1
  module GlobalRegistryModels
2
- VERSION = "0.1.0"
2
+ VERSION = '0.7.0'
3
3
  end
@@ -4,18 +4,34 @@ require 'global_registry_models/version'
4
4
 
5
5
  require 'global_registry_models/response_parser'
6
6
  require 'global_registry_models/retryer'
7
+ require 'global_registry_models/collection'
7
8
 
8
- require 'global_registry_models/entity/api_operations/finders'
9
- require 'global_registry_models/entity/api_operations/search'
10
- require 'global_registry_models/entity/api_operations/delete'
9
+ require 'global_registry_models/errors/record_invalid'
10
+
11
+ require 'global_registry_models/api_operations/persistence'
12
+ require 'global_registry_models/api_operations/finders'
13
+ require 'global_registry_models/api_operations/search'
14
+ require 'global_registry_models/api_operations/delete'
15
+
16
+ require 'global_registry_models/common_base'
11
17
 
12
18
  require 'global_registry_models/entity/base'
13
19
  require 'global_registry_models/entity/area'
14
- require 'global_registry_models/entity/collection'
15
20
  require 'global_registry_models/entity/global_mcc'
16
21
  require 'global_registry_models/entity/iso_country'
17
22
  require 'global_registry_models/entity/ministry'
18
23
  require 'global_registry_models/entity/target_area'
19
24
 
25
+ require 'global_registry_models/entity_type/base'
26
+ require 'global_registry_models/entity_type/entity_type'
27
+ require 'global_registry_models/entity_type/field'
28
+
29
+ require 'global_registry_models/relationship_type/base'
30
+ require 'global_registry_models/relationship_type/relationship_type'
31
+ require 'global_registry_models/relationship_type/involved_type'
32
+
33
+ require 'global_registry_models/measurement_type/base'
34
+ require 'global_registry_models/measurement_type/measurement_type'
35
+
20
36
  module GlobalRegistryModels
21
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: global_registry_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Dueck
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-09 00:00:00.000000000 Z
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -142,16 +142,33 @@ files:
142
142
  - bin/setup
143
143
  - global_registry_models.gemspec
144
144
  - lib/global_registry_models.rb
145
- - lib/global_registry_models/entity/api_operations/delete.rb
146
- - lib/global_registry_models/entity/api_operations/finders.rb
147
- - lib/global_registry_models/entity/api_operations/search.rb
145
+ - lib/global_registry_models/.DS_Store
146
+ - lib/global_registry_models/api_operations/delete.rb
147
+ - lib/global_registry_models/api_operations/finders.rb
148
+ - lib/global_registry_models/api_operations/persistence.rb
149
+ - lib/global_registry_models/api_operations/search.rb
150
+ - lib/global_registry_models/collection.rb
151
+ - lib/global_registry_models/common_base.rb
152
+ - lib/global_registry_models/entity/.DS_Store
148
153
  - lib/global_registry_models/entity/area.rb
149
154
  - lib/global_registry_models/entity/base.rb
150
- - lib/global_registry_models/entity/collection.rb
155
+ - lib/global_registry_models/entity/entity_collection.rb
151
156
  - lib/global_registry_models/entity/global_mcc.rb
152
157
  - lib/global_registry_models/entity/iso_country.rb
153
158
  - lib/global_registry_models/entity/ministry.rb
154
159
  - lib/global_registry_models/entity/target_area.rb
160
+ - lib/global_registry_models/entity_type/.DS_Store
161
+ - lib/global_registry_models/entity_type/base.rb
162
+ - lib/global_registry_models/entity_type/entity_type.rb
163
+ - lib/global_registry_models/entity_type/field.rb
164
+ - lib/global_registry_models/errors/record_invalid.rb
165
+ - lib/global_registry_models/measurement_type/.DS_Store
166
+ - lib/global_registry_models/measurement_type/base.rb
167
+ - lib/global_registry_models/measurement_type/measurement_type.rb
168
+ - lib/global_registry_models/relationship_type/.DS_Store
169
+ - lib/global_registry_models/relationship_type/base.rb
170
+ - lib/global_registry_models/relationship_type/involved_type.rb
171
+ - lib/global_registry_models/relationship_type/relationship_type.rb
155
172
  - lib/global_registry_models/response_parser.rb
156
173
  - lib/global_registry_models/retryer.rb
157
174
  - lib/global_registry_models/version.rb
@@ -1,24 +0,0 @@
1
- module GlobalRegistryModels
2
- module Entity
3
- module APIOperations
4
- module Delete
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
8
- def delete(id)
9
- GlobalRegistry::Entity.delete id
10
- end
11
- end
12
-
13
- def delete
14
- if id.present?
15
- self.class.delete id
16
- else
17
- false
18
- end
19
- end
20
-
21
- end
22
- end
23
- end
24
- end
@@ -1,39 +0,0 @@
1
- require 'active_support'
2
- require 'global_registry'
3
- require 'global_registry_models/retryer'
4
-
5
- module GlobalRegistryModels
6
- module Entity
7
- module APIOperations
8
- module Finders
9
- extend ActiveSupport::Concern
10
-
11
- module ClassMethods
12
-
13
- def all!(filters: nil, start_page: 1, per_page: nil, order: nil, fields: nil, ruleset: nil)
14
- GlobalRegistryModels::Entity::Collection.new(meta: {}, entities: []).tap do |collection|
15
- page_num = start_page
16
- loop do
17
- sub_collection = GlobalRegistryModels::Retryer.new(RestClient::InternalServerError).try do
18
- self.search(filters: filters, page: page_num, per_page: per_page, order: order, fields: fields, ruleset: ruleset)
19
- end
20
- break if sub_collection.blank?
21
- collection.concat sub_collection.all
22
- page_num += 1
23
- end
24
- end
25
- end
26
-
27
- def find(id)
28
- new GlobalRegistry::Entity.find(id)['entity'][name]
29
- end
30
-
31
- def page(page_number = 1)
32
- search page: page_number
33
- end
34
-
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,37 +0,0 @@
1
- module GlobalRegistryModels
2
- module Entity
3
- module APIOperations
4
- module Search
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
8
-
9
- def search(filters: nil, page: nil, per_page: nil, order: nil, fields: nil, ruleset: nil)
10
- params = {
11
- entity_type: name,
12
- page: page,
13
- per_page: per_page,
14
- order: order,
15
- fields: fields,
16
- ruleset: ruleset
17
- }.delete_if { |_, v| v.blank? }
18
-
19
- filters.try(:reject!) { |_, v| v.blank? }
20
- if filters.present?
21
- # We need to generate a hash like this: { 'filters[name]' => 'name query', 'filters[attribute][nested_attribute]' => 'nested_attribute query' }
22
- # It just so happens we can use CGI::parse to do it.
23
- filter_params_hash = CGI::parse({ filters: filters }.to_query)
24
- filter_params_hash.each { |k, v| filter_params_hash[k] = v.first if v.is_a?(Array) } # CGI::parse returns values as arrays, we just want string values
25
-
26
- params.merge! filter_params_hash
27
- end
28
-
29
- response = GlobalRegistryModels::ResponseParser.new Retryer.new(RestClient::InternalServerError).try { GlobalRegistry::Entity.get(params) }
30
- Entity::Collection.new meta: response.meta, entities: response.entities
31
- end
32
-
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,68 +0,0 @@
1
- require 'csv'
2
-
3
- module GlobalRegistryModels
4
- module Entity
5
- class Collection
6
- include Enumerable
7
- extend Forwardable
8
-
9
- def_delegators :@entities, :[], :size, :concat, :blank?, :present?
10
-
11
- def initialize(meta:, entities:)
12
- @meta = meta
13
- @entities = entities
14
- end
15
-
16
- def each
17
- @entities.each { |entity| yield entity }
18
- end
19
-
20
- def all
21
- @entities
22
- end
23
-
24
- def page
25
- @meta['page']
26
- end
27
-
28
- def last_page?
29
- !@meta['next_page']
30
- end
31
-
32
- def first_page?
33
- page == 1
34
- end
35
-
36
- def next_page
37
- last_page? ? nil : page + 1
38
- end
39
-
40
- def prev_page
41
- first_page? ? nil : page - 1
42
- end
43
-
44
- def from
45
- @meta['from']
46
- end
47
-
48
- def to
49
- @meta['to']
50
- end
51
-
52
- def per
53
- (to - from) + 1
54
- end
55
-
56
- def to_csv
57
- CSV.generate do |csv|
58
- attributes = @entities.first.attributes.collect(&:first).sort
59
- csv << attributes
60
- @entities.each do |entity|
61
- csv << attributes.collect { |attribute| entity.attributes[attribute] }
62
- end
63
- end
64
- end
65
-
66
- end
67
- end
68
- end