• 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 = <span class="synSpecial">'</span><span class="synConstant">https://api.instagram.com/v1/users/{YOUR_ID}/media/recent</span><span class="synSpecial">'</span>
    access_token = <span class="synType">YOUR_ACCESS_TOKEN</span>
    param = <span class="synSpecial">&quot;</span><span class="synConstant">access_token=</span><span class="synSpecial">#{</span>access_token<span class="synSpecial">}&quot;</span>
    json = <span class="synConstant">nil</span>

    uri = <span class="synType">URI</span>.parse(url)
    https = <span class="synType">Net</span>::<span class="synType">HTTP</span>.new(uri.host,<span class="synConstant">443</span>)
    https.use_ssl = <span class="synConstant">true</span>
    https.start {
      response = https.get(uri.path + <span class="synSpecial">&quot;</span><span class="synConstant">?</span><span class="synSpecial">#{</span>param<span class="synSpecial">}&quot;</span>)
      json = <span class="synType">JSON</span>.parse(response.body)
    }

    json[<span class="synSpecial">&quot;</span><span class="synConstant">data</span><span class="synSpecial">&quot;</span>].each <span class="synStatement">do</span> |<span class="synIdentifier">item</span>|
      print <span class="synSpecial">&quot;#{</span>item[<span class="synSpecial">&quot;</span><span class="synConstant">id</span><span class="synSpecial">&quot;</span>]<span class="synSpecial">}&quot;</span>
    <span class="synStatement">end</span>
  <span class="synStatement">end</span>
<span class="synPreProc">end</span>

end end

undefined method `bytesize' for #

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

<blockquote>
    <p>Your getting this error because <a class="keyword" href="http://d.hatena.ne.jp/keyword/Sinatra">Sinatra</a> takes the return <a class="keyword" href="http://d.hatena.ne.jp/keyword/value">value</a> of a route and converts it into a string before trying to display it to the client.<br />

sinatra - error happens when I try "all" method in datamapper - Stack Overflow

</blockquote>
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