1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud
ae2974e3b8 Add Authorize A cURL Request as a Unix TIL 2025-07-02 12:25:32 -05:00
jbranchaud
0ed4d84bc6 Add Get Specific Values From Hashes And Arrays as a Ruby TIL 2025-07-02 10:09:48 -05:00
3 changed files with 79 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1650 TILs and counting..._
_1652 TILs and counting..._
See some of the other learning resources I work on:
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
@@ -1345,6 +1345,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Generate A Signed JWT Token](ruby/generate-a-signed-jwt-token.md)
- [Generate Ruby Version And Gemset Files With RVM](ruby/generate-ruby-version-and-gemset-files-with-rvm.md)
- [Get Info About Your RubyGems Environment](ruby/get-info-about-your-ruby-gems-environment.md)
- [Get Specific Values From Arrays And Hashes](ruby/get-specific-values-from-hashes-and-arrays.md)
- [Get The Output Of Running A System Program](ruby/get-the-output-of-running-a-system-program.md)
- [Get UTC Offset For Different Time Zones](ruby/get-utc-offset-for-different-time-zones.md)
- [Identify Outdated Gems](ruby/identify-outdated-gems.md)
@@ -1533,6 +1534,7 @@ If you've learned something here, support my efforts writing daily TILs by
### Unix
- [All The Environment Variables](unix/all-the-environment-variables.md)
- [Authorize A cURL Request](unix/authorize-a-curl-request.md)
- [Cat A File With Line Numbers](unix/cat-a-file-with-line-numbers.md)
- [Cat Files With Color Using Bat](unix/cat-files-with-color-using-bat.md)
- [Change Default Shell For A User](unix/change-default-shell-for-a-user.md)

View File

@@ -0,0 +1,44 @@
# Get Specific Values From Hashes And Arrays
Ruby defines a `#values_at` method on both `Hash` and `Array` that can be used
to grab multiple values from an instance of either of those structures.
Here is an example of grabbing values by key (if they exist) from a hash.
```ruby
> hash = {one: :two, hello: "world", four: 4}
=> {one: :two, hello: "world", four: 4}
> hash.values_at(:one, :four, :three)
=> [:two, 4, nil]
```
And here is an example of grabbing values at specific indexes from an array, if
those indexes exist.
```ruby
> arr = [:a, :b, :c, :d, :e]
=> [:a, :b, :c, :d, :e]
> arr.values_at(0, 3, 6)
=> [:a, :d, nil]
```
Notice that in both cases, `nil` is returned for a key or index that doesn't
exist.
What I like about this method is that in a single call I can grab multiple
named (or indexed) values and get a single array result with those values.
One way I might use this with a JSON response from an API request could look
like this:
```ruby
resp = client.getSomeData(id: 123)
[status, body] = resp.values_at("status", "body")
if status == 200
puts body
end
```
[source](https://docs.ruby-lang.org/en/3.4/Hash.html#method-i-values_at)

View File

@@ -0,0 +1,32 @@
# Authorize A cURL Request
When making a cURL request to an endpoint that requires authentication,
sometimes you already have a bearer token and other times you have a username
and password pair. If you have a bearer token, you can format a `Authorization`
header with the `-H` flag that includes that value.
If you have a username and password for the API, you can instead use the `-u`
flag. The `-u` flag will format the username and password, base64 encode it,
and then add it as an `Authorization` header.
```bash
$ curl -v -u "username:password" https://some-endpoint.com/api/v1/status
...
> GET /api/v1/status HTTP/2
> Host: some-endpoint.com
> Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQK
> User-Agent: curl/8.1.2
...
```
You can even pass in only the username to the `-u` flag and cURL will know to
prompt you for the password. This is a nice way to avoid putting plain text
passwords in your shell history.
```bash
$ curl -v -u "username" https://some-endpoint.com/api/v1/status
Enter host password for user 'username':
```
See `man curl` for more details.