Last active
August 29, 2015 13:59
-
-
Save catm705/10547367 to your computer and use it in GitHub Desktop.
API Parsing - Using Instagram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------------------------------------------------- | |
user.rb | |
--------------------------------------------------------------------- | |
# == Schema Information | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# name :string(255) | |
# email :string(255) | |
# password_digest :string(255) | |
# created_at :datetime | |
# updated_at :datetime | |
# image_url :string(255) | |
class User < ActiveRecord::Base | |
has_many :searches | |
has_many :favorites | |
has_secure_password | |
validates(:name, { :presence => true }) | |
validates(:email,{ :uniqueness => { case_sensitive: false }}) | |
validates(:password, { :length => { :minimum => 8, :maximum => 16 }, | |
:presence => true, | |
:confirmation => true }) | |
INSTAGRAM_ACCESS_TOKEN = ENV['INSTAGRAM_ACCESS_TOKEN'] --> #This token is saved as subl ~/.bash_profile | |
def self.tag_search(hashtag) | |
the_data = HTTParty.get('https://api.instagram.com/v1/tags/#{hashtag}/media/recent?access_token=#{INSTAGRAM_ACCESS_TOKEN}') | |
@my_urls = the_data["data"].map do |image| | |
image["images"]["low_resolution"]["url"] | |
end | |
end | |
def self.weather_request(hashtag) | |
the_data = HTTParty.get('http://api.worldweatheronline.com/free/v1/weather.ashx?q=#{hashtag}&format=json&num_of_days=5&key=52uj5t2aqbycwszq8kmqsa8y') | |
results = {} -->#to store values from API return that you want. | |
if the_data['data'].include?('error') | |
flash[:error] = the_data['data']['error'][0]['msg'] | |
return false | |
else | |
results[:weather_f] = the_data['data']['current_condition'][0]['temp_F'] | |
results[:weather_c] = the_data['data']['current_condition'][0]['temp_C'] | |
results[:weather_h] = the_data['data']['current_condition'][0]['humidity'] | |
results[:weather_desc] = the_data['data']['current_condition'][0]['weatherDesc'][0]['value'] | |
results[:weather_icon]=the_data['data']['current_condition'][0]['weatherIconUrl'][0]['value'] | |
return results | |
end | |
end | |
end | |
--------------------------------------------------------------------- | |
search_controller.rb | |
--------------------------------------------------------------------- | |
class SearchesController < ApplicationController | |
def index | |
@user = User.find(current_user.id) | |
#Calling method to call API | |
@last_search = @user.searches.last | |
@hashtag = @last_search.hashtag | |
@tag_pics = User.tag_search(@hashtag) | |
@weather_results = User.weather_request(@hashtag) | |
end | |
def show | |
end | |
def create | |
@user = User.find(current_user.id) | |
@hashtag = params[:hashtag].downcase | |
#This is a class method - so you have to call it on the Class itself 'User' | |
@tag_pics = User.tag_search(@hashtag) | |
if @weather_results = User.weather_request(@hashtag) | |
@search = Search.new(hashtag: @hashtag) | |
@search.save | |
@user.searches << @search | |
end | |
redirect_to searches_path | |
end | |
end | |
--------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment