Skip to content

Commit 28a6a96

Browse files
committed
add first post in the series on external APIs and the Representable gem
1 parent 279bf01 commit 28a6a96

3 files changed

+82
-118
lines changed

README.md

-118
Original file line numberDiff line numberDiff line change
@@ -1,118 +0,0 @@
1-
# Jekyll Now
2-
3-
**Jekyll** is a static site generator that's perfect for GitHub hosted blogs ([Jekyll Repository](https://github.com/jekyll/jekyll))
4-
5-
**Jekyll Now** makes it easier to create your Jekyll blog, by eliminating a lot of the up front setup.
6-
7-
- You don't need to touch the command line
8-
- You don't need to install/configure ruby, rvm/rbenv, ruby gems :relaxed:
9-
- You don't need to install runtime dependancies like markdown processors, Pygments, etc
10-
- If you're on Windows, this will make setting up Jekyll a lot easier
11-
- It's easy to try out, you can just delete your forked repository if you don't like it
12-
13-
In a few minutes you'll be set up with a minimal, responsive blog like the one below giving you more time to spend on writing epic blog posts!
14-
15-
![Jekyll Now Theme Screenshot](/images/jekyll-now-theme-screenshot.jpg "Jekyll Now Theme Screenshot")
16-
17-
## Quick Start
18-
19-
### Step 1) Fork Jekyll Now to your User Repository
20-
21-
Fork this repo, then rename the repository to yourgithubusername.github.io.
22-
23-
Your Jekyll blog will often be viewable immediately at <http://yourgithubusername.github.io> (if it's not, you can often force it to build by completing step 2)
24-
25-
![Step 1](/images/step1.gif "Step 1")
26-
27-
### Step 2) Customize and view your site
28-
29-
Enter your site name, description, avatar and many other options by editing the _config.yml file. You can easily turn on Google Analytics tracking, Disqus commenting and social icons here too.
30-
31-
Making a change to _config.yml (or any file in your repository) will force GitHub Pages to rebuild your site with jekyll. Your rebuilt site will be viewable a few seconds later at <http://yourgithubusername.github.io> - if not, give it ten minutes as GitHub suggests and it'll appear soon
32-
33-
> There are 3 different ways that you can make changes to your blog's files:
34-
35-
> 1. Edit files within your new username.github.io repository in the browser at GitHub.com (shown below).
36-
> 2. Use a third party GitHub content editor, like [Prose by Development Seed](http://prose.io). It's optimized for use with Jekyll making markdown editing, writing drafts, and uploading images really easy.
37-
> 3. Clone down your repository and make updates locally, then push them to your GitHub repository.
38-
39-
![_config.yml](/images/config.png "_config.yml")
40-
41-
### Step 3) Publish your first blog post
42-
43-
Edit `/_posts/2014-3-3-Hello-World.md` to publish your first blog post. This [Markdown Cheatsheet](http://www.jekyllnow.com/Markdown-Style-Guide/) might come in handy.
44-
45-
![First Post](/images/first-post.png "First Post")
46-
47-
> You can add additional posts in the browser on GitHub.com too! Just hit the + icon in `/_posts/` to create new content. Just make sure to include the [front-matter](http://jekyllrb.com/docs/frontmatter/) block at the top of each new blog post and make sure the post's filename is in this format: year-month-day-title.md
48-
49-
## Local Development
50-
51-
1. Install Jekyll and plug-ins in one fell swoop. `gem install github-pages` This mirrors the plug-ins used by GitHub Pages on your local machine including Jekyll, Sass, etc.
52-
2. Clone down your fork `git clone git@github.com:yourusername/yourusername.github.io.git`
53-
3. Serve the site and watch for markup/sass changes `jekyll serve`
54-
4. View your website at http://0.0.0.0:4000
55-
5. Commit any changes and push everything to the master branch of your GitHub user repository. GitHub Pages will then rebuild and serve your website.
56-
57-
## Moar!
58-
59-
I've created a more detailed walkthrough, [**Build A Blog With Jekyll And GitHub Pages**](http://www.smashingmagazine.com/2014/08/01/build-blog-jekyll-github-pages/) over at the Smashing Magazine website. Check it out if you'd like a more detailed walkthrough and some background on Jekyll. :metal:
60-
61-
It covers:
62-
63-
- A more detailed walkthrough of setting up your Jekyll blog
64-
- Common issues that you might encounter while using Jekyll
65-
- Importing from Wordpress, using your own domain name, and blogging in your favorite editor
66-
- Theming in Jekyll, with Liquid templating examples
67-
- A quick look at Jekyll 2.0’s new features, including Sass/Coffeescript support and Collections
68-
69-
## Jekyll Now Features
70-
71-
✓ Command-line free _fork-first workflow_, using GitHub.com to create, customize and post to your blog
72-
✓ Fully responsive and mobile optimized base theme (**[Theme Demo](http://jekyllnow.com)**)
73-
✓ Sass/Coffeescript support using Jekyll 2.0
74-
✓ Free hosting on your GitHub Pages user site
75-
✓ Markdown blogging
76-
✓ Syntax highlighting
77-
✓ Disqus commenting
78-
✓ Google Analytics integration
79-
✓ SVG social icons for your footer
80-
✓ 3 http requests, including your avatar
81-
82-
✘ No installing dependancies
83-
✘ No need to set up local development
84-
✘ No configuring plugins
85-
✘ No need to spend time on theming
86-
✘ More time to code other things ... wait ✓!
87-
88-
## Questions?
89-
90-
[Open an Issue](https://github.com/barryclark/jekyll-now/issues/new) and let's chat!
91-
92-
## Other forkable themes
93-
94-
You can use the [Quick Start](https://github.com/barryclark/jekyll-now#quick-start) workflow with other themes that are set up to be forked too! Here are some of my favorites:
95-
96-
- [Hyde](https://github.com/poole/hyde) by MDO
97-
- [Lanyon](https://github.com/poole/lanyon) by MDO
98-
- [mojombo.github.io](https://github.com/mojombo/mojombo.github.io) by Tom Preston-Werner
99-
- [Left](https://github.com/holman/left) by Zach Holman
100-
- [Minimal Mistakes](https://github.com/mmistakes/minimal-mistakes) by Michael Rose
101-
- [Skinny Bones](https://github.com/mmistakes/skinny-bones-jekyll) by Michael Rose
102-
103-
## Credits
104-
105-
- [Jekyll](https://github.com/jekyll/jekyll) - Thanks to its creators, contributors and maintainers.
106-
- [SVG icons](https://github.com/neilorangepeel/Free-Social-Icons) - Thanks, Neil Orange Peel. They're beautiful.
107-
- [Solarized Light Pygments](https://gist.github.com/edwardhotchkiss/2005058) - Thanks, Edward.
108-
- [Joel Glovier](http://joelglovier.com/writing/) - Great Jekyll articles. I used Joel's feed.xml in this repository.
109-
- [David Furnes](https://github.com/dfurnes), [Jon Uy](https://github.com/jonuy), [Luke Patton](https://github.com/lkpttn) - Thanks for the design/code reviews.
110-
- [Bart Kiers](https://github.com/bkiers), [Florian Simon](https://github.com/vermluh), [Henry Stanley](https://github.com/henryaj), [Hun Jae Lee](https://github.com/hunjaelee), [Javier Cejudo](https://github.com/javiercejudo), [Peter Etelej](https://github.com/etelej), [Ben Abbott](https://github.com/jaminscript), [Ray Nicholus](https://github.com/rnicholus), [Erin Grand](https://github.com/eringrand), [Léo Colombaro](https://github.com/LeoColomb), [Dean Attali](https://github.com/daattali), [Clayton Errington](https://github.com/cjerrington), [Colton Fitzgerald](https://github.com/coltonfitzgerald), [Trace Mayer](https://github.com/sunnankar) - Thanks for your [fantastic contributions](https://github.com/barryclark/jekyll-now/commits/master) to the project!
111-
112-
## Contributing
113-
114-
Issues and Pull Requests are greatly appreciated. If you've never contributed to an open source project before I'm more than happy to walk you through how to create a pull request.
115-
116-
You can start by [opening an issue](https://github.com/barryclark/jekyll-now/issues/new) describing the problem that you're looking to resolve and we'll go from there.
117-
118-
I want to keep Jekyll Now as minimal as possible. Every line of code should be one that's useful to 90% of the people using it. Please bear that in mind when submitting feature requests. If it's not something that most people will use, it probably won't get merged. :guardsman:

_layouts/post.html

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ <h1>{{ page.title }}</h1>
1414
</div>
1515

1616
<div class="social">
17+
<br />
1718
Did you find this post helpful? If so, please consider taking a moment to share it on your social network of choice (I'm particularly fond of Twitter). If you'd like to be the first to know when I've published something new, you can follow me at <a href="https://twitter.com/davidcpell">@davidcpell</a>!
1819
</div>
1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
layout: post
3+
title: Mapping Data from External APIs to ActiveRecord Objects with the Representable Gem - Part I
4+
---
5+
6+
## What is an API?
7+
At my day job I spend a lot of time writing code to communicate with APIs and retrieve data from them. What exactly does that mean? First we should take a step back and look at how we get data into our databases.
8+
9+
Let's say we're working on a Rails app related to comic book characters and have a model like this:
10+
11+
```ruby
12+
# app/models/supervillain.rb
13+
14+
class Supervillain < ActiveRecord::Base
15+
has_many :plots
16+
end
17+
```
18+
19+
...and the `supervillains` table in our database has columns for `name`, `alias`, and `publisher`.
20+
21+
#### Creating data in the Rails console
22+
One way for us to populate the database with supervillain entries would be to create entries by hand in a Rails console:
23+
24+
```ruby
25+
Supervillain.create(name: "Lex Luther", alias: "Mockingbird", publisher: "DC Comics")
26+
Supervillain.create(name: "Magneto", alias: "Magnus", publisher: "Marvel Comics")
27+
... # this could take a while...
28+
```
29+
That's obviously not a good idea if we have a lot of data to import.
30+
31+
#### User-submitted data (forms)
32+
33+
Another way we could populate our db is by having forms on a web page that can be filled out by users. When a user submits our imaginary Supervillain form, the data is posted to the `create` action of `SupervillainsController`, which does the work of saving the new Supervillain object to the database.
34+
35+
#### A better option
36+
37+
Databases can also be programmatically "seeded" with large amounts of data from various third-party sources. In other words, we let someone else collate all the data and then we go and plunder it! Or sometimes they send it to us. This could happen in a number of ways, such as parsing and importing the contents of CSV files (comma-separated values, a text file that can be read as a spreadsheet with commas delimiting the columns) and, most importantly for this post, retrieving the data in JSON format from an external API.
38+
39+
#### What is an API?
40+
41+
"API" is one of those terms that you've probably heard thrown around a lot if you're just getting started with software development. Maybe you've done some Googling or even visited [Wikipedia][2] to try to get a better understanding of what people are talking about. If you _haven't_ heard it yet, I assure you that you _will_ hear people talking about APIs sooner or later.
42+
43+
Before making my career change to software development, I was a foreign language teacher. Because of my background with learning and teaching languages I like to think of APIs as the vocabularies "spoken" by the objects in an application. For example, all of the methods that a string can respond to, taken collectively, could be referred to as the String's vocabulary - its API. This understanding of API as vocabulary fits well with the convention of referring to Ruby methods as "messages" that objects send to and receive from each other popularized by [Sandi Metz][3] and others. When you build your own classes, you define their APIs as you specify which messages they are able to respond to. So let's open our class back up:
44+
45+
```ruby
46+
# app/models/supervillain.rb
47+
48+
class Supervillain < ActiveRecord::Base
49+
has_many :plots
50+
51+
def maniacal_laughter
52+
"MUAHAHAHA"
53+
end
54+
end
55+
```
56+
57+
Now we can open up our console again and play around like this, with a method we've defined and one we haven't:
58+
59+
```ruby
60+
[1] pry(main)> villain = Supervillain.new
61+
=> #<Supervillain:0x007fb29ba24ab8>
62+
[2] pry(main)> villain.maniacal_laughter
63+
=> "MUAHAHAHA"
64+
[3] pry(main)> villain.feel_pity
65+
NoMethodError: undefined method `feel_pity' for #<Supervillain:0x007fb29ba24ab8>
66+
67+
# A NoMethodError means, "I don't know what you're talking about! That message isn't in my vocabulary!"
68+
```
69+
70+
When we go back to look at the technical definitions of API, it becomes more clear what the "I" ("interface") means. Interfaces act as the meeting point between two parties and facilitate their communication or interaction. Your keyboard and mouse are the interface that allow you to provide instructions to your computer. Language and the alphabet are interfaces that are allowing me to communicate all of this information to you. A Ruby application's interface is the set of objects of which it is comprised and the messages to which they respond.
71+
72+
#### Network APIs
73+
74+
For applications configured to operate over network connections (like your web apps!), API can refer specifically to the objects/resources that its developers have "exposed" to users to be created, read, updated, and destroyed. In part 2 of this series I'll look at what it means to make network requests to an external API and receive a response, which can then be processed and used in some way by our own application.
75+
76+
77+
78+
79+
[1]: https://github.com/apotonick/representable
80+
[2]: https://en.wikipedia.org/wiki/Application_programming_interface
81+
[3]: https://twitter.com/sandimetz?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor

0 commit comments

Comments
 (0)