lp_rails_util 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 985d27d5f00db98f617194033682006b4b50fa60
4
+ data.tar.gz: f96fa3d43ba4813b2ba44e44e291a9d2ac4e03de
5
+ SHA512:
6
+ metadata.gz: ea02caa2d387c58141ca03a40faa9708324c55e1afc6431d0dd7933315ad915bd0460815f7a0202659e0fbfe6c27852eb2204d9c0ecb111323a06d1e95e1d647
7
+ data.tar.gz: 2dd2c0d8b2bcb8d9447d9ec85d361f1a21c1aa226e85c1666ae92d2affe88504cfb997fb48034866c05b9c76d1bbb6c8eb04f985fd9482c3ea4b861e3ccb83f4
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .yardoc
2
+ doc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --markup markdown
2
+ -
3
+ README.md
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lp_rails_util (1.4.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ concurrent-ruby (1.1.4)
16
+ diff-lcs (1.3)
17
+ i18n (1.3.0)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.11.3)
20
+ rspec (3.5.0)
21
+ rspec-core (~> 3.5.0)
22
+ rspec-expectations (~> 3.5.0)
23
+ rspec-mocks (~> 3.5.0)
24
+ rspec-core (3.5.4)
25
+ rspec-support (~> 3.5.0)
26
+ rspec-expectations (3.5.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.5.0)
29
+ rspec-mocks (3.5.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.5.0)
32
+ rspec-support (3.5.0)
33
+ thread_safe (0.3.6)
34
+ tzinfo (1.2.5)
35
+ thread_safe (~> 0.1)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ lp_rails_util!
42
+ rspec
43
+
44
+ BUNDLED WITH
45
+ 1.15.1
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Launchpad Lab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Rails Utils
2
+ A utility library for our Rails projects.
3
+
4
+ ## JsonHelper
5
+ Helper methods for Rails JSON API responses.
6
+
7
+ ## ReactHelper
8
+ Helper methods for rendering React components using the `react_on_rails` gem.
9
+
10
+ ## TimezoneHelper
11
+ Helper methods for converting times between timezones.
12
+
13
+ ## Documentation
14
+ To view the documentation locally, run:
15
+ + `yard doc`
16
+ + `yard server`
17
+ + Open `localhost:8808`
18
+
19
+ ## Tests
20
+ Run tests with `bundle exec rspec`.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lp_rails_util"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/rails_util.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'active_support'
2
+ require 'rails_util/util'
3
+ require 'rails_util/json_helper'
4
+ require 'rails_util/react_helper'
5
+ require 'rails_util/timezone_helper'
6
+ require 'rails_util/version'
@@ -0,0 +1,127 @@
1
+ module RailsUtil
2
+ # `RailsUtil::JsonHelper` contains helper methods for rendering JSON API responses
3
+ module JsonHelper
4
+ class MissingSerializerError < StandardError; end
5
+
6
+ # Renders JSON object, along with other options
7
+ # @param [Object] resource `ActiveRecord` resource
8
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
9
+ # @return [Object] json object
10
+ def json_with(resource, **options)
11
+ return json_empty(**options) unless resource
12
+ root_key = resource.class.name.split('::').last.underscore
13
+
14
+ return json_error(
15
+ { root_key => map_base_to__error(resource.errors.messages) },
16
+ **options
17
+ ) if errors?(resource)
18
+
19
+ return json_success(
20
+ { root_key => {} },
21
+ **options
22
+ ) if destroyed?(resource)
23
+
24
+ serialize_json_resource(resource, **options)
25
+ end
26
+
27
+ # Renders array of JSON objects, along with other options, or an empty array
28
+ # if the array contains no values
29
+ # @param [Array] resources array of resources
30
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
31
+ # @return [Array] array of json objects
32
+ def json_collection(resource, **options)
33
+ return render json: [], **options unless resource.any?
34
+ json_with(resource, **options)
35
+ end
36
+
37
+ # Renders empty JSON object, along with other options
38
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
39
+ # @return [Object] empty json object
40
+ def json_empty(**options)
41
+ render json: {}, **options
42
+ end
43
+
44
+ # Renders JSON error response with `422` status, along with other options
45
+ # @param [String, Hash] nested_path_or_obj the key-value option pairs
46
+ # @param [String] message the message to include in the error object
47
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
48
+ # @return [Object] json error object
49
+ def json_error(nested_path_or_obj, message=nil, **options)
50
+ error_obj = set_nested_path(nested_path_or_obj, message)
51
+ render(
52
+ json: { errors: error_obj },
53
+ status: :unprocessable_entity,
54
+ **options
55
+ )
56
+ end
57
+
58
+ # Renders JSON success response, along with other options
59
+ # @param [String,Hash] path_or_obj the key-value option pairs
60
+ # @param [String=nil] message the message to include in the error object
61
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
62
+ # @return [Object] json success object
63
+ def json_success(path_or_obj, message=nil, **options)
64
+ render(
65
+ json: set_nested_path(path_or_obj, message),
66
+ **options
67
+ )
68
+ end
69
+
70
+ # Renders serialized JSON resource
71
+ # @param [Object] resource `ActiveRecord` resource
72
+ # @param [Symbol=>[Integer, String, Array]] options the key-value option pairs
73
+ # @return [Object] json resource object
74
+ # @raise [MissingSerializerError] if the provided resource does not have a serializer
75
+ def serialize_json_resource(resource, **options)
76
+ serializer_options = options[:serializer_options] || {}
77
+ serializable_resource = ActiveModelSerializers::SerializableResource.new(
78
+ resource,
79
+ serializer_options.merge(scope: serialization_scope)
80
+ )
81
+ raise MissingSerializerError unless serializable_resource.serializer?
82
+ serialized_obj = serializable_resource.serializer_instance.object
83
+ type = options[:resource] || serialized_object_type(serialized_obj)
84
+
85
+ data = { type: type, attributes: serializable_resource.serializer_instance }
86
+ data[:additional_data] = options[:additional_data] if options && options[:additional_data]
87
+
88
+ render json: { data: data }, **options
89
+ end
90
+
91
+ private
92
+
93
+ def set_nested_path(nested_path_or_obj, message)
94
+ return set_nested(nested_path_or_obj, message) if nested_path_or_obj.is_a? String
95
+ nested_path_or_obj
96
+ end
97
+
98
+ def map_base_to__error(error_obj)
99
+ error_obj[:_error] = error_obj.delete(:base) if error_obj.key? :base
100
+ error_obj
101
+ end
102
+
103
+ def errors?(resource)
104
+ resource.respond_to?(:errors) && resource.errors.any?
105
+ end
106
+
107
+ def destroyed?(resource)
108
+ resource.respond_to?(:destroyed?) && resource.destroyed?
109
+ end
110
+
111
+ def set_nested(path, value, obj={})
112
+ obj.deep_merge(path_to_hash(path, value))
113
+ end
114
+
115
+ def serialized_object_type(obj)
116
+ is_multiple = obj.try(:length) && obj.length.positive?
117
+ type = is_multiple ? Util.underscored_class_name(obj.first).pluralize : Util.underscored_class_name(obj)
118
+ type.split('/').first
119
+ end
120
+
121
+ def path_to_hash(path, value)
122
+ parts = (path.is_a?(String) ? path.split('.') : path).reverse
123
+ initial = { parts.shift => value }
124
+ parts.reduce(initial) { |a, e| { e => a } }
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,22 @@
1
+ module RailsUtil
2
+ # `RailsUtil::ReactHelper` contains wrapper methods for rendering React components using the `react_on_rails` gem
3
+ module ReactHelper
4
+ # Helper method for rendering a React component to the DOM
5
+ # Note: A view file named `component.html.erb` containing the `react_component` view helper is also required by this method
6
+ # @example Render a component
7
+ # # app/controllers/main_controller.rb
8
+ # class MainController < ApplicationController
9
+ # def app
10
+ # render_component 'MainApp', props: {}
11
+ # end
12
+ # end
13
+ #
14
+ # # app/views/application/component.html.erb
15
+ # <%= react_component component, options %>
16
+ # @param [String] component the name of the React component to render
17
+ # @param [Hash] options any `props` and other options to pass to the React object
18
+ def render_component(component, options = {})
19
+ render 'component', locals: { component: component, options: options }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,72 @@
1
+ require 'active_support/time'
2
+
3
+ module RailsUtil
4
+ # `RailsUtil::TimezoneHelper` contains helper methods for converting a time between timezones.
5
+ # Uses `ActiveSupport::TimeZone` `timezone.tzinfo.name` names (e.g., 'America/Chicago') to identify timezones.
6
+ module TimezoneHelper
7
+ class << self
8
+ SECONDS_PER_HOUR = 3600
9
+
10
+ # Converts a time between a `from_timezone` and a `to_timezone`
11
+ # @param [Time, DateTime] time can be a Time or DateTime object, will use the date of this argument if date not provided separately
12
+ # @param [String] to_timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name` of the desired timezone
13
+ # @param [String] from_timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name` of the timezone being converted
14
+ # @param [Symbol=>[Date]] options key-value option pairs, used to provide a separate Date object if a specific date is needed, otherwise the date of the time argument is used
15
+ # @return DateTime object with the time in the desired `to_timezone`, and a date of either the provided date or time
16
+ def convert_timezone(time, to_timezone, from_timezone, **options)
17
+ date = options.fetch(:date, time)
18
+ utc_time = convert_to_utc(time, from_timezone, date)
19
+ convert_from_utc(utc_time, to_timezone, date)
20
+ end
21
+
22
+ private
23
+
24
+ # Returns the time in the given timezone converted to UTC with the formatted offset
25
+ # @param [String] from_timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name` of the timezone being converted
26
+ # @param [Time, DateTime] time can be a Time or DateTime object, will use date of Time object if no date provided
27
+ # @param [Time, DateTime] date optional parameter to specify a date, otherwise the date of the provided time will be used
28
+ # @return DateTime object with the time in UTC, and a date of either the provided date or time
29
+ def convert_to_utc(time, from_timezone, date=nil)
30
+ date ||= time
31
+ timezone_with_offset(time, from_timezone, date).utc
32
+ end
33
+
34
+ # Returns the time in UTC converted to the given timezone with the formatted offset
35
+ # @param [String] to_timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name` of the desired timezone
36
+ # @param [Time, DateTime] time can be a Time or DateTime object, will use date of Time object if no date provided
37
+ # @param [Time, DateTime] date optional parameter to specify a date, otherwise the date of the provided time will be used
38
+ # @return DateTime object with the time in the given timezone, and a date of either the provided date or time
39
+ def convert_from_utc(time, to_timezone, date=nil)
40
+ date ||= time
41
+ time += utc_offset(to_timezone).hours
42
+ timezone_with_offset(time, to_timezone, date)
43
+ end
44
+
45
+ # Creates a DateTime object with the offset of the given timezone, a time of the time provided, and a date of either the date or time provided
46
+ # @param [String] timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name` of the desired timezone
47
+ # @param [Time, DateTime] time can be a Time or DateTime object, will use date of Time object if no date provided
48
+ # @param [Time, DateTime] date optional parameter to specify a date, otherwise the date of the provided time will be used
49
+ # @return DateTime object in the given time with an offset of the given timezone
50
+ def timezone_with_offset(time, timezone, date=nil)
51
+ date ||= time
52
+ DateTime.new(date.year, date.month, date.day, time.hour, time.min, 0, format_offset(timezone))
53
+ end
54
+
55
+ # Returns a string representation of the UTC offset for the given timezone
56
+ # @param [String] timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name`
57
+ # @return String object of the UTC offset of the given timezone
58
+ def format_offset(timezone)
59
+ offset = utc_offset(timezone).to_s
60
+ offset.insert(1, '0') if utc_offset(timezone).abs < 10
61
+ offset + ':00'
62
+ end
63
+
64
+ # Returns a fixnum representation of the UTC offset for the given timezone in hours, taking into account whether Daylight Savings Time is in effect
65
+ # @param [String] timezone the `ActiveSupport::TimeZone` `timezone.tzinfo.name`
66
+ # @return FixNum object of the UTC offset of the given timezone
67
+ def utc_offset(timezone)
68
+ TZInfo::Timezone.get(timezone).current_period.utc_total_offset / SECONDS_PER_HOUR
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,41 @@
1
+ module RailsUtil
2
+ # `RailsUtil::Util` includes class helper methods for handling nested hashes
3
+ module Util
4
+ # Deep merges a nested hash given a path
5
+ # Does not mutate the original hash
6
+ # @param [String, Array] path the nested keys
7
+ # @param [String, Integer, Array] value the value of nested path key
8
+ # @param [Hash] obj the hash object to merge
9
+ # @return [Hash] the nested hash
10
+ def self.set_nested(path, value, obj={})
11
+ obj.deep_merge(path_to_hash(path, value))
12
+ end
13
+
14
+ # Deep merges a nested hash given a path
15
+ # Mutates the original hash
16
+ # @param [String, Array] path the nested keys
17
+ # @param [String, Integer, Array] value the value of nested path key
18
+ # @param [Hash] obj the hash object to merge
19
+ # @return [Hash] the nested hash
20
+ def self.set_nested!(path, value, obj={})
21
+ obj.deep_merge!(path_to_hash(path, value))
22
+ end
23
+
24
+ # Creates a nested hash given a path
25
+ # @param [String, Array] path the nested keys
26
+ # @param [String, Integer, Array] value the value of nested path key
27
+ # @return [Hash] the nested hash
28
+ def self.path_to_hash(path, value)
29
+ parts = (path.is_a?(String) ? path.split('.') : path).reverse
30
+ initial = { parts.shift => value }
31
+ parts.reduce(initial) { |a, e| { e => a } }
32
+ end
33
+
34
+ # Returns the underscored class name of an `ActiveRecord` object
35
+ # @param [ActiveRecord Object] the `ActiveRecord` object
36
+ # @return [String] underscored class name
37
+ def self.underscored_class_name(obj)
38
+ obj.class.to_s.underscore
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ module RailsUtil
2
+ # Current `RailsUtil` version
3
+ VERSION = '1.4.0'.freeze
4
+ end
@@ -0,0 +1,19 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rails_util/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'lp_rails_util'
7
+ s.version = RailsUtil::VERSION
8
+ s.date = '2017-04-11'
9
+ s.summary = 'Utility code for Rails apps'
10
+ s.description = 'Utility code for Rails apps'
11
+ s.authors = ['Dave Corwin']
12
+ s.email = 'dave@launchpadlab.com'
13
+ s.homepage = 'https://github.com/launchpadlab/rails_util'
14
+ s.license = 'None'
15
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+
17
+ s.add_development_dependency 'rspec'
18
+ s.add_dependency 'activesupport'
19
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lp_rails_util
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Dave Corwin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Utility code for Rails apps
42
+ email: dave@launchpadlab.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".rspec"
49
+ - ".yardopts"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/rails_util.rb
57
+ - lib/rails_util/json_helper.rb
58
+ - lib/rails_util/react_helper.rb
59
+ - lib/rails_util/timezone_helper.rb
60
+ - lib/rails_util/util.rb
61
+ - lib/rails_util/version.rb
62
+ - lp_rails_util.gemspec
63
+ homepage: https://github.com/launchpadlab/rails_util
64
+ licenses:
65
+ - None
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Utility code for Rails apps
87
+ test_files: []
88
+ has_rdoc: