whiplash-app 0.9.5 → 0.9.6

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: e0e2ae334bdd40de1ae96543cd86eb7506e3de9a7d65d04b003fa67d2eb83da1
4
- data.tar.gz: a2ae0fe3f75c4ed25e7703c85f71495609f10434c9bf3d687483d1ad440a5513
3
+ metadata.gz: ed3192117f026dce179a249afae7d1dd8e887883f4272ce9a8ceeff2f9ae9429
4
+ data.tar.gz: 2b3626ec34d6d3fe8bfd4111c3578235ae129321b090501cd0881917506aafe8
5
5
  SHA512:
6
- metadata.gz: 1fd9e63cc8d1359a38adc462eb4818928d4ea84bc1e57db46e1fa9725ef547c59172763727f8fc7957c8de22d0a3720a437040789966116ce5a1c0a833ad210f
7
- data.tar.gz: 0f35b12647a50d4a4c8fbb1a0f24fb6d40946c19dbbeb4d0238eb19d5c075fd3958e2cea68a72072d8db5770c83d18a5c1bc831a3bee2497e9cc7a4bfde52913
6
+ metadata.gz: 8a2ad62e4666d538fed3ecefb03bd675196c4c2612255f84e38e5865d5abca52a2bbe1c026b6e52cc14c16a9c409719c4cd404f60d7d0cbbfdc0e477232ae8c2
7
+ data.tar.gz: cee2a29068bf4ca7e200085539738a902c72c096bc01af699cb8a2330e1c2511a86715fc4d2aefd15487d603f31d0011972cdf1bd6ddefc2678b0a6ec5560db5
data/README.md CHANGED
@@ -59,18 +59,26 @@ As long as all of your apps are on the same subdomain, they will share auth cook
59
59
 
60
60
  ```json
61
61
  {
62
- "oauth_token": XXXXXXX,
63
- "user": {"id":151,"email":"mark@getwhiplash.com","role":"admin","locale":"en","first_name":"Mark","last_name":"Dickson","partner_id":null,"warehouse_id": 1,"customer_ids":[1, 2, 3]}
62
+ "oauth_token": "XXXXXXX",
63
+ "user": {"id":151,"email":"mark@getwhiplash.com","role":"admin","locale":"en","first_name":"Mark","last_name":"Dickson","partner_id":null, "customer_ids":[1, 2, 3]},
64
+ "customer": {"id": 123, "name": "BooYaa"},
65
+ "warehouse": {"id": 1, "name": "Ann Arbor"}
64
66
  }
65
67
  ```
66
68
 
67
69
  You get a variety of helper methods for free:
68
70
 
69
71
  `init_whiplash_api` - This instantiates `@whiplash_api` which can be used to make requests, out of the box
70
- `current_user` - This is a **hash** with the above fields; you typically shouldn't need much more user info than this
72
+ `current_user` - This is a **hash** with the above fields; you typically shouldn't need much more user info than this'
73
+ `current_customer` - This is a **hash** with the above fields; you typically shouldn't need much more user info than this
74
+ `current_warehouse` - This is a **hash** with the above fields; you typically shouldn't need much more user info than this
71
75
  `require_user` - Typically you'd use this in a `before_action`. You almost always want this in `ApplicationController`.
72
76
  `set_locale!` - Sets the locale based on the value in the user hash
73
- `set_current_user_cookie!` - Updates the current user cookie with fresh data from the api. You typically won't need this, unless your app updates fields like `warehouse_id` or `locale`.
77
+ `set_current_user_cookie!` - Updates the current user cookie with fresh data from the api. You typically won't need this, unless your app updates fields like `locale`.
78
+ `set_current_customer_cookie!` - Updates the current customer cookie with fresh data from the api; typically used after you've changed customer
79
+ `set_current_warehouse_cookie!` - Updates the current customer cookie with fresh data from the api; typically used after you've changed warehouse
80
+ `unset_current_customer_cookie!` - Deletes the current customer cookie, with appropriate domain settings
81
+ `unset_current_warehouse_cookie!` - Deletes the current warehouse cookie, with appropriate domain settings
74
82
  `core_url` - Shorthand for `ENV['WHIPLASH_API_URL']`
75
83
  `core_url_for` - Link back to Core like `core_url_for('login')`
76
84
 
@@ -8,7 +8,9 @@ module Whiplash
8
8
  helper_method :cookie_domain,
9
9
  :core_url,
10
10
  :core_url_for,
11
- :current_user
11
+ :current_customer,
12
+ :current_user,
13
+ :current_warehouse
12
14
  end
13
15
 
14
16
  private
@@ -25,6 +27,28 @@ module Whiplash
25
27
  [core_url, path].join('/')
26
28
  end
27
29
 
30
+ def current_customer
31
+ return if cookies[:customer].blank?
32
+ begin
33
+ customer_hash = JSON.parse(cookies[:customer])
34
+ @current_customer ||= customer_hash
35
+ rescue StandardError => e
36
+ Rails.logger.warn "Customer could not be initialized: #{e.message}"
37
+ nil
38
+ end
39
+ end
40
+
41
+ def current_customer_full
42
+ return if cookies[:customer].blank?
43
+ begin
44
+ customer_hash = JSON.parse(cookies[:customer])
45
+ @current_customer ||= @whiplash_api.get!("customers/#{customer_hash['id']}").body
46
+ rescue StandardError => e
47
+ Rails.logger.warn "Customer could not be initialized: #{e.message}"
48
+ nil
49
+ end
50
+ end
51
+
28
52
  def current_user
29
53
  return if cookies[:user].blank?
30
54
  begin
@@ -35,6 +59,28 @@ module Whiplash
35
59
  end
36
60
  end
37
61
 
62
+ def current_warehouse
63
+ return if cookies[:warehouse].blank?
64
+ begin
65
+ warehouse_hash = JSON.parse(cookies[:warehouse])
66
+ @current_warehouse ||= warehouse_hash
67
+ rescue StandardError => e
68
+ Rails.logger.warn "Warehouse could not be initialized: #{e.message}"
69
+ @current_warehouse = nil
70
+ end
71
+ end
72
+
73
+ def current_warehouse_full
74
+ return if cookies[:warehouse].blank?
75
+ begin
76
+ warehouse_hash = JSON.parse(cookies[:warehouse])
77
+ @whiplash_api.get!("warehouses/#{warehouse_hash['id']}").body
78
+ rescue StandardError => e
79
+ Rails.logger.warn "Warehouse could not be initialized: #{e.message}"
80
+ nil
81
+ end
82
+ end
83
+
38
84
  def http_scheme
39
85
  URI(core_url).scheme
40
86
  end
@@ -59,10 +105,25 @@ module Whiplash
59
105
  I18n.locale = current_user.try('locale') || I18n.default_locale
60
106
  end
61
107
 
108
+ def set_current_customer_cookie!(customer_id, expires_at = nil)
109
+ customer = @whiplash_api.get!("customers/#{customer_id}").body
110
+ user = @whiplash_api.get!("me").body
111
+ fields_we_care_about = %w(id name)
112
+ customer_hash = customer.slice(*fields_we_care_about)
113
+ expires_at ||= user['current_sign_in_expires_at']
114
+
115
+ shared_values = {
116
+ expires: DateTime.parse(expires_at),
117
+ secure: http_scheme == 'https',
118
+ samesite: :strict,
119
+ domain: cookie_domain
120
+ }
121
+ cookies[:customer] = shared_values.merge(value: customer_hash.to_json)
122
+ end
62
123
 
63
124
  def set_current_user_cookie!(expires_at = nil)
64
125
  user = @whiplash_api.get!("me").body
65
- fields_we_care_about = %w(id email role locale first_name last_name partner_id warehouse_id customer_ids)
126
+ fields_we_care_about = %w(id email role locale first_name last_name partner_id customer_ids)
66
127
  user_hash = user.slice(*fields_we_care_about)
67
128
  expires_at ||= user['current_sign_in_expires_at']
68
129
 
@@ -75,6 +136,30 @@ module Whiplash
75
136
  cookies[:user] = shared_values.merge(value: user_hash.to_json)
76
137
  end
77
138
 
139
+ def set_current_warehouse_cookie!(warehouse_id, expires_at = nil)
140
+ warehouse = @whiplash_api.get!("warehouses/#{warehouse_id}").body
141
+ user = @whiplash_api.get!("me").body
142
+ fields_we_care_about = %w(id name)
143
+ warehouse_hash = warehouse.slice(*fields_we_care_about)
144
+ expires_at ||= user['current_sign_in_expires_at']
145
+
146
+ shared_values = {
147
+ expires: DateTime.parse(expires_at),
148
+ secure: http_scheme == 'https',
149
+ samesite: :strict,
150
+ domain: cookie_domain
151
+ }
152
+ cookies[:warehouse] = shared_values.merge(value: warehouse_hash.to_json)
153
+ end
154
+
155
+ def unset_current_customer_cookie!
156
+ cookies.delete(:customer, domain: cookie_domain)
157
+ end
158
+
159
+ def unset_current_warehouse_cookie!
160
+ cookies.delete(:warehouse, domain: cookie_domain)
161
+ end
162
+
78
163
  end
79
164
  end
80
165
  end
@@ -1,5 +1,5 @@
1
1
  module Whiplash
2
2
  class App
3
- VERSION = "0.9.5"
3
+ VERSION = "0.9.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whiplash-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Sullivan, Mark Dickson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-10 00:00:00.000000000 Z
11
+ date: 2024-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2