glimmer-dsl-web 0.8.8 → 0.8.9

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
  SHA256:
3
- metadata.gz: db73018239b32a5d5ef65132760f1df53fe162e8199468b674d0a3e4fd675863
4
- data.tar.gz: f188ededffff10a18faebbdf370967fed8d0ffc1b220289524d659e61b694c31
3
+ metadata.gz: b97d0d0e09800873120d29ac0948ec4080ea88a183a6b949633deeaafd141d31
4
+ data.tar.gz: 0fa0e8210d1411317b504b643b5874e966bb38c580f45a7b6b54f5e56e248450
5
5
  SHA512:
6
- metadata.gz: 9f1bc0048487b56d1b58a6f4ccf784b3ab3ceb8c5cc97aad007f2ae1a1aec666e09a18d4261109cb12dab99bbae3aae2bd111dcba25670ae94db24569e473266
7
- data.tar.gz: f3715134efe3b06bb2677e15939a23d097a9d825afd99d42275fa350c0097b9bd4bf6f7fb0ed64e64193e2e44151b50caa92ce995e127aab389997cff9445046
6
+ metadata.gz: c2883556cc96742480f175155f6ac7dfe38467b4421211b12233ab6c3b8012db9b45646f0c9b01d9cf1c9f066ea4c80333e2d239507d0d4bf870e8fdf730cae1
7
+ data.tar.gz: e71fb15e206fe173253016e74e5ea5e85e5e72765a5a63303f332b853dfcc3f52a3b4cdb6f5d87472147409f89cfb702ce88933ebf4686a225460bd5c166ba15
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.8.9
4
+
5
+ - `Rails::ResourceService.index` supports `root` argument to be able to handle responses that include resources + metadata (e.g. pagination info), providing |response, resources, metadata| to response handler block.
6
+ - `Glimmer::Util::UrlQueryStringBuilder` supports `param`, `params`, and `query` to build a URL query string
7
+ - Fix issue in `Rails::ResourceService.index`, `Rails::ResourceService.show`, `Rails::ResourceService.destroy` with incorrectly passing in query params via payload instead of query string
8
+
3
9
  ## 0.8.8
4
10
 
5
11
  - `Glimmer::Util::UrlBuilder` supports `params` to set multiple params at once as a hash
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Web 0.8.8 (Beta)
1
+ # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Web 0.8.9 (Beta)
2
2
  ## Ruby-in-the-Browser Web Frontend Framework
3
3
  ### The "Rails" of Frontend Frameworks!!! ([Fukuoka Award Winning](https://andymaleh.blogspot.com/2025/01/glimmer-dsl-for-web-wins-in-fukuoka.html))
4
4
  #### Finally, Ruby Developer Productivity, Happiness, and Fun in the Frontend!!!
@@ -1411,7 +1411,7 @@ Please follow these steps to setup.
1411
1411
  Install a Rails 7 gem:
1412
1412
 
1413
1413
  ```
1414
- gem install rails -v7.0.8.8
1414
+ gem install rails -v7.0.8.9
1415
1415
  ```
1416
1416
 
1417
1417
  Start a new Rails 7 app:
@@ -1423,7 +1423,7 @@ rails new glimmer_app_server
1423
1423
  Add the following to `Gemfile`:
1424
1424
 
1425
1425
  ```
1426
- gem 'glimmer-dsl-web', '~> 0.8.8'
1426
+ gem 'glimmer-dsl-web', '~> 0.8.9'
1427
1427
  ```
1428
1428
 
1429
1429
  Run:
@@ -1726,7 +1726,7 @@ the `Rails::ResourceService` class source code to find out what its API is. It c
1726
1726
  if Developers would rather not write the Backend by hand.
1727
1727
 
1728
1728
  `Rails::ResourceService` API:
1729
- - `index(resource: nil, resource_class: nil, singular_resource_name: nil, plural_resource_name: nil, index_resource_url: nil, params: nil) { |response, resources| ... }`
1729
+ - `index(resource: nil, resource_class: nil, singular_resource_name: nil, plural_resource_name: nil, index_resource_url: nil, params: nil, root: nil) { |response, resources, metadata| ... }`
1730
1730
  - `show(resource: nil, resource_class: nil, resource_id: nil, singular_resource_name: nil, plural_resource_name: nil, show_resource_url: nil, params: nil) { |response, resource| ... }`
