Friday, February 26, 2016

Week 5 - Movie Review app - Carrierwave, Fog, Rmagick, Fog-aws, Mini-magick

Click here to go to website
Yesterday I began the movie review app.  I was excited about this one.  I love love love movies and I love to see the art work too.  I love the way this app displays the posters in the index view so while I was making it, I decided to use fan art instead of the original posters so I could give the site a fresh but classic look.

While working on this app I had an issue with the search method.  The current way to do search is to use elastic search.  Its great and works real well.  It will do a search on all the text in the data that you are searching in.  Too bad heroku doesn't support elastic search... for free.  Yes they do support it, but you have to pay $10/month as of Feb 26, 2016.  That doesn't work for me so I used html to do search.

<%= form_tag search_movies_path, :method => 'get', class: "navbar-form", role: "search" do %>
<%= text_field_tag :search, params[:search], class: "form-control" %>
<%= submit_tag "Search", name: nil, class: "btn btn-default" %>

# I needed a search method in the corresponding movies controller.rb

 def search
  @movies = Movie.search(params[:search])
 end

# Then I created a search view search.html.erb

class="row">
  <% @movies.each do |movie| %>
  
class="col-sm-6 col-md-3">
    
class="thumbnail">
     <%= link_to (image_tag movie.image.url(:poster), class: 'image'), movie %>
   

<% end %>
The search is in the _header.html.erb partial
<%= form_tag search_movies_path, :method => 'get', class: "navbar-form", role: "search" do %>
<%= text_field_tag :search, params[:search], class: "form-control" %>
<%= submit_tag "Search", name: nil, class: "btn btn-default" %>

I needed a search method in the corresponding movies controller.rb
def search
@movies = Movie.search(params[:search])
end

Then I created a search view search.html.erb

class="row">
<% @movies.each do |movie| %>

class="col-sm-6 col-md-3">

class="thumbnail">
<%= link_to (image_tag movie.image.url(:poster), class: 'image'), movie %>
</div>

<% end %>
</div>

Here  is where I put my search results.  This search method is not the best. 
    If the search field is left blank, there are no results.
    You also have to spell whole words correctly. Iron Man will not return Ironman.

Looking forward to work with Elastic Search and any other gems that simplify my life.

Check out my github for this app.  Better yet see the app live

Features I would like to add:
1. Use an api to pull in imdb info
2. Reformat show view to a more landscape display of info.
3. Upload multiple poster.
4. Display multiple posters.
5. Show a trailer of the movie.
6. Use a better search method. Heroku has a free add-on called SearchBox Elasticsearch
7. Rich text formating

Until next week, same blog chanel, same blog time,

Kenyacode

Friday, February 19, 2016

Week 4 - Pinterest app part 2 - jQuery, Haml, Simple form, Bootstrap-sass, Paperclip

Click Here to go to Website
Ok, I had to fly out of town but I found a great co-working space in Chandler, AZ, just 20 min from Phoenix.  Stop by if you are ever in town.  Its called gangplank


So yesterday and today I have been working on changing this app from paperclip to carrierwave and setting the env variables.  I did some stupid rookie mistakes! I put code in a place where it didn't belong.  I was following a fix from stackoverflow that did not work.  I forgot to remove all the code from the fix that didn't work.  It kept throwing errors and I didn't figure it out for 2 hours.

Now that I got that fixed, I simply had to create a prod database on amazon s3 and set that in my env and on heroku config:set and I was up and running.

Note: make sure you use the same wording when setting vars. From your computer to your app to heroku.  I had 2 sets, one upper case and one lower case.  I now have 4 working apps and it only took me... 5 or 6 weeks.  Yikes!

I did have to help out some family here in Arizona for a few days but I'm still 2 weeks long.  Well the movie app is next and I'm excited about this one.  I have my own idea for a tv show review app that I really want to build.

Features I would like to add:
1. Search function
2. Multiple image upload. Auto img rotation on show view.
3. Social media shareability
4. Facebook log in.
5. Add a splash page.

See you next week,

Kenyacode

Tuesday, February 9, 2016

Week 4 - Pinterest app - jQuery, Haml, Simple form, Bootstrap-sass, Paperclip

It is week 4 on Mackenzie's 12 in 12 challenge.  It is my third week and this paperclip gem has been giving me a tough time.  I scrapped the whole app twice and started over.

This time I am using carrierwave gem with rmagick and fog.  Fog is made specifically to upload to Amazon S3.

I'm going to go through what I had to learn.

Having ENV variables:  This is a file on your computer that your apps can point to and get information that you don't want to have in your app.  More on why this is important later!

I didn't understand how it worked.  The directions said:

Since it's a bad practice to have your credentials in source code, you should load them from default fog configuration file:~/.fog. This file could look like this:
default:
  aws_access_key_id:     
  aws_secret_access_key: 
So there is a file called .fog that is in my directory and I should put my aws credentials there.

Ok, did that, app doesn't work.  I now know that the repo for fog needs to be a whole more descriptive.  Developers assume you know some things that they don't tell you and it is annoying reading these readme files.  Like for instance, the .fog file did not exist, I had to create it.  Also, the app needs you to specify the region and the bucket name.

Here is my slack conversation with a senior developer.

You'll need to provide your fog_credentials and a fog_directory (also known as a bucket) in an initializer.  For the sake of performance it is assumed that the directory already exists, so please create it if need be.  You can also pass in additional options, as documented fully in lib/carrierwave/storage/fog.rb. Here's a full example:

```ruby
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => 'xxx',                        # required
    :aws_secret_access_key  => 'yyy',                        # required
    :region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    :host                   => 's3.example.com',             # optional, defaults to nil
    :endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'name_of_directory'                     # required
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end
```



So I put my credentials in this file and pushed to github.  Some of you are cringing right now because you know what that means.  I realized a while later that my ENV variable go in place of 'AWS','XXX' and so forth.  The fact that the example has the actual region 'eu-west-1' made me think this is where those credentials go.  After I fixed this, the app was still throwing errors!

        
         Joshua Munene 9:27pm
I can’t gain access to amazon’s server
terminal

```AllAccessDisabledAll access to this object has been disabled209C1CF7E929B824Sj2Yrfph0TqpclltpXM0Sz46vNnPb4uR6w3AxfFez+X2hpVvqnml54pLhgQpIsfW1JTyzqK9yHE=```

Same error

Same error in the browser as before 403 forbidden


```Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)
excon.error.response
  :body          => "\nAllAccessDisabledAll access to this object has been disabled209C1CF7E929B824Sj2Yrfph0TqpclltpXM0Sz46vNnPb4uR6w3AxfFez+X2hpVvqnml54pLhgQpIsfW1JTyzqK9yHE="
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Wed, 03 Feb 2016 05:29:34 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "Sj2Yrfph0TqpclltpXM0Sz46vNnPb4uR6w3AxfFez+X2hpVvqnml54pLhgQpIsfW1JTyzqK9yHE="
    "x-amz-request-id" => "209C1CF7E929B824"
  }
  :local_address => "192.168.1.217"
  :local_port    => 56683
  :reason_phrase => "Forbidden"
  :remote_ip     => "54.231.16.216"
  :status        => 403
  :status_line   => "HTTP/1.1 403 Forbidden\r\n"
):
  app/controllers/recipes_controller.rb:20:in `create'
```

Joshua Munene [9:40 PM] any ideas???
Emmanuel Mwangi [9:41 PM] What is your CarrierWave config?
Joshua Munene [9:41 PM] 
```CarrierWave.configure do |config|
        config.fog_credentials = {
            provider:                                'AWS',
            aws_access_key_id:            ENV['AWS_ACCESS_KEY_ID'],
            aws_secret_access_key:    ENV['AWS_SECRET_ACCESS_KEY']
        }
        # To let CarrierWave work on Heroku
        config.cache_dir                    = "#{Rails.root}/tmp/uploads"
        config.fog_directory            = ENV['S3_BUCKET']
end

module CarrierWave
    module MiniMagick
        def quality(percentage)
            manipulate! do |img|
                img.quality(percentage.to_s)
                img = yield(img) if block_given?
                img
            end
        end
    end
end
```

so I made a directory at 
~/.fog
that is where I put my credentials. Inside is:

```aws_access_key_id: '(can't show you that)'
aws_secret_access_key: '(That one either)'
s3_bucket: 'recipe-box-dev'```

that is what the config.fog_credentials should be pointing to right?
Emmanuel Mwangi [9:47 PM] do this:
`env | grep AWS`
on the command line

Joshua Munene [9:48 PM] AWS_SECRET_ACCESS_KEY=(Hidden)
AWS_ACCESS_KEY_ID=(Hidden)

thats the output
Emmanuel Mwangi [9:48 PM] ok now
`env | grep S3`(edited)

Joshua Munene [9:49 PM] nothing outputs
Emmanuel Mwangi [9:49 PM] `cat ~/.bash_profile | grep S3`

Joshua Munene [9:50 PM] => export S3_BUCKET_NAME="recipe-box-dev"
Emmanuel Mwangi [10:06 PM] Then you just need to `source ~/.bash_profile`
Then `env | grep S3` should show you
`S3_BUCKET_NAME=recipe-box-dev`
Then rails should pick it up and everything should be good.

Here is when I learned about the .bash_profile and how it works with Iterm or terminal.  This is a file that has your ENV variables and you can use it to make sure that they are set correctly.

Can I just say how awesome it is to have friend who is a developer/jedi master. He illuminated my path as I was in the darkness, barely able to feel the force. But this padawan is learning.

Ok so earlier I told you that ENV variable are for info that your app needs but you can not put in your app.  There reason is that your aws credentials must be inputed into Heroku config manually so your app can use it.  NOT through a file in your app.  I made this mistake when I first put my credentials in the carrierwave.rb file.  I got a message from Amazon.  I'll let you read it.

Case ID: 1648488551
Subject: Your AWS account  is compromised
Severity: Low
Correspondence: Dear AWS Customer,

Your AWS Account is compromised! Please review the following notice and take immediate action to secure your account.


Your security is important to us. We have become aware that the AWS Access Key AKIAI3273*hidden along with the corresponding Secret Key is publicly available online at https://github.com/search?q=AKIAI3273PKPPCD3KFPQ&type=Code&utf8=%25E2%259C%2593.

This poses a security risk to your account and other users, could lead to excessive charges from unauthorized activity or abuse, and violates the AWS Customer Agreement.

We also believe that this credential exposure led to unauthorized activity in your account. 

Your current EC2 usage is about $1200 per day.

My eyes were fixated on that number after the $.  Whaaaaat

Good thing Amazon is so cool.  They congradulated me on fixing the problem so soon and started a refund request for me.  See the thing is I had no idea this was happening.  I pushed the file to github and then I knew it wasn't the right thing to do and deleted it.  I was reading this email 2 days later saying "Lord Jesus please handle this. We both know I ain't got no $1200"  So i'm still waiting to hear from Amazon as to the refund request.

So to recap, I switched the app from paperclip to carrierwave and the documentation for carrierwave is more detailed that paperclip.  (after finishing this app) I went back and read the paperclip gem and I believe I can use it on amazon s3.  But I understood it only after using carrierwave.

Sorry for some of the formatting issues.  The background makes it easier to read code, but copy and pasting my chat history from Slack comes with some color issues.

Kenyacode