Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 1900-01-10-10-api-driven-example.md #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions _posts/chapters/1900-01-10-10-api-driven-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ Time to hook it up to some events. Remember when I said `BubbleWrap` comes with

The `when` function is available to every `UIControl` subclass (of which `UIButton` is one) and takes the usual bitmask of `UIControlEvent`s as its arguments. While the request runs, we temporarily disable our UI elements.


In order to allow our app to access the web we need to add an application intitlement. So let's go into our rake file and add:

```ruby
...
app.info_plist['ITSAppUsesNonExemptEncryption'] = false
app.info_plist["NSAppTransportSecurity"] = { "NSAllowsArbitraryLoads" => true
...

```
This a very simple way to get acess to the web. This allows everything, which is bad practice normally you would be very specific about the url's your are acessing in your release section, but for our purpose this is fine.



But wait...what's that `Color.find` method? Well, it's a good idea to keep all of your URL requests inside of models instead of controllers. That way if we want to get a `Color` from the server somewhere else in the app then there's no code duplication. Who knows, maybe we'll need that too...*foreshadowing*

So in your `Color` class, add the `find` static method:
Expand All @@ -240,6 +254,7 @@ class Color
end
```


Look bad? We use the basic `HTTP.get` to get some data from the server via the proper API URL. Note that we use the `&block` notation to make it plain that this function is intended to be used with a block. This block isn't explicitly passed as another argument, but rather is implicitly passed when we put a `do/end` after we call the method. The number and order of variables in `.call(some, variables)` corresponds to `do |some, variables|`.

Anyway, `rake` and give it a go with a color like "3B5998". You should see something like this output in the console:
Expand Down