1731
1731
  - `create(resource: nil, resource_class: nil, resource_attributes: nil, singular_resource_name: nil, plural_resource_name: nil, create_resource_url: nil, params: nil) { |response, created_resource, errors| ... }`
1732
1732
  - `update(resource: nil, resource_class: nil, resource_id: nil, resource_attributes: nil, singular_resource_name: nil, plural_resource_name: nil, update_resource_url: nil, params: nil) { |response, updated_resource, errors| ... }`
@@ -1735,6 +1735,7 @@ if Developers would rather not write the Backend by hand.
1735
1735
  `Rails::ResourceService` follows the 'Convention over Configuration' Rails principle as it auto-derives the URL to call based on the `resource` class and data.
1736
1736
  `resource` can be a Ruby class with attribute method declarations or `attr_accessor` declarations, or a Ruby `Struct` class.
1737
1737
  `Rails::ResourceService` is flexible enough to work with all options.
1738
+ If you are making an `index` request that receives pagination `metadata`, make sure to specify the `root` argument as `true` (to determine root key by convention) or by specifying a custom `root` explicitly.
1738
1739
 
1739
1740
  Examples from [ContactPresenter](https://github.com/AndyObtiva/sample-glimmer-dsl-web-rails7-app/blob/master/app/assets/opal/contact_manager/presenters/contact_presenter.rb) in the [Contact Manager](#contact-manager) sample:
1740
1741
 
@@ -1776,7 +1777,7 @@ Note that there are alternative ways of invoking the `Rails::ResourceService.upd
1776
1777
 
1777
1778
  ### Glimmer::Util::UrlBuilder
1778
1779
 
1779
- `Glimmer::Util::UrlBuilder` is a convenience class that facilitate building URLs (with protocol scheme, host, path, query params, and fragment)
1780
+ `Glimmer::Util::UrlBuilder` is a convenience class that facilitates building URLs (with protocol scheme, host, path, query params, and fragment)
1780
1781
 
1781
1782
  First, make sure to require the class file:
1782
1783
  ```ruby
@@ -1796,6 +1797,21 @@ Glimmer::Util::UrlBuilder.current.param('year', '2036').to_url
1796
1797
  # => 'http://www.google.com/results?q=sports&year=2036'
1797
1798
  ```
1798
1799
 
1800
+ ### Glimmer::Util::UrlQueryStringBuilder
1801
+
1802
+ `Glimmer::Util::UrlQueryStringBuilder` is a convenience class that facilitates building URL query strings
1803
+
1804
+ First, make sure to require the class file:
1805
+ ```ruby
1806
+ require 'glimmer/util/url_builder'
1807
+ ```
1808
+
1809
+ Usage:
1810
+ ```ruby
1811
+ subject.with_question_mark.param('q', 'sports').param('year', '2040').to_s
1812
+ # => '?q=sports&year=2040'
1813
+ ```
1814
+
1799
1815
  ## Supported Glimmer DSL Keywords
1800
1816
 
1801
1817
  [All HTML elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element), following the Ruby method name standard of lowercase and underscored names.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.8
1
+ 0.8.9
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: glimmer-dsl-web 0.8.8 ruby lib
5
+ # stub: glimmer-dsl-web 0.8.9 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "glimmer-dsl-web".freeze
9
- s.version = "0.8.8".freeze
9
+ s.version = "0.8.9".freeze
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
@@ -90,6 +90,7 @@ Gem::Specification.new do |s|
90
90
  "lib/glimmer/helpers/glimmer_helper.rb",
91
91
  "lib/glimmer/util/proc_tracker.rb",
92
92
  "lib/glimmer/util/url_builder.rb",
93
+ "lib/glimmer/util/url_query_string_builder.rb",
93
94
  "lib/glimmer/web.rb",
94
95
  "lib/glimmer/web/component.rb",
95
96
  "lib/glimmer/web/component/component_style_container.rb",
@@ -22,6 +22,8 @@
22
22
  # TODO consider extracting this class into its own Ruby gem
23
23
  require 'delegate'
24
24
 
25
+ require 'glimmer/util/url_query_string_builder'
26
+
25
27
  module Glimmer
26
28
  module Util
27
29
  class UrlBuilder
@@ -113,9 +115,7 @@ module Glimmer
113
115
  alias build to_url
114
116
 
115
117
  def compute_query
116
- computed_query = @params.reject { |name, value| value.nil? || value.to_s.empty? }.map { |name, value| "#{name}=#{value}" }.join('&')
117
- computed_query = "?#{computed_query}" unless computed_query.empty?
118
- computed_query
118
+ UrlQueryStringBuilder.new.with_prefix.params(@params).to_s
119
119
  end
120
120
  end
121
121
  end
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2023-2026 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ # TODO consider extracting this class into its own Ruby gem
23
+ require 'delegate'
24
+
25
+ module Glimmer
26
+ module Util
27
+ class UrlQueryStringBuilder
28
+ class << self
29
+ def current
30
+ new.query($$.document.location.search) if RUBY_ENGINE == 'opal'
31
+ end
32
+ end
33
+
34
+ def initialize
35
+ @params = {}
36
+ end
37
+
38
+ def with_question_mark
39
+ @with_question_mark = true
40
+ self
41
+ end
42
+ alias with_prefix with_question_mark
43
+
44
+ def param(name, value)
45
+ @params[name.to_s] = value
46
+ self
47
+ end
48
+
49
+ def params(params_hash)
50
+ @params = @params.merge(params_hash)
51
+ self
52
+ end
53
+
54
+ def query(value)
55
+ return self if value.nil?
56
+
57
+ value = value.sub('?', '') if value.start_with?('?')
58
+ value.split('&').each do |param_pair|
59
+ name, value = param_pair.split('=')
60
+ @params[name.to_s] = value
61
+ end
62
+ self
63
+ end
64
+
65
+ def to_query_string
66
+ computed_query = @params.reject { |name, value| value.nil? || value.to_s.empty? }.map { |name, value| "#{name}=#{value}" }.join('&')
67
+ computed_query = "?#{computed_query}" if @with_question_mark && !computed_query.empty?
68
+ computed_query
69
+ end
70
+ alias to_s to_query_string
71
+ alias build to_query_string
72
+ end
73
+ end
74
+ end
@@ -1,4 +1,5 @@
1
1
  require 'facets/string/underscore'
2
+ require 'glimmer/util/url_builder'
2
3
 
3
4
  module Rails
4
5
  # ResourceService supports index/show/create/update/destroy REST API calls for any Rails resources
@@ -8,19 +9,27 @@ module Rails
8
9
  TIMESTAMP_ATTRIBUTES = ['created_at', 'updated_at']
9
10
 
10
11
  class << self
11
- def index(resource: nil, resource_class: nil, singular_resource_name: nil, plural_resource_name: nil, index_resource_url: nil, params: nil, &response_handler)
12
+ def index(resource: nil, resource_class: nil, singular_resource_name: nil, plural_resource_name: nil, index_resource_url: nil, params: nil, root: nil, &response_handler)
12
13
  resource_class ||= resource&.class
13
14
  singular_resource_name ||= singular_resource_name_for_resource_class(resource_class)
14
15
  plural_resource_name ||= "#{singular_resource_name}s"
15
16
  index_resource_url ||= "/#{plural_resource_name}.json"
16
- HTTP.get(index_resource_url, payload: index_show_destroy_resource_params(params: params.to_h)) do |response|
17
+ root = plural_resource_name if root == true
18
+ index_resource_url = Glimmer::Util::UrlBuilder.new.url(index_resource_url).params(index_show_destroy_resource_params(params: params.to_h)).to_s
19
+ HTTP.get(index_resource_url) do |response|
17
20
  if response.ok? && !resource_class.nil?
18
21
  resource_response_objects = Native(response.body)
19
- # TODO consider having index not make the assumpion that resource_response_objects is an array in case it isn't
20
- # when including pagination information (maybe test if it is an array first)
21
- resources = resource_response_objects.map { |resource_response_object| build_resource_from_response_object(resource_class:, resource_response_object:) }
22
+ metadata = {}
23
+ if root
24
+ resources = resource_response_objects[root].map { |resource_response_object| build_resource_from_response_object(resource_class:, resource_response_object:) }
25
+ populate_metadata_from_native_response(metadata:, native_response: resource_response_objects, root:)
26
+ else
27
+ resources = resource_response_objects.map { |resource_response_object| build_resource_from_response_object(resource_class:, resource_response_object:) }
28
+ end
22
29
  end
23
- response_handler.call(response, resources)
30
+ puts 'metadata'
31
+ puts metadata
32
+ response_handler.call(response, resources, metadata) # TODO support 3rd argument for metadata
24
33
  end
25
34
  end
26
35
 
@@ -30,7 +39,8 @@ module Rails
30
39
  singular_resource_name ||= singular_resource_name_for_resource_class(resource_class)
31
40
  plural_resource_name ||= "#{singular_resource_name}s"
32
41
  show_resource_url ||= "/#{plural_resource_name}/#{resource_id}.json"
33
- HTTP.get(show_resource_url, payload: index_show_destroy_resource_params(params: params.to_h)) do |response|
42
+ show_resource_url = Glimmer::Util::UrlBuilder.new.url(show_resource_url).params(index_show_destroy_resource_params(params: params.to_h)).to_s
43
+ HTTP.get(show_resource_url) do |response|
34
44
  if response.ok? && !resource_class.nil?
35
45
  resource_response_object = Native(response.body)
36
46
  resource = build_resource_from_response_object(resource_class:, resource_response_object:)
@@ -82,7 +92,8 @@ module Rails
82
92
  singular_resource_name ||= singular_resource_name_for_resource_class(resource_class)
83
93
  plural_resource_name ||= "#{singular_resource_name}s"
84
94
  destroy_resource_url ||= "/#{plural_resource_name}/#{resource_id}.json"
85
- HTTP.delete(destroy_resource_url, payload: index_show_destroy_resource_params(params: params.to_h), &response_handler)
95
+ destroy_resource_url = Glimmer::Util::UrlBuilder.new.url(destroy_resource_url).params(index_show_destroy_resource_params(params: params.to_h)).to_s
96
+ HTTP.delete(destroy_resource_url, &response_handler)
86
97
  end
87
98
 
88
99
  def index_show_destroy_resource_params(params: nil)
@@ -114,6 +125,23 @@ module Rails
114
125
  resource
115
126
  end
116
127
 
128
+ def populate_metadata_from_native_response(metadata:, native_response:, root:)
129
+ native_response.each do |attribute, value|
130
+ next if attribute == root
131
+ begin
132
+ value.class
133
+ metadata[attribute] = value
134
+ rescue Exception => e
135
+ if `typeof #{value}` == 'object'
136
+ metadata[attribute] = {}
137
+ populate_metadata_from_native_response(metadata: metadata[attribute], native_response: Native(value), root: nil)
138
+ else
139
+ metadata[attribute] = value
140
+ end
141
+ end
142
+ end
143
+ end
144
+
117
145
  # TODO consider offering generic Rails::ResourceService.get, Rails::ResourceService.put, Rails::ResourceService.post, Rails::ResourceService.patch, Rails::ResourceService.delete methods
118
146
  end
119
147
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.8
4
+ version: 0.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
@@ -357,6 +357,7 @@ files:
357
357
  - lib/glimmer/helpers/glimmer_helper.rb
358
358
  - lib/glimmer/util/proc_tracker.rb
359
359
  - lib/glimmer/util/url_builder.rb
360
+ - lib/glimmer/util/url_query_string_builder.rb
360
361
  - lib/glimmer/web.rb
361
362
  - lib/glimmer/web/component.rb
362
363
  - lib/glimmer/web/component/component_style_container.rb