Sha256: 549028e79155b27d9a72abd56f84bd514963d6f90133b2fef184e60eca7ccf33

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

require "net/https"
require "json"
require "open-uri"
require "io/console"
require "colorize"
require "qiita_org/search_conf_path.rb"

class GetFileUrl
  def initialize(id, file, mode)
    @id = id
    @file = file
    @mode = (mode == "qiita" || mode == "open")? "public" : mode
    search = SearchConfPath.new(Dir.pwd, Dir.home)
    @conf_dir = search.search_conf_path()
    set_config()
  end

  def set_config()
    conf_path = File.join(@conf_dir, ".qiita.conf")
    @conf = JSON.load(File.read(conf_path))
    @access_token = @conf["access_token"]
    @teams_url = @conf["teams_url"]
  end

  def get_file_url()
    qiita = (@mode == "teams")? @teams_url : "https://qiita.com/"
    path = "api/v2/items/#{@id}"

    items = access_qiita(@access_token, qiita, path)

    file_url = items["body"].match(/\!\[#{@file}\]\(((.+))\)/)[2]
    return file_url

    #File.write("url_text.md", items["body"])
  end

  def access_qiita(access_token, qiita, path)
    uri = URI.parse(qiita + path)

    headers = { "Authorization" => "Bearer #{access_token}",
                "Content-Type" => "application/json" }

    response = URI.open(
      "#{uri}",
      "Authorization" => "#{headers["Authorization"]}",
    )
    items = JSON.parse(response.read)
    return items
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
qiita_org-0.1.22 lib/qiita_org/get_file_url.rb
qiita_org-0.1.21 lib/qiita_org/get_file_url.rb
qiita_org-0.1.20 lib/qiita_org/get_file_url.rb
qiita_org-0.1.19 lib/qiita_org/get_file_url.rb
qiita_org-0.1.18 lib/qiita_org/get_file_url.rb
qiita_org-0.1.17 lib/qiita_org/get_file_url.rb
qiita_org-0.1.16 lib/qiita_org/get_file_url.rb