appfuel 0.2.11 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 684209e19f1a18b1b855531cc3cfa8a3780add38
4
- data.tar.gz: fa2a53437d5dc7c08dc8364bb83a4bd6bf2ee5d6
3
+ metadata.gz: 4cd3911f6f1983c5d75076fe3f2c8187227689ce
4
+ data.tar.gz: dddd8acaac5dd41bf98b6ce7f7ba5d6c481c23c7
5
5
  SHA512:
6
- metadata.gz: e16e8282f66922d69856c3c693ab007984a319898248392210de9412c4ace6e2b78d6798ecb2bcfea84f566fadf5dc0eaed10e71a6721e7dc1346f1f44c52a95
7
- data.tar.gz: 51369a25b4359e1bd30dd8162a3303e9685a6e953a3e435dd5124d032d33fc6e0311bdbe2ca84239bdd7a368b6b254510166470e9f689977e25ab72f5e1bb6b7
6
+ metadata.gz: ed43fb31b3cd2a2d09db565d9a70a017c0d6f9b88d1f48a7ad9e8e383e720662cc0b475bf6a9f6ed438d05233b4b9284eb7ade4519a5f64cd871fce0038ac130
7
+ data.tar.gz: 4fa72c3fdd7c070cf484c63bee167d126144d93d311577f4d2697ce8d0ef4a570f40f8da2cb41dba9cf8a54afda3aca53ed246e8c909c2e9b84b19bac121f54c
data/CHANGELOG.md CHANGED
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. (Pending ap
5
5
 
6
6
 
7
7
  # Releases
8
+ ## [[0.3.0]](https://github.com/rsb/appfuel/releases/tag/0.3.0) 2017-06-20
9
+ ### Added
10
+ - adding `web_api` storage to be used for WebApi repositories. The default
11
+ http adapter will be `RestClient`
12
+
8
13
  ## [[0.2.11]](https://github.com/rsb/appfuel/releases/tag/0.2.9) 2017-06-15
9
14
  ### Changed
10
15
  - Adding error handling to dispatching. Errors are converted to responses
data/appfuel.gemspec CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_dependency "dry-monads", "~> 0.2"
32
32
  spec.add_dependency "dry-configurable", "~> 0.6"
33
33
  spec.add_dependency "parslet", "~> 1.8.0"
34
+ spec.add_dependency "rest-client", "~> 2.0"
34
35
 
35
36
  spec.add_development_dependency "bundler", "~> 1.13"
36
37
  spec.add_development_dependency "rake", "~> 10.0"
@@ -0,0 +1,59 @@
1
+ require 'uri'
2
+
3
+ module Appfuel
4
+ module WebApi
5
+ class HttpModel
6
+ include Appfuel::Application::AppContainer
7
+ class << self
8
+ def container_class_type
9
+ 'web_api'
10
+ end
11
+
12
+ def config_key(value = nil)
13
+ return @config_key if value.nil?
14
+ @config_key = value.to_sym
15
+ end
16
+
17
+ def load_config
18
+ config = app_container[:config]
19
+ unless config.key?(config_key)
20
+ fail "[web_api] config key (#{config_key}) not found - #{self}"
21
+ end
22
+ config[config_key]
23
+ end
24
+
25
+ def load_http_adapter
26
+ container = app_container
27
+ if container.key?('web_api.http_adapter')
28
+ container['web_api.http_adapter']
29
+ else
30
+ load_default_http_adapter
31
+ end
32
+ end
33
+
34
+ def load_default_http_adapter
35
+ unless Kernel.const_defined?(:RestClient)
36
+ require 'rest-client'
37
+ end
38
+ RestClient
39
+ end
40
+
41
+ def inherited(klass)
42
+ stage_class_for_registration(klass)
43
+ end
44
+ end
45
+
46
+ attr_reader :config, :url, :adapter
47
+
48
+ def initialize
49
+ @config = self.class.load_config
50
+ unless @config.key?(:url)
51
+ fail "[web_api initialize] config is missing :url"
52
+ end
53
+ @url = URI(@config[:url])
54
+ @adapter = self.class.load_http_adapter
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,148 @@
1
+ module Appfuel
2
+ module WebApi
3
+ class Repository < Appfuel::Repository::Base
4
+ class << self
5
+ def container_class_type
6
+ "#{super}.web_api"
7
+ end
8
+ end
9
+
10
+ attr_reader :response_handler
11
+
12
+ def create(entity, exclude = [])
13
+ =begin
14
+ data = to_storage(entity, exclude: ['id'])
15
+ results = []
16
+ data.each do |api_class_key, mapped|
17
+ api_model = api_class(api_class_key)
18
+ results << api_model.create(mapped)
19
+ end
20
+
21
+ build(name: entity.domain_name, storage: db_results, type: :web_api)
22
+ =end
23
+ end
24
+
25
+ # when key has no . assume the feature of the repository
26
+ #
27
+ #
28
+ def api_class(key)
29
+ unless key.include?('.')
30
+ key = "features.#{self.class.container_feature_name}.web_api.#{key}"
31
+ end
32
+ app_container[key]
33
+ end
34
+
35
+ # Handles the treatment of the relation when the recordset is empty
36
+ # based on the criteria.
37
+ #
38
+ # @param criteria [SpCore::Criteria]
39
+ # @return [SpCore::Error, SpCore::Domain::EntityNotFound, nil]
40
+ def handle_empty_relation(criteria, relation)
41
+ return nil unless relation.blank?
42
+
43
+ if criteria.error_on_empty_dataset?
44
+ return error(criteria.domain => ["#{criteria.domain} not found"])
45
+ end
46
+
47
+ if criteria.single?
48
+ return create_entity_not_found(criteria)
49
+ end
50
+ end
51
+
52
+ # Null object used when you can not find a given entity
53
+ #
54
+ # @param criteria [SpCore::Criteria]
55
+ # @return SpCore::Domain::EntityNotFound
56
+ def create_entity_not_found(criteria)
57
+ Appfuel::Domain::EntityNotFound.new(entity_name: criteria.domain_name)
58
+ end
59
+
60
+ # Apply where, order and limit clauses to the relation based on the
61
+ # criteria.
62
+ #
63
+ # @param criteria [SpCore::Criteria]
64
+ # @param relation [mixed]
65
+ # @return relation
66
+ def apply_query_conditions(criteria, relation, _settings)
67
+
68
+ end
69
+
70
+ # Determines which query conditions to apply to the relation
71
+ #
72
+ # @param criteria [SpCore::Criteria]
73
+ # @param relation
74
+ # @return relation
75
+ def handle_query_conditions(criteria, relation, settings)
76
+ if settings.all?
77
+ return order(criteria, relation.all)
78
+ end
79
+
80
+ apply_query_conditions(criteria, relation, settings)
81
+ end
82
+
83
+ def handle_empty_relation(criteria, relation, settings)
84
+ unless relation.respond_to?(:blank?)
85
+ fail "The database relation invalid, does not implement :blank?"
86
+ end
87
+
88
+ return nil unless relation.blank?
89
+
90
+ if criteria.error_on_empty_dataset?
91
+ return domain_not_found_error(criteria)
92
+ end
93
+
94
+ if criteria.single?
95
+ return domain_not_found(criteria)
96
+ end
97
+ end
98
+
99
+ # 1) lookup query id in cache
100
+ # if found build collection from cached query ids
101
+ #
102
+ # 2) query id not found in cache
103
+ # a) assign query id from criteria
104
+ # b) find the domain builder for that criteria
105
+ # c) loop through each item in the database relation
106
+ # d) build an domain from each record in the relation
107
+ # e) create cache id from the domain
108
+ # f) record cache id into a list that represents query
109
+ # g) assign domain into the cache with its cache id
110
+ # id is in the cache the its updated *represents a miss
111
+ # h) assign the query list into the cache with its query id
112
+ #
113
+ def build_domains(criteria, relation, settings)
114
+ result = handle_empty_relation(criteria, relation, settings)
115
+ return result if result
116
+
117
+
118
+ if settings.single?
119
+ method = settings.first? ? :first : :last
120
+ db_model = relation.send(method)
121
+ builder = domain_builder(criteria.domain_name)
122
+ domain = builder.call(db_model, criteria)
123
+ ap domain
124
+ end
125
+
126
+ end
127
+
128
+ def domain_builder(domain_name)
129
+ key = qualify_container_key(domain_name, 'domain_builders.db')
130
+ app_container[key]
131
+ end
132
+
133
+ private
134
+
135
+ def raise_error(err, message)
136
+ error = RuntimeError.new(message)
137
+ error.set_backtrace(err.backtrace)
138
+ raise error
139
+ end
140
+
141
+ def validate_entity_id(entity)
142
+ if entity.id == Types::Undefined
143
+ fail("entity id is #{entity.id}")
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'web_api/repository'
2
+ require_relative 'web_api/http_model'
@@ -2,3 +2,4 @@ require_relative 'storage/repository'
2
2
  require_relative 'storage/file'
3
3
  require_relative 'storage/memory'
4
4
  require_relative 'storage/db'
5
+ require_relative 'storage/web_api'
@@ -1,3 +1,3 @@
1
1
  module Appfuel
2
- VERSION = "0.2.11"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appfuel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Scott-Buccleuch
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-15 00:00:00.000000000 Z
11
+ date: 2017-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 1.8.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rest-client
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: bundler
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -339,6 +353,9 @@ files:
339
353
  - lib/appfuel/storage/repository/search_parser.rb
340
354
  - lib/appfuel/storage/repository/search_transform.rb
341
355
  - lib/appfuel/storage/repository/settings.rb
356
+ - lib/appfuel/storage/web_api.rb
357
+ - lib/appfuel/storage/web_api/http_model.rb
358
+ - lib/appfuel/storage/web_api/repository.rb
342
359
  - lib/appfuel/types.rb
343
360
  - lib/appfuel/validation.rb
344
361
  - lib/appfuel/validation/validator.rb