LoginSignup
0
0

More than 5 years have passed since last update.

Rails newからHogestgram

Last updated at Posted at 2016-01-04

Rails newからHogestgram

概要

InstgramのAPIを仕様し任意のタグのついた画像を取得する
http://ec2-54-92-85-246.ap-northeast-1.compute.amazonaws.com:3000

Instgramとは

写真共有SNS
アクティブ数がtwitterを超えたらしい
http://gaiax-socialmedialab.jp/socialmedia/368

類似サービス(?)

ネコスタグラム
http://tuiken.jp/archives/2324339.html
イヌスタグラム
http://tuiken.jp/archives/2350615.html
ビジョスタグラム
http://tuiken.jp/archives/2382368.html

実装

Rrails new

rails new hogestgram
cd hogestgram

Gemを用意

Gemfileに

gem 'instagram'

を追加
bundle install

下記のGemを使用します。
https://github.com/Instagram/instagram-ruby-gem

表示ページを用意

$rails g controller home index
vim config/routes.rb
post 'home#index'
root 'home#index'

Instagram API_IDの取得

アプリの登録

1.https://instagram.com/developer/
2.[Register a new Client]ボタンを押下
3.もろもろ入力して登録

IDの取得

CLIENT ID:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CLIENT SECRET:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
をメモしておく

コントローラ作成

home_controller.rb
    Instagram.configure do |config|
      config.client_id = "CLIENT ID"
      config.client_secret = "CLIENT SECRET"
    end
    if params[:home_index] && params[:home_index][:search] != ""
      @image = Instagram.tag_recent_media(params[:home_index][:search], {:count =>20})
    end

view作成

<h1>Hogestagram</h1>
<div>
<%= form_for :home_index do |f| %>
  <p>
    <%= f.label :search, "search" %>
    <%= f.text_field :search %>
    <%= f.submit %>
  </p>
<% end %>
</div>

<% if @image.present? %>
  <% @image.each do |p| %>
    <%= link_to p.link, :target => "_blank" do %>
      <%= image_tag(p.images.low_resolution.url) %>
    <% end %>
  <% end %>
<% end %>

参考

instagram api 事始め access_tokenを取得する - Qiita
InstagramのAPIを使って画像をひっぱてくる - Qiita

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0