万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    Railsでのjasonのパース

    →ネット調べても分かりにくいのでメモ。

    まずjsonを使えるようにしたいが

    sudo gem install json
    sudo gem install simple-json

    してもRailsに反映されなかったので
    プロジェクト直下のGemfileに※拡張子はないテキスト

    gem 'json'
    gem 'simple-json'
    

    を追加した。


    FacebookのFQLでの結果の取得。
    https://api.facebook.com/method/fql.query?query=SELECT name, sex, pic FROM user WHERE uid=1016196881
    これをcromeブラウザに入れた結果を取得
    https://api.facebook.com/method/fql.query?query=SELECT%20name,%20sex,%20pic%20FROM%20user%20WHERE%20uid=1016196881

    ・分解した。

        fql = "SELECT%20name,%20sex,%20pic%20FROM%20user%20WHERE%20uid=1016196881&format=json"
        json = "https://api.facebook.com/method/fql.query?query="+fql
    

    ・FQL結果を変数に入れたい

        src =''
        open(json) do |file|
          src = file.read
        end
    

    が、URLを単なる文字として認識してしまうみたいでエラーになるので'open-uri'を使う

        require 'open-uri'
        src =''
        open(json) do |file|
          src = file.read
        end
    

    結果のsrcをJSONでパースする。
    ×これはだめ

        require 'json'
        json = JSON.parser.new(src)
    

    なぜなら以下のようにsrcの中身にはには[]がついてる。

    [{"name":"\u8a31\u76f4\u4eba","sex":"male","pic":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-snc4\/41379_1016196881_4826_s.jpg"}] 

    力技だけど
    src[1..-2])こんなかんじで、最後と最後の'['と']'を無しで切り取る。

    json = JSON.parser.new(src[1..-2])
    

    これの結果のjson

    @jason=jason.parse()
    

    こうすれば完成


    hoge_controller.rb

      def index#
        require 'open-uri'
        fql = "SELECT%20name,%20sex,%20pic%20FROM%20user%20WHERE%20uid=1016196881&format=json"
        json = "https://api.facebook.com/method/fql.query?query="+fql
        src =''
        open(json) do |file|
          src = file.read
        end
        require 'json'
        json = JSON.parser.new(src[1..-2])
        @json = json.parse()['name']
        end
    

    tryとか例外処理をやってね

    ・view.rb

    <%= @json['name'] %>
    

    heokuの使い方

    ・手順
    http://devcenter.heroku.com/articles/quickstart
    ・herokuクライアントのインストール

    gem install heroku

    rails new hogeで作ったアプリの中に入って作業

    $ cd 自分の作ったアプリの中
    $ git init
    Initialized empty Git repository in .git/
    $ git add .
    $ git commit -m "new app"

    ・新たにherokuを作る。自分のページに追加されるよ

    →”注意”ランダムな名前になってしまう

     heroku create

    →これならhogeって名前がつく

     heroku create hoge


    ・新たに作成したものをgitに追加

    git add -A

    ・コミットする

    git commit -a -m 'scaffold'

    ・Herokuに反映

    git push heroku master

    ・herokuのデータベースも反映(これしないとエラーでる)

    heroku rake db:migrate


    Facebookアプリよくわかってないから復習

    wnagataの日記
    http://d.hatena.ne.jp/wnagata/?of=10

    Facebookアプリテストユーザー作成方法〜実践編
    http://blog.livedoor.jp/takah0918/archives/50175801.html