美文网首页
ruby教程之进行网络请求并解析数据

ruby教程之进行网络请求并解析数据

作者: 夏天然后 | 来源:发表于2023-01-16 22:33 被阅读0次

如何调试ruby请看 ruby教程之VSCode 运行调试ruby
demo请点击这里

1. 创建一个 Network 类, 在内部回调数据给类外使用, 类似于 iOS 的 Block

require 'uri'
require 'net/http'
require 'net/https'
require 'json'

#网络请求类
class Network    
    def Network.request (url, &block)
        uri = URI.parse(url)
        https = Net::HTTP.new(uri.host,uri.port)
        https.use_ssl = true
        req = Net::HTTP::Get.new(uri.path, initheader = {'Content-Type' =>'application/json'})
        req['Authorization'] = 'token token_Id'
        res = https.request(req)
        if res.code == "200"
            data = JSON.parse(res.body)
            # 回调data数据
            yield data
        else 
            puts "请求失败 : Response code = #{res.code}, message = #{res.message}"
        end
    end
end

2. 调用Network 并处理数据, 其中 |data|为回调的数据, 为此篇博客需要重要掌握的地方

Network.request(url) { | data |

    if data 
        #创建一个 names 数组
        names = Array.new
        #遍历得到每个 repo 的对象
        for i in 0..data.count
            repo = data[i]
            # 判断 repo 非空
            if repo
            # puts "full_name = #{repo["full_name"]}, i = #{i}"
            # 添加数据进入数组
            names << repo["full_name"]
            end
        end

        for i in 0..names.count - 1
            name = names[i]
            url = "https://api.github.com/repos/#{name}"
            # puts url
            Network.request(url) { | data |
                # 判断 repo 非空
                if data
                    puts "full_name = #{data["full_name"]}, created_at = #{data["created_at"]}, i = #{i}}"
                    sheet1.row(i + 1)[0] = data["full_name"]
                    sheet1.row(i + 1)[1] = data["html_url"]
                    sheet1.row(i + 1)[2] = data["description"]
                    sheet1.row(i + 1)[3] = data["forks_count"]
                    sheet1.row(i + 1)[4] = data["created_at"]
                    sheet1.row(i + 1)[5] = data["updated_at"]
                    sheet1.row(i + 1)[6] = data["pushed_at"]
                    sheet1.row(i + 1)[7] = data["stargazers_count"]
                    sheet1.row(i + 1)[8] = data["subscribers_count"]
                    sheet1.row(i + 1)[9] = data["open_issues_count"]
                    sheet1.row(i + 1)[10] = data["fork"]
                    sheet1.row(i + 1)[11] = data["has_wiki"]
                    sheet1.row(i + 1)[12] = data["has_pages"]
                    sheet1.row(i + 1)[13] = data["archived"]
                    sheet1.row(i + 1)[14] = data["disabled"]
                    if data["license"] 
                        license = data["license"]
                        name = license["name"]
                        sheet1.row(i + 1)[15] = name
                    end
                    sheet1.row(i + 1)[16] = data["language"]

                        if i == names.count - 1
                            puts "excel 已经导出成功, 请查看桌面"
                            #在指定路径下面创建表格,并写book对象
                            book.write "/Users/summerxx/Desktop/rubyExcel/summerxx专属表.xls"
                        end
                end
            }
        end
    end
}

相关文章

网友评论

      本文标题:ruby教程之进行网络请求并解析数据

      本文链接:https://www.haomeiwen.com/subject/alhthdtx.html