• SIS Lab
  • >
  • Blog
  • >
  • undefined method `bytesize' for エラー。 -Instagramのプラグインが作りたい-

undefined method `bytesize' for エラー。 -Instagramのプラグインが作りたい-

更新日:2017.12.09 作成日:2011.12.15

Lokkaの プラグイン hello.rb を改造してみる。試しに、 Instagram プラグイン でも作ってみたいという願望。

でも、ここでエラーが。。

module Lokka
  module Hello
    def self.registered(app)
      app.get '/hello' do
        require 'uri'
        require 'net/https'
        require 'json'

        url = 'https://api.instagram.com/v1/users/{YOUR_ID}/media/recent'
        access_token = YOUR_ACCESS_TOKEN
        param = "access_token=#{access_token}"
        json = nil

        uri = URI.parse(url)
        https = Net::HTTP.new(uri.host,443)
        https.use_ssl = true
        https.start {
          response = https.get(uri.path + "?#{param}")
          json = JSON.parse(response.body)
        }

        json["data"].each do |item|
          print "#{item["id"]}"
        end
      end
    end
  end
end

undefined method `bytesize’ for # Hash:...

methodがないんじゃなくて、 sinatra の仕様みたい。試しに"hello"を返すようにしたらうまくいった。

Your getting this error because Sinatra takes the return value of a route and converts it into a string before trying to display it to the client. sinatra - error happens when I try “all” method in datamapper - Stack Overflow

module Lokka
  module Hello
    def self.registered(app)
      app.get '/hello' do
        require 'uri'
        require 'net/https'
        require 'json'

        url = 'https://api.instagram.com/v1/users/{YOUR_ID}/media/recent'
        access_token = YOUR_ACCESS_TOKEN
        param = "access_token=#{access_token}"
        json = nil

        uri = URI.parse(url)
        https = Net::HTTP.new(uri.host,443)
        https.use_ssl = true
        https.start {
          response = https.get(uri.path + "?#{param}")
          json = JSON.parse(response.body)
        }

        json["data"].each do |item|
          print "#{item["id"]}"
        end
        "hello"
      end
    end
  end
end

参考

Related contents