Need help integrating youtube_it into my rails app - ruby-on-rails

I am pretty new to rails so this may be an easy question, but I was looking to create a Rails app that would use youtube in it. I have found that youtube_it seems to be the gem of choice for a task like this but I am having trouble using it. Basically, I want to use the gem so that I can get videos from specific users, i.e. Stanford University and create a list of those videos with links to another page that has the video information and player. To test this out I tried the follow code:
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :yt_client
private
def yt_client
#yt_client ||= YouTubeIt::Client.new(:dev_key => dev_key)
end
end
home_controller.rb
class HomeController < ApplicationController
def index
#playlists = yt_client.playlists('stanforduniversity')
end
end
index.html.erb
<h3>Home</h3>
<% #playlists.each do |playlist| %>
<p>Playlist: <%= playlist %></p>
<% end %>
The problem with this is all I get for output on my home page is a list of something like this: #
My questions then are: is there a way to change this output into an actual title? Am I doing something wrong/forgetting a step? Or should I just use python code to use the Google API and put all the videos into my database(I already have some code for this) and just access it using my rails app?
Hope this is clear enough.

it looks like what you want to print out is the name of the playlist - but that is an attribute of the playlist object, so you'll need something like:
<% #playlists.each do |playlist| %>
<p>Playlist: <%= playlist.title %></p>
<% end %>
otherwise ruby is trying to "print" the playlist object - which just doesn't work the way you expect.
Note: I've not used this gem either, I'm gathering this info from the gem docs here: https://github.com/kylejginavan/youtube_it

https://github.com/kylejginavan/youtube_it or youtube_it is the new version of youtube_g. This was forked and enhanced. If you need any enhancements please reach out to me.

you have a complete demo that I made here
http://youtube-it.heroku.com/
included source!

Related

Rendering an image from the Unsplash API in Rails

I have extensively researched this matter both on Stack Overflow and Google but found nothing conclusive. Since I'm completely new to the concept of API usage within Rails I have to ask for some advice.
I have followed the procedure from the github page
I have included the Unsplash helper in application_helper.rb as follows
def show_photo
Unsplash::Photo.find("tAKXap853rY")
end
and simply added
<%= image_tag show_photo %>
in my view.
This returns an object (So connectivity is good)
<img src="/images/#<Unsplash::Photo:0x007fc4b2f953c0>" alt="#
<unsplash::photo:0x007fc4b2f953c0>">
I'm aware that Rails is looking for a picture in the assets/images folder
How do I parse the inbound JSON and render it in my Rails view?
You can access to the urls key within the OpenStruct attributes in the Photo object that includes the raw, full, regular, small and thumb sizes, also as keys.
So, just to test you could use the raw one, like:
<%= image_tag Unsplash::Photo.find('tAKXap853rY')[:urls][:raw] %>
Or I think you could modify your method to accept one parameter which is the size key of the image, like:
module ApplicationHelper
def show_photo(size)
Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
end
Then:
<%= show_photo('raw') %> # 'full', 'regular', etc ...
further to this solution I am trying to display the photographer's name by using the user.name method.
In the console I can get the following :
photo = Unsplash::Photo.find("tAKXap853rY")
photo.user.name
will return
=> "Alejandro Escamilla".
But in RAILS :
def show_photo(size)
#photo =Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
just trying to display the name in my view like:
<%= #photo.user.name %> will return "user undefined method".
The .user.name is accessible in the console but not in rails! What have I missed? Thanks

ApplicationHelper not loaded in Rails 4 engine

Long time reader of SO here. I'm working on a Rails Engine. The big picture problem is that I get a NoMethodError on a helper method living in my Engine's ApplicationHelper. This is for work so I'm going to call the engine Blorge.
I have my helper method that is causing issues anywhere it is called. The Helper method is returning a NoMethodError. I thought maybe I needed to manually add helper Blorge::ApplicationHelper to Blorge::ApplicationController but the issue is still happening.
Am I missing something fundamental about Engines here?
Here is some actual code to give you a better idea of what I'm looking at.
index_header partial
app/views/blorge/shared/_index_header.html.erb
# require_locals is the helper method in question here
<% require_locals ['title'], local_assigns %>
<% title = title.pluralize %>
<section class="main_content-header">
<div class="main_content-header-wrapper">
<%= content_tag :h1, title %>
<div class="main_content-header-save">
<%= link_to "New #{title.singularize}", #new_path, class: "add-button" %>
</div>
</div>
</section>
Pages#home view
app/views/blorge/pages/home.html.erb
<%= render 'blorge/shared/index_header', title: "Welcome, #{current_user.full_name}" %>
...
Engine Application Helper
app/helpers/blorge/application_helper.rb
module Blorge
module ApplicationHelper
def require_locals(local_array, local_assigns)
local_array.each do |loc|
raise "#{loc} is a required local, please define it when you render this partial" unless local_assigns[loc.to_sym].present?
end
end
end
end
Engine Pages Controller
app/controller/blorge/pages_controller.rb
module Blorge
class PagesController < ApplicationController
def home
end
end
end
Engine Application Controller
app/controllers/blorge/application_controller.rb
class Blorge::ApplicationController < ActionController::Base
helper Blorge::ApplicationHelper
...
end
If I restart the server and reload the page, it will usually work just fine, and once it works, the issue doesn't come back for a couple days. After reading Helpers in Rails engine and Rails Engines: Helpers only are reloaded when restarting the server it sounds like I need to include the helper in my application controller with the to_prepare method in my engine.rb file. I am going to try this next but I most want to know if I'm missing something very basic here, If i do just have to add it to engine.rb, can someone explain why?
This might have been too much information, but I'd rather give more than not enough. Thanks in advance.
Edit
the fix seems to have been adding the helpers to application controller within engine.rb. I suspected this would be the fix, but I still have no clue why this is. Does anyone know why I should have to do this?
The Solution
config.to_prepare do
ApplicationController.helper(MyEngineHelper)
end

How to use "impressionist" gem in Rails?

I am new to rails dev and is looking to use https://github.com/charlotte-ruby/impressionist but not able to figure out how to use it looking at its documentation. I have done the migration and when trying to put
class ArticlesController < InheritedResources::Base
impressionist
end
OR
class ArticlesController < InheritedResources::Base
impressionist :actions=>[:show,:index]
end
it is throwing no method found error. I am unsure which code needs to be put in model, which to be in controller and which to be in views to see view_count, any help ?
the first question was a type
and as for your second question (inside the comment) the easiest but very efficient way would be
in the view:
<% #pages.each do |page| %>
<%= page.impressionist_count %>
<% end %>
You can also use Impression as a model to query stuff, just like normal models. For example,
Impression.where(user_id: 12).length
I find this is handy if you need some advanced queries.
The documentation does not specify to re-start server. This is not obvious as it does not apply to all gems, however it is required for impressionist.

Caching Twitter Results with Dalli

I'm new to Ruby (and Rails) and was hoping you could help clear up some confusion I'm experiencing.
I'm trying to integrate the Twitter gem into my website in order to get a user's latest tweet, and grab the link to their profile picture. The gem works great up until (what I think is) the 100th API call in an hour, after which Twitter will cut you off.
From what I've gathered, I need to cache the result for ~1 minute using memcache. There was some great pseudocode here but unfortunately, it was a bit over my head. I was hoping I could get some more specifics.
At the moment, I'm unsure where would I place that code? I want to display the twitter information in the application layout view, so would it go into a method in the application_helper.rb file?
My best attempt at figuring this out resulted in the following code, which throws a "Missing Helper File" error.
module ApplicationHelper
require "memcache"
def twitter
cache = MemCache.new
twitter = cache.get("twitter").first
if twitter.nil?
begin
twitter = Twitter.user("TwitterName")
cache.set("twitter", twitter, :expires_in => 1.minute) if twitter
rescue
twitter = default
end
end
return twitter
end
end
First enable caching and memcache for your environment (e.g. config/environments/production.rb)
# Use a different cache store in production
config.cache_store = :mem_cache_store
Then in the view you want to show tweets do something like this
<% cache("tweets", :expires_in => 42.minutes) do %>
<% Twitter.user_timeline("User").each do %>
.....
<% end %>
<% end %>

Basecamp API Rails

I was wondering if someone could do me massive favour..
I really don't understand how to make use of APIs - so I was wondering if, using Basecamp as an example, someone could talk me though the basics.
So far I have an application with a dashboard controller/view, I have put basecamp.rb into my /lib directory, added the following to my application_controller:
def basecamp_connect
Basecamp.establish_connection!('XXXXXX.basecamphq.com', 'USER', 'PASS', false)
#basecamp = Basecamp.new
end
Obviously changing the required parts to my credentials.
Next up I have added the following to my dashboard_controller:
def index
Basecamp::TodoList.find(:all)
end
Next I presume I have to somehow list the Todos on the dashboard using some sort of loop.
Am I doing the right thing, if so - how on earth do I display all the todo items and if not - what am I doing wrong/missing.
It doesn't have to be todos, anything from Basecamp or any other popular API service would be a good start. It's just that I happen to have a basecamp account!
Thanks,
Danny
Your view expects to have some variables defined. You can loop through those variables and display their content as you want.
So you could do, in your action :
def index
#list = Basecamp::TodoList.find(:all)
end
Then in your view you have access to the #list variable and you can to the following :
<ul>
<% #list.each do |item| %>
<li><%= item.to_json</li>
<% end %>
</ul>
Replacing the json dump by the elements as you wish to display them of course.
You might want to read the rails guides to get a lot more of informations.

Resources