Hey everybody what's going on this is Caleb with deps slopes dot com.
And in this video we're going to go ahead and build the model layer for our application that's going
to allow us to create an instance of a hub repository will be able to download the data inject it into
our instance and then use it to display that data and our table view.
But first we're going to actually start by taking a look at the get hub developer guide.
So I'm actually going to pull it up in Safari and we're going to be looking at the search functionalities
of the Get hub API.
And of course if you want to know more you can come here and you can read through all of it it's very
robust very in-depth.
And basically you can read all of the specifics but what we need to actually look at is the ability
to search repositories and that's exactly what we want to do.
So we can do that by using slash search slash repositories that's hard to say.
But anyway that is what we can do.
Now there are three parameters to sort and order and that allows us to one give a query a search query.
What we want to look for we can sort it by the number of stars and number of forks or when it was last
updated of course by default they're sorted by best match as Herb sees fit I guess.
Then of course there is order.
OK.
Now you can sort them ascending or descending meaning from least the greatest or greatest at least.
Now we can also in any of these additional parameters like create it meaning the date of creation the
forked repositories or we can basically set it to be false or true so we can view for depositories or
not we can filter them based on the number of fork's search by programming language by repo or user
or the size number of stars et cetera et cetera.
So there's a ton of different things we can do.
And there's a great example right here.
OK this is an example to search for Tetris repositories that are popular.
OK.
So it's going to be sorted by stars meaning the most popular written in the assembly programming language.
So this is an example of what it might look like and let me zoom in here so we can see it really well.
There we go.
So we have API get hub dot com slash search slash repositories question mark Q equals and there is our
search query Tetris.
Now on top of the search query we can do.
Plus language or plus fork's plus size we can do any of those and we've done plus language assembly
because remember we're looking for Tetris that has been built in assembly.
Then we can call and sort K and we can sort it by the number of stars.
The order is descending.
So it's going to sort them by the highest number of stars down to the least.
OK that's pretty cool.
Let's go ahead and let's close it out.
Let's see what this query actually looks like when we call it.
We get a lot of Jason data.
And if you've worked with Jason before you know that this is just a way that applies as far as restful
API go.
They tend to return data in a Jason format which is really easy for us to work with actually.
So I'm going to go ahead and pass this in to what's called a Jason beautifier which you can find easily
by searching in Google.
And this is the same Jason code.
But it has been beautified.
Let me see if I can zoom in.
There we go.
And basically this allows us to kind of collapse and open the different pieces here and dent it nicely
bowled the keys and shows the values.
It's really cool.
So as you can see this is the first result called Tetris.
OK.
Now the owner is Daniel Eve.
We can see his username.
We can see his User ID.
We can get a link to his avatar.
You are l.
That's pretty cool.
So let's go ahead and close that down.
We can see this is not a private repository.
The U R L to the directory there which is pretty sweet.
We can see your elf for the Tetris from his repo's.
Part of his profile.
You can see there's a ton of stuff here.
Now as I scroll down you'll notice there's not going to be a number of contributors.
If you think back that is one of the things that we use there is a fork's count that shows how many
times it has been forked.
But you'll notice there is not a number of contributors and that's because we need to go into contributor's
you RL And we need to create another Jaison call and we need to download the Jaison for the contributor's
pull out the count of contributors from there and we'll do that later on.
But as you can see all of our data here is coming in.
So we're going to go ahead and use this as a way of framing our mind when creating our model layer.
So I'm going to go ahead and close out of this and pull open my X code project.
I would recommend that you do the same because you're probably watching this wanting to learn anyway.
So let's continue go to your project go ahead and right click on the model folder and select new file.
Click on swift file DoubleClick and we're going to name this repo.
Because that is what our instance will be called repo of course is short for repository that we go.
So go ahead and we can just collapse this for now because we're just going to be writing some code and
we're going to think our repo needs a few things.
We're going to need a name obviously.
So let's go ahead and let's actually create these properties now first let's create the class.
Class repo.
And we're going to go ahead and create var name which of course is going to come in as a string.
OK.
Let's use our table view here as an example.
I mean quit out and open up the finished version.
So let's see we have a name.
We have a description.
So let's create our description of type string.
What do we have next.
We have a number of what is this number of forks.
OK so let's say number.
Oops var.
Come on var.
Number of forks.
OK.
And that is going to be of type integer.
OK what's next we have the language.
So let's just say our language of type string.
Var contributor's So we'll just say number of contribute towards of type int and we have what else.
For the read me we need to know the U R L for the certain repository.
So let's go ahead and just say var Ribault u r l of type string.
Instead of casting it as a u r l it will be easier to use string.
And let's think what else.
This has everything a name description fork's language contributor's you r l o.
Each repo needs an image will download an image and save it in the repository as well and I'm going
to put it up here at the top var image of type II image but you'll notice that class is not coming in.
So we need to actually import it to access that and it's OK because you I can include foundation all
the same stuff is in there.
But we're getting an error now.
Class repo has no initializers.
Let's create one.
So let's go ahead and below.
Go ahead and type in it.
And we need to pass in every one of these variables and then set the variables using our initializers.
So go ahead and type image of type II image description of type string.
You know what I should do name before that name of type String comma description of type String comma
language comma number of forks of type int language of type string number of contributors of type ints
and repo you are l of type strings.
All right so there is all of our properties.
And now we can go ahead and set self image meaning the image from this class not from the initializer.
And we're going to set it to be whatever image we pass in we'll do the same thing for name is equal
to name self-caused description is equal to description self-doubt number of forks is equal to number
of forks.
Self-taught language is equal to language self-taught number of contributors is equal to number of contributors
and self.
Repo your L is equal to.
That's not real euro.
There we go.
OK so now we have properly initialized all of this.
But there is a big problem.
All of these variables are publicly accessible.
We can access them we can modify them.
We can change them from anywhere.
So we need to basically think about how we can be doing this.
And in the past we've used data encapsulation where we've typed you know private var image and then
down here we've said Whoops sorry with an underscore and then we'll save our image of type II image
return image.
Hey that's how we have basically set up private variables and we have a public accessor.
And that is one way to do it.
But there's actually a way easier way to do it.
That requires a lot less code.
And what we can do is we can set each variable itself to be public meaning we can access it from anywhere
but we can set the setter to be private so that it can only be set inside this class repo.
So this repo class.
So it's going to be a public variable with a private sector.
Whoops there we go.
And by doing this we create a variable that is public but we allow it to only be set inside the class
that it belongs to.
So to save time I'm going to copy this and paste it before each variable but we are basically saving
ourselves a ton of code.
This is a really cool thing that Swift can do.
We can set private variables are get a variable privately but access it publicly.
So if we build and run you'll notice there are no problems no errors.
We have done a great job now just to see how it works.
Let's go ahead and let's try to create an instance of this just below that we're not actually going
to use this but I just want to show you how you're going to call it so.
Var Ribault equals repo.
And if we use the parentheses there we can go ahead and access all these properties.
Image of type image name description number of fork's language contributors and repo you are l.
So we have successfully created our model layer for our repository.
All this information we're going to get from the get hub API which is really cool and publicly available
which is amazing and we're going to go ahead and move on to the next video where we're going to use
this repo class to basically connect dummy data to our table view and start displaying cells with dummy
data.
Pretty cool.
So let's go ahead and head over to the next video and let's start building that out.