RestClient? Don’t know her.

William Lin
5 min readApr 17, 2020

--

I expected to be average when it came to picking up programming skills but I was gravely mistaken when I started Flatiron School. Personally, I believe I learn better in an immersed environment without distractions from life: AKA not at my computer at home with family distractions.

During my time at the Flatiron School’s Software development coding Bootcamp, I’m running into walls, falling down and getting up. All along the way, I’m curious about how each of the things works. Week after week, I go through a cycle of feeling stupid and not understanding everything. Meanwhile, everyone else understood and seemed to be chugging along. By the end of the week, I’d feel more confident and look back at my past self and laugh.

I was so disappointed in myself when I failed the first module because I felt like I was so close and immediately began studying for the next code challenge the moment after I failed. I realized that the TTY Prompt blog post I wanted to write about needed to be changed since it’s been done twice already and the dead horse has been beaten already. I decided to dive back deep into my notes to find a “hipster-esque” topic to write about.

That's right, I chose the gem rest-client! When my instructor Tashawn taught us about rest-client, he told us that we didn’t have to worry about it. But oh no, that’s not how I roll. The more you tell me not to worry about it, the more I worry. So I looked into the lab that had Open URI, Net::HTTP, and JSON.

Working With APIs Code-Along MADE WITH BY FLATIRON SCHOOL

Basically, it takes info from the url and translates it. Then, he nested data structure gets turned into JSON and parsed so that ruby can interact with it.

I wondered how rest-client does the tasks that Net::HTTP and Open URI do in one. Instead of learning about how it does both of those tasks, I fell into a rabbit hole of learning how rest-client works in general. Before I move on, I must tell you a spoiler that I made a fundamental error. RestClient is a gem, correct! But within RestClient, there are other gems! It’s gem-ception.

rest-client gem github

I clicked on the link and learned that RestClient is composed of 4 other gems and the first gem mime-types

require 'mime/types'

plaintext = MIME::Types['text/plain'] # => [ text/plain ]
text = plaintext.first
puts text.media_type # => 'text'
puts text.sub_type # => 'plain'

puts text.extensions.join(' ') # => 'txt asc c cc h hh cpp hpp dat hlp'
puts text.preferred_extension # => 'txt'
puts text.friendly # => 'Text Document'
puts text.i18n_key # => 'text.plain'

puts text.encoding # => quoted-printable
puts text.default_encoding # => quoted-printable
puts text.binary? # => false
puts text.ascii? # => true
puts text.obsolete? # => false
puts text.registered? # => true
puts text.complete? # => true

puts text # => 'text/plain'

puts text == 'text/plain' # => true
puts 'text/plain' == text # => true
puts text == 'text/x-plain' # => false
puts 'text/x-plain' == text # => false

puts MIME::Type.simplified('x-appl/x-zip') # => 'x-appl/x-zip'
puts MIME::Type.i18n_key('x-appl/x-zip') # => 'x-appl.x-zip'

puts text.like?('text/x-plain') # => true
puts text.like?(MIME::Type.new('x-text/x-plain')) # => true

puts text.xrefs.inspect # => { "rfc" => [ "rfc2046", "rfc3676", "rfc5147" ] }
puts text.xref_urls # => [ "http://www.iana.org/go/rfc2046",
# "http://www.iana.org/go/rfc3676",
# "http://www.iana.org/go/rfc5147" ]

xtext = MIME::Type.new('x-text/x-plain')
puts xtext.media_type # => 'text'
puts xtext.raw_media_type # => 'x-text'
puts xtext.sub_type # => 'plain'
puts xtext.raw_sub_type # => 'x-plain'
puts xtext.complete? # => false

puts MIME::Types.any? { |type| type.content_type == 'text/plain' } # => true
puts MIME::Types.all?(&:registered?) # => false

# Various string representations of MIME types
qcelp = MIME::Types['audio/QCELP'].first # => audio/QCELP
puts qcelp.content_type # => 'audio/QCELP'
puts qcelp.simplified # => 'audio/qcelp'

xwingz = MIME::Types['application/x-Wingz'].first # => application/x-Wingz
puts xwingz.content_type # => 'application/x-Wingz'
puts xwingz.simplified # => 'application/x-wingz'

source: mime-type gem GitHub

Yea, I didn’t bother reading it either. Anyways, I went around the interwebs and found that MIME is an acronym for multipurpose internet mail extension. And it basically means a mime file is stored in a way that ruby doesn’t understand.

n = Netrc.read("sample.netrc")

source: mime-type gem GitHub

The Netrc gem reads files and decrypts them and unscrambles.

module ToJSON
def content_type
HTTP::Accept::ContentType.new("application", "json", charset: 'utf-8')
end

# Used for inserting into map.
def split(*args)
content_type.split(*args)
end

def convert(object, options)
object.to_json
end
end

module ToXML
# Are you kidding?
end

map = HTTP::Accept::MediaTypes::Map.new
map << ToJSON
map << ToXML

object, media_range = map.for(media_types)
content = object.convert(model, media_range.parameters)
response = [200, {'Content-Type' => object.content_type}, [content]]

source: http-accept gem Github

The http-accept gem coverts the file to JSON

########################
# Client side example 1
########################

# Initialize a cookie jar
jar = HTTP::CookieJar.new

# Load from a file
jar.load(filename) if File.exist?(filename)

# Store received cookies, where uri is the origin of this header
header["Set-Cookie"].each { |value|
jar.parse(value, uri)
}

# ...

# Set the Cookie header value, where uri is the destination URI
header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri))

# Save to a file
jar.save(filename)


########################
# Client side example 2
########################

# Initialize a cookie jar using a Mozilla compatible SQLite3 backend
jar = HTTP::CookieJar.new(store: :mozilla, filename: 'cookies.sqlite')

# There is no need for load & save in this backend.

# Store received cookies, where uri is the origin of this header
header["Set-Cookie"].each { |value|
jar.parse(value, uri)
}

# ...

# Set the Cookie header value, where uri is the destination URI
header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri))


########################
# Server side example
########################

# Generate a domain cookie
cookie1 = HTTP::Cookie.new("uid", "u12345", domain: 'example.org',
for_domain: true,
path: '/',
max_age: 7*86400)

# Add it to the Set-Cookie response header
header['Set-Cookie'] = cookie1.set_cookie_value

# Generate a host-only cookie
cookie2 = HTTP::Cookie.new("aid", "a12345", origin: my_url,
path: '/',
max_age: 7*86400)

# Add it to the Set-Cookie response header
header['Set-Cookie'] = cookie2.set_cookie_value

source: http-cookie gem GitHub

The http-accept cookie basically tells the server that you took something.

Voila! you get rest-client!

--

--