yadisk-client 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/.rspec +3 -0
- data/.rubocop.yml +142 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +110 -0
- data/LICENSE +7 -0
- data/README.md +61 -0
- data/Rakefile +16 -0
- data/lib/yadisk/client.rb +44 -0
- data/lib/yadisk/collection.rb +26 -0
- data/lib/yadisk/error.rb +6 -0
- data/lib/yadisk/object.rb +25 -0
- data/lib/yadisk/objects/disk.rb +6 -0
- data/lib/yadisk/objects/link.rb +5 -0
- data/lib/yadisk/objects/operation.rb +5 -0
- data/lib/yadisk/objects/public.rb +6 -0
- data/lib/yadisk/objects/resource.rb +11 -0
- data/lib/yadisk/objects/trash.rb +6 -0
- data/lib/yadisk/resources/disk.rb +88 -0
- data/lib/yadisk/resources/operation.rb +10 -0
- data/lib/yadisk/resources/public.rb +17 -0
- data/lib/yadisk/resources/trash.rb +20 -0
- data/lib/yadisk/rest.rb +58 -0
- data/lib/yadisk/version.rb +9 -0
- data/lib/yadisk.rb +35 -0
- data/spec/lib/yadisk/client_spec.rb +19 -0
- data/spec/lib/yadisk/disk_spec.rb +226 -0
- data/spec/lib/yadisk/errors_spec.rb +16 -0
- data/spec/lib/yadisk/operations_spec.rb +23 -0
- data/spec/lib/yadisk/public_spec.rb +51 -0
- data/spec/lib/yadisk/trash_spec.rb +72 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/cassette_helpers.rb +23 -0
- data/spec/test_data/test_file.txt +1 -0
- data/spec/vcr/disk/DELETE_delete.yml +50 -0
- data/spec/vcr/disk/ERROR_info.yml +48 -0
- data/spec/vcr/disk/GET_download.yml +52 -0
- data/spec/vcr/disk/GET_get_meta.yml +52 -0
- data/spec/vcr/disk/GET_info.yml +52 -0
- data/spec/vcr/disk/GET_list_public.yml +52 -0
- data/spec/vcr/disk/GET_upload.yml +48 -0
- data/spec/vcr/disk/PATCH_update_meta.yml +54 -0
- data/spec/vcr/disk/POST_copy.yml +50 -0
- data/spec/vcr/disk/POST_move.yml +50 -0
- data/spec/vcr/disk/POST_upload_by_url.yml +52 -0
- data/spec/vcr/disk/PUT_create_dir.yml +50 -0
- data/spec/vcr/disk/PUT_publish.yml +54 -0
- data/spec/vcr/disk/PUT_unpublish.yml +54 -0
- data/spec/vcr/operations/GET_status.yml +52 -0
- data/spec/vcr/trash/DELETE_delete.yml +50 -0
- data/spec/vcr/trash/DELETE_delete_error.yml +49 -0
- data/spec/vcr/trash/GET_list.yml +51 -0
- data/spec/vcr/trash/PUT_restore.yml +51 -0
- data/spec/vcr_setup.rb +35 -0
- data/yadisk.gemspec +26 -0
- metadata +146 -0
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Yadisk
         | 
| 4 | 
            +
              class TrashRest < Rest
         | 
| 5 | 
            +
                def delete(path:, **params)
         | 
| 6 | 
            +
                  path = Addressable::URI.parse(path).normalize
         | 
| 7 | 
            +
                  Link.new delete_request("disk/trash/resources?path=#{path}", params: params).body
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def list(path:, **params)
         | 
| 11 | 
            +
                  path = Addressable::URI.parse(path).normalize
         | 
| 12 | 
            +
                  TrashResource.new get_request("disk/trash/resources?path=#{path}", params: params).body
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def restore(path:)
         | 
| 16 | 
            +
                  path = Addressable::URI.parse(path).normalize
         | 
| 17 | 
            +
                  Link.new put_request("disk/trash/resources/restore?path=#{path}", body: "").body
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
    
        data/lib/yadisk/rest.rb
    ADDED
    
    | @@ -0,0 +1,58 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Yadisk
         | 
| 4 | 
            +
              class Rest
         | 
| 5 | 
            +
                attr_reader :client
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize(client)
         | 
| 8 | 
            +
                  @client = client
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                private
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def get_request(url, params: {}, headers: {})
         | 
| 14 | 
            +
                  handle_response client.connection.get(url, params, headers)
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def post_request(url, body:, headers: {})
         | 
| 18 | 
            +
                  handle_response client.connection.post(url, body, headers)
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                def patch_request(url, body:, headers: {})
         | 
| 22 | 
            +
                  handle_response client.connection.patch(url, body, headers)
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def put_request(url, body:, headers: {})
         | 
| 26 | 
            +
                  handle_response client.connection.put(url, body, headers)
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def delete_request(url, params: {}, headers: {})
         | 
| 30 | 
            +
                  handle_response client.connection.delete(url, params, headers)
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                # rubocop:disable Metrics/CyclomaticComplexity
         | 
| 34 | 
            +
                # rubocop:disable Metrics/AbcSize
         | 
| 35 | 
            +
                def handle_response(response)
         | 
| 36 | 
            +
                  case response.status
         | 
| 37 | 
            +
                  when 400
         | 
| 38 | 
            +
                    raise Error, "Your request was malformed. #{response.body['error']}"
         | 
| 39 | 
            +
                  when 401
         | 
| 40 | 
            +
                    raise Error, "You did not supply valid authentication credentials. #{response.body['error']}"
         | 
| 41 | 
            +
                  when 403
         | 
| 42 | 
            +
                    raise Error, "You are not allowed to perform that action. #{response.body['error']}"
         | 
| 43 | 
            +
                  when 404
         | 
| 44 | 
            +
                    raise Error, "No results were found for your request. #{response.body['error']}"
         | 
| 45 | 
            +
                  when 429
         | 
| 46 | 
            +
                    raise Error, "Your request exceeded the API rate limit. #{response.body['error']}"
         | 
| 47 | 
            +
                  when 500
         | 
| 48 | 
            +
                    raise Error, "We were unable to perform the request due to server-side problems. #{response.body['error']}"
         | 
| 49 | 
            +
                  when 503
         | 
| 50 | 
            +
                    raise Error, "You have been rate limited for sending more  requests per second. #{response.body['error']}"
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  response
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
                # rubocop:enable Metrics/AbcSize
         | 
| 56 | 
            +
                # rubocop:enable Metrics/CyclomaticComplexity
         | 
| 57 | 
            +
              end
         | 
| 58 | 
            +
            end
         | 
    
        data/lib/yadisk.rb
    ADDED
    
    | @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "addressable/uri"
         | 
| 4 | 
            +
            require "faraday"
         | 
| 5 | 
            +
            require "faraday_middleware"
         | 
| 6 | 
            +
            require "yadisk/version"
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            module Yadisk
         | 
| 9 | 
            +
              autoload :Client, "yadisk/client"
         | 
| 10 | 
            +
              autoload :Object, "yadisk/object"
         | 
| 11 | 
            +
              autoload :Error,  "yadisk/error"
         | 
| 12 | 
            +
              autoload :Rest,   "yadisk/rest"
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              autoload :Collection, "yadisk/collection"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              autoload :Operation, "yadisk/objects/operation"
         | 
| 17 | 
            +
              autoload :OperationRest, "yadisk/resources/operation"
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              autoload :PublicRest, "yadisk/resources/public"
         | 
| 20 | 
            +
              autoload :PublicResource, "yadisk/objects/public"
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              autoload :Trash, "yadisk/objects/trash"
         | 
| 23 | 
            +
              autoload :TrashResource, "yadisk/objects/trash"
         | 
| 24 | 
            +
              autoload :TrashRest, "yadisk/resources/trash"
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              autoload :Disk, "yadisk/objects/disk"
         | 
| 27 | 
            +
              autoload :DiskRest, "yadisk/resources/disk"
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              autoload :Link, "yadisk/objects/link"
         | 
| 30 | 
            +
              autoload :Resource, "yadisk/objects/resource"
         | 
| 31 | 
            +
              autoload :ResourceUploadLink, "yadisk/objects/resource"
         | 
| 32 | 
            +
              autoload :LastUploadedResourceList, "yadisk/objects/resource"
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              autoload :Operation, "yadisk/objects/operation"
         | 
| 35 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe Yadisk::Client do
         | 
| 6 | 
            +
              context "client with token: test" do
         | 
| 7 | 
            +
                it "returns correct instance of Yadisk::Client" do
         | 
| 8 | 
            +
                  expect(@yandex).to be_kind_of(Yadisk::Client)
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              context "client without token" do
         | 
| 13 | 
            +
                let(:client) { Yadisk::Client.new }
         | 
| 14 | 
            +
                it "returns nil" do
         | 
| 15 | 
            +
                  expect { client }.to raise_error(ArgumentError)
         | 
| 16 | 
            +
                    .with_message("missing keyword: token")
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,226 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # e2e vars
         | 
| 6 | 
            +
            # test_file_path = "spec/test_data/test_file.txt"
         | 
| 7 | 
            +
            test_file_name = "remote_file.txt"
         | 
| 8 | 
            +
            test_upload_link = ""
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            # rubocop:disable Metrics/BlockLength
         | 
| 11 | 
            +
            describe Yadisk::Client do
         | 
| 12 | 
            +
              # TODO: refactor by separating test cases - HOW TO DO THIS ?
         | 
| 13 | 
            +
              # TODO: describe > context > it
         | 
| 14 | 
            +
              context "#disk" do
         | 
| 15 | 
            +
                it "#info", :vcr do
         | 
| 16 | 
            +
                  name = "disk/GET_info"
         | 
| 17 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 18 | 
            +
                    resp = @yandex.disk.info
         | 
| 19 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 20 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk")
         | 
| 21 | 
            +
                      expect(c.code).to eq(200)
         | 
| 22 | 
            +
                      expect(c.method).to eq("get")
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Disk)
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                it "#upload", :vcr do
         | 
| 29 | 
            +
                  name = "disk/GET_upload"
         | 
| 30 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 31 | 
            +
                    resp = @yandex.disk.upload(path: test_file_name)
         | 
| 32 | 
            +
                    test_upload_link = resp.href
         | 
| 33 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 34 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/upload?path=remote_file.txt")
         | 
| 35 | 
            +
                      expect(c.code).to eq(200)
         | 
| 36 | 
            +
                      expect(c.method).to eq("get")
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                    expect(resp).to be_instance_of(Yadisk::ResourceUploadLink)
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                # it "#upload_from_file", :vcr do
         | 
| 43 | 
            +
                #   name = "disk/PUT_upload_from_file"
         | 
| 44 | 
            +
                #   VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 45 | 
            +
                #     @yandex.disk.upload_from_file(upload_link: test_upload_link, src: test_file_path)
         | 
| 46 | 
            +
                #     CassetteHelpers::Use.new(name) do |c|
         | 
| 47 | 
            +
                #       expect(c.url).to eq(upload_link)
         | 
| 48 | 
            +
                #       expect(c.code).to eq(201)
         | 
| 49 | 
            +
                #       expect(c.method).to eq("put")
         | 
| 50 | 
            +
                #     end
         | 
| 51 | 
            +
                #   end
         | 
| 52 | 
            +
                # end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                it "#upload_by_url", :vcr do
         | 
| 55 | 
            +
                  name = "disk/POST_upload_by_url"
         | 
| 56 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 57 | 
            +
                    path = "uploaded_by_url.png"
         | 
| 58 | 
            +
                    link = "https://www.example.com/image.png"
         | 
| 59 | 
            +
                    @yandex.disk.upload_by_url(path: path, url: link)
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 62 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/upload?path=#{path}&url=#{link}")
         | 
| 63 | 
            +
                      expect(c.code).to eq(202)
         | 
| 64 | 
            +
                      expect(c.method).to eq("post")
         | 
| 65 | 
            +
                    end
         | 
| 66 | 
            +
                  end
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                it "#get_meta", :vcr do
         | 
| 70 | 
            +
                  name = "disk/GET_get_meta"
         | 
| 71 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 72 | 
            +
                    resp = @yandex.disk.get_meta(path: "remote_file.txt")
         | 
| 73 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 74 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources?path=/remote_file.txt")
         | 
| 75 | 
            +
                      expect(c.code).to eq(200)
         | 
| 76 | 
            +
                      expect(c.method).to eq("get")
         | 
| 77 | 
            +
                    end
         | 
| 78 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Resource)
         | 
| 79 | 
            +
                  end
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                it "#update_meta", :vcr do
         | 
| 83 | 
            +
                  name = "disk/PATCH_update_meta"
         | 
| 84 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 85 | 
            +
                    resp = @yandex.disk.update_meta(path: "remote_file.txt", key_one: "value_1", key_two: "value_2")
         | 
| 86 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 87 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources")
         | 
| 88 | 
            +
                      expect(c.code).to eq(200)
         | 
| 89 | 
            +
                      expect(c.method).to eq("patch")
         | 
| 90 | 
            +
                    end
         | 
| 91 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Resource)
         | 
| 92 | 
            +
                  end
         | 
| 93 | 
            +
                end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                it "#create_dir", :vcr do
         | 
| 96 | 
            +
                  name = "disk/PUT_create_dir"
         | 
| 97 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 98 | 
            +
                    resp = @yandex.disk.create_dir(path: "test_dir")
         | 
| 99 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 100 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources")
         | 
| 101 | 
            +
                      expect(c.code).to eq(200)
         | 
| 102 | 
            +
                      expect(c.method).to eq("get")
         | 
| 103 | 
            +
                    end
         | 
| 104 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 105 | 
            +
                  end
         | 
| 106 | 
            +
                end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                it "#copy", :vcr do
         | 
| 109 | 
            +
                  name = "disk/POST_copy"
         | 
| 110 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 111 | 
            +
                    resp = @yandex.disk.copy(from: "remote_file.txt", to: "remote_file_copy.txt")
         | 
| 112 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 113 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/copy")
         | 
| 114 | 
            +
                      expect(c.code).to eq(200)
         | 
| 115 | 
            +
                      expect(c.method).to eq("post")
         | 
| 116 | 
            +
                    end
         | 
| 117 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 118 | 
            +
                  end
         | 
| 119 | 
            +
                end
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                it "#download", :vcr do
         | 
| 122 | 
            +
                  name = "disk/GET_download"
         | 
| 123 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 124 | 
            +
                    resp = @yandex.disk.download(path: "remote_file.txt")
         | 
| 125 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 126 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources")
         | 
| 127 | 
            +
                      expect(c.code).to eq(200)
         | 
| 128 | 
            +
                      expect(c.method).to eq("get")
         | 
| 129 | 
            +
                    end
         | 
| 130 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 131 | 
            +
                  end
         | 
| 132 | 
            +
                end
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                # it "#list_files", :vcr do
         | 
| 135 | 
            +
                #   name = "disk/GET_list_files"
         | 
| 136 | 
            +
                #   VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 137 | 
            +
                #     @yandex.disk.list_files
         | 
| 138 | 
            +
                #     CassetteHelpers::Use.new(name) do |c|
         | 
| 139 | 
            +
                #       expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources")
         | 
| 140 | 
            +
                #       expect(c.code).to eq(200)
         | 
| 141 | 
            +
                #       expect(c.method).to eq("get")
         | 
| 142 | 
            +
                #     end
         | 
| 143 | 
            +
                #     expect(resp).to be_instance_of(Yadisk::Disk)
         | 
| 144 | 
            +
                #   end
         | 
| 145 | 
            +
                # end
         | 
| 146 | 
            +
             | 
| 147 | 
            +
                # TODO: filter sensitive data
         | 
| 148 | 
            +
                # it "#last_uploaded", :vcr do
         | 
| 149 | 
            +
                #   name = "disk/GET_last_uploaded"
         | 
| 150 | 
            +
                #   VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 151 | 
            +
                #     resp = @yandex.disk.last_uploaded
         | 
| 152 | 
            +
                #     CassetteHelpers::Use.new(name) do |c|
         | 
| 153 | 
            +
                #       expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources")
         | 
| 154 | 
            +
                #       expect(c.code).to eq(200)
         | 
| 155 | 
            +
                #       expect(c.method).to eq("get")
         | 
| 156 | 
            +
                #     end
         | 
| 157 | 
            +
                #     expect(resp).to be_instance_of(Yadisk::Collection)
         | 
| 158 | 
            +
                #   end
         | 
| 159 | 
            +
                # end
         | 
| 160 | 
            +
             | 
| 161 | 
            +
                it "#move", :vcr do
         | 
| 162 | 
            +
                  name = "disk/POST_move"
         | 
| 163 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 164 | 
            +
                    resp = @yandex.disk.move(from: "remote_file_copy.txt", to: "remote_file_new_name.txt")
         | 
| 165 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 166 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/move")
         | 
| 167 | 
            +
                      expect(c.code).to eq(200)
         | 
| 168 | 
            +
                      expect(c.method).to eq("post")
         | 
| 169 | 
            +
                    end
         | 
| 170 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 171 | 
            +
                  end
         | 
| 172 | 
            +
                end
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                it "#list_public", :vcr do
         | 
| 175 | 
            +
                  name = "disk/GET_list_public"
         | 
| 176 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 177 | 
            +
                    resp = @yandex.disk.list_public
         | 
| 178 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 179 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/public")
         | 
| 180 | 
            +
                      expect(c.code).to eq(200)
         | 
| 181 | 
            +
                      expect(c.meth).to eq("get")
         | 
| 182 | 
            +
                    end
         | 
| 183 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Collection)
         | 
| 184 | 
            +
                  end
         | 
| 185 | 
            +
                end
         | 
| 186 | 
            +
             | 
| 187 | 
            +
                it "#publish", :vcr do
         | 
| 188 | 
            +
                  name = "disk/PUT_publish"
         | 
| 189 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 190 | 
            +
                    resp = @yandex.disk.publish(path: test_file_name)
         | 
| 191 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 192 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/publish")
         | 
| 193 | 
            +
                      expect(c.code).to eq(200)
         | 
| 194 | 
            +
                      expect(c.method).to eq("put")
         | 
| 195 | 
            +
                    end
         | 
| 196 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 197 | 
            +
                  end
         | 
| 198 | 
            +
                end
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                it "#unpublish", :vcr do
         | 
| 201 | 
            +
                  name = "disk/PUT_unpublish"
         | 
| 202 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 203 | 
            +
                    resp = @yandex.disk.unpublish(path: test_file_name)
         | 
| 204 | 
            +
                    CassetteHelpers::Use.new(name).url do |c|
         | 
| 205 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources/unpublish")
         | 
| 206 | 
            +
                      expect(c.code).to eq(200)
         | 
| 207 | 
            +
                      expect(c.method).to eq("put")
         | 
| 208 | 
            +
                    end
         | 
| 209 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 210 | 
            +
                  end
         | 
| 211 | 
            +
                end
         | 
| 212 | 
            +
             | 
| 213 | 
            +
                it "#delete", :vcr do
         | 
| 214 | 
            +
                  name = "disk/DELETE_delete"
         | 
| 215 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 216 | 
            +
                    @yandex.disk.delete(path: test_file_name)
         | 
| 217 | 
            +
                    CassetteHelpers::Use.new(name).url do |c|
         | 
| 218 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources?path=remote_file.txt")
         | 
| 219 | 
            +
                      expect(c.code).to eq(204)
         | 
| 220 | 
            +
                      expect(c.method).to eq("delete")
         | 
| 221 | 
            +
                    end
         | 
| 222 | 
            +
                  end
         | 
| 223 | 
            +
                end
         | 
| 224 | 
            +
              end
         | 
| 225 | 
            +
            end
         | 
| 226 | 
            +
            # rubocop:enable Metrics/BlockLength
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe Yadisk::Client do
         | 
| 6 | 
            +
              context "client with bad credentials.", :vcr do
         | 
| 7 | 
            +
                let(:client) { Yadisk::Client.new(token: "very bad token").disk.info }
         | 
| 8 | 
            +
                let(:name) { "disk/ERROR_info" }
         | 
| 9 | 
            +
                it "UnauthorizedError" do
         | 
| 10 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 11 | 
            +
                    expect { client }.to raise_error(Yadisk::Error)
         | 
| 12 | 
            +
                      .with_message(/did not supply valid authentication/)
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe Yadisk::Client do
         | 
| 6 | 
            +
              # TODO: describe > context > it
         | 
| 7 | 
            +
              context "#operations" do
         | 
| 8 | 
            +
                it "#status", :vcr do
         | 
| 9 | 
            +
                  name = "operations/GET_status"
         | 
| 10 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 11 | 
            +
                    id = "a6e706d94c4adcd17298c1eed06729f9e9217569e6376a51c38de74d8d6367c5"
         | 
| 12 | 
            +
                    resp = @yandex.operation.status(operation_id: id)
         | 
| 13 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 14 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/operations/#{id}")
         | 
| 15 | 
            +
                      expect(c.code).to eq(200)
         | 
| 16 | 
            +
                      expect(c.method).to eq("get")
         | 
| 17 | 
            +
                    end
         | 
| 18 | 
            +
                    expect(resp).to be_instance_of(String)
         | 
| 19 | 
            +
                    expect(resp).to eq("in-progress")
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # TODO: make specs code cleaner
         | 
| 6 | 
            +
            # TODO: describe > context > it
         | 
| 7 | 
            +
            describe Yadisk::Client do
         | 
| 8 | 
            +
              context "#public" do
         | 
| 9 | 
            +
                it "#meta", :vcr do
         | 
| 10 | 
            +
                  name = "public/GET_meta"
         | 
| 11 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 12 | 
            +
                    url = "https://disk.yandex.ru/d/Th-o2SkdjoALxA"
         | 
| 13 | 
            +
                    resp = @yandex.public_resource.meta(public_url: url)
         | 
| 14 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 15 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/operations/#{id}")
         | 
| 16 | 
            +
                      expect(c.code).to eq(200)
         | 
| 17 | 
            +
                      expect(c.method).to eq("get")
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
                    expect(resp).to be_instance_of(Yadisk::PublicResource)
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                it "#link", :vcr do
         | 
| 24 | 
            +
                  name = "public/GET_link"
         | 
| 25 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 26 | 
            +
                    url = "https://disk.yandex.ru/d/Th-o2SkdjoALxA"
         | 
| 27 | 
            +
                    resp = @yandex.public_resource.link(public_url: url)
         | 
| 28 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 29 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/operations/#{id}")
         | 
| 30 | 
            +
                      expect(c.code).to eq(200)
         | 
| 31 | 
            +
                      expect(c.method).to eq("get")
         | 
| 32 | 
            +
                    end
         | 
| 33 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                it "#save", :vcr do
         | 
| 38 | 
            +
                  name = "public/POST_save"
         | 
| 39 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 40 | 
            +
                    url = "https://disk.yandex.ru/d/Th-o2SkdjoALxA"
         | 
| 41 | 
            +
                    resp = @yandex.public_resource.save(public_url: url)
         | 
| 42 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 43 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/operations/#{id}")
         | 
| 44 | 
            +
                      expect(c.code).to eq(200)
         | 
| 45 | 
            +
                      expect(c.method).to eq("post")
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
            end
         | 
| @@ -0,0 +1,72 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "spec_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe Yadisk::Client do
         | 
| 6 | 
            +
              let(:file_path) { "trash:/remote_path.txt_05572eeb69f47093eac51b40f1513d3bddff1b6c" }
         | 
| 7 | 
            +
              context "#trash" do
         | 
| 8 | 
            +
                it "#list - all", :vcr do
         | 
| 9 | 
            +
                  name = "trash/GET_list"
         | 
| 10 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 11 | 
            +
                    resp = @yandex.trash.list(path: "/")
         | 
| 12 | 
            +
                    CassetteHelpers::Use.new(name).url do |c|
         | 
| 13 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/trash/resources")
         | 
| 14 | 
            +
                      expect(c.code).to eq(200)
         | 
| 15 | 
            +
                      expect(c.method).to eq("get")
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                    expect(resp).to be_instance_of(Yadisk::TrashResource)
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                it "#list - single file", :vcr do
         | 
| 22 | 
            +
                  name = "trash/GET_list"
         | 
| 23 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 24 | 
            +
                    resp = @yandex.trash.list(path: file_path)
         | 
| 25 | 
            +
                    CassetteHelpers::Use.new(name).url do |c|
         | 
| 26 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/trash/resources")
         | 
| 27 | 
            +
                      expect(c.code).to eq(200)
         | 
| 28 | 
            +
                      expect(c.method).to eq("get")
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
                    expect(resp).to be_instance_of(Yadisk::TrashResource)
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                it "#restore", :vcr do
         | 
| 35 | 
            +
                  name = "trash/PUT_restore"
         | 
| 36 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 37 | 
            +
                    resp = @yandex.trash.restore(path: file_path)
         | 
| 38 | 
            +
                    CassetteHelpers::Use.new(name).url do |c|
         | 
| 39 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/resources?path=remote_path.txt")
         | 
| 40 | 
            +
                      expect(c.code).to eq(204)
         | 
| 41 | 
            +
                      expect(c.method).to eq("put")
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                it "#delete - ok", :vcr do
         | 
| 48 | 
            +
                  name = "trash/DELETE_delete"
         | 
| 49 | 
            +
                  VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 50 | 
            +
                    resp = @yandex.trash.delete(path: "trash:/remote_file.txt_f7fc32168f3b30e60994fd2e82af7d230dab5c91")
         | 
| 51 | 
            +
                    CassetteHelpers::Use.new(name) do |c|
         | 
| 52 | 
            +
                      expect(c.url).to eq("https://cloud-api.yandex.net/v1/disk/trash/resources")
         | 
| 53 | 
            +
                      expect(c.code).to eq(200)
         | 
| 54 | 
            +
                      expect(c.method).to eq("delete")
         | 
| 55 | 
            +
                    end
         | 
| 56 | 
            +
                    expect(resp).to be_instance_of(Yadisk::Link)
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                # if error raised
         | 
| 61 | 
            +
                describe "#delete - DiskNotFoundError" do
         | 
| 62 | 
            +
                  let(:request) { @yandex.trash.delete(path: file_path) }
         | 
| 63 | 
            +
                  it "#delete - file not found", :vcr do
         | 
| 64 | 
            +
                    name = "trash/DELETE_delete_error"
         | 
| 65 | 
            +
                    VCR.use_cassette(name, allow_playback_repeats: true) do
         | 
| 66 | 
            +
                      expect { request }.to raise_error(Yadisk::Error)
         | 
| 67 | 
            +
                        .with_message(/DiskNotFoundError/)
         | 
| 68 | 
            +
                    end
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "./spec/support/cassette_helpers" # VCR helpers
         | 
| 4 | 
            +
            require File.expand_path("./vcr_setup", __dir__)
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
         | 
| 7 | 
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            require_relative "../lib/yadisk"
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            RSpec.configure do |config|
         | 
| 12 | 
            +
              config.include CassetteHelpers
         | 
| 13 | 
            +
              config.before(:each) do
         | 
| 14 | 
            +
                @yandex = Yadisk::Client.new(token: "your_access_token_for_e2e")
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              config.expect_with :rspec do |c|
         | 
| 18 | 
            +
                c.syntax = %i[expect]
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              config.mock_with :rspec do |c|
         | 
| 22 | 
            +
                c.syntax = %i[expect]
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module CassetteHelpers
         | 
| 4 | 
            +
              class Use
         | 
| 5 | 
            +
                def initialize(name)
         | 
| 6 | 
            +
                  path = "spec/vcr/#{name}.yml"
         | 
| 7 | 
            +
                  @cassette = YAML.load_file(path) if File.exist?(path)
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                # helper method for match request URL from cassette
         | 
| 11 | 
            +
                def url
         | 
| 12 | 
            +
                  @cassette["http_interactions"].first["request"]["uri"]
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def method
         | 
| 16 | 
            +
                  @cassette["http_interactions"].first["request"]["method"]
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def code
         | 
| 20 | 
            +
                  @cassette["http_interactions"].first["response"]["status"]["code"]
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            test upload method
         | 
| @@ -0,0 +1,50 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            http_interactions:
         | 
| 3 | 
            +
            - request:
         | 
| 4 | 
            +
                method: delete
         | 
| 5 | 
            +
                uri: https://cloud-api.yandex.net/v1/disk/resources?path=remote_file.txt
         | 
| 6 | 
            +
                body:
         | 
| 7 | 
            +
                  encoding: US-ASCII
         | 
| 8 | 
            +
                  string: ''
         | 
| 9 | 
            +
                headers:
         | 
| 10 | 
            +
                  User-Agent:
         | 
| 11 | 
            +
                  - Faraday v1.10.2
         | 
| 12 | 
            +
                  Authorization:
         | 
| 13 | 
            +
                  - "[**********]"
         | 
| 14 | 
            +
                  Accept-Encoding:
         | 
| 15 | 
            +
                  - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
         | 
| 16 | 
            +
                  Accept:
         | 
| 17 | 
            +
                  - "*/*"
         | 
| 18 | 
            +
              response:
         | 
| 19 | 
            +
                status:
         | 
| 20 | 
            +
                  code: 204
         | 
| 21 | 
            +
                  message: NO CONTENT
         | 
| 22 | 
            +
                headers:
         | 
| 23 | 
            +
                  Server:
         | 
| 24 | 
            +
                  - nginx
         | 
| 25 | 
            +
                  Date:
         | 
| 26 | 
            +
                  - Wed, 05 Oct 2022 14:32:20 GMT
         | 
| 27 | 
            +
                  Content-Type:
         | 
| 28 | 
            +
                  - application/json; charset=utf-8
         | 
| 29 | 
            +
                  Content-Length:
         | 
| 30 | 
            +
                  - '0'
         | 
| 31 | 
            +
                  Connection:
         | 
| 32 | 
            +
                  - keep-alive
         | 
| 33 | 
            +
                  Access-Control-Allow-Methods:
         | 
| 34 | 
            +
                  - PUT, PATCH, DELETE, GET, OPTIONS
         | 
| 35 | 
            +
                  Access-Control-Allow-Credentials:
         | 
| 36 | 
            +
                  - 'true'
         | 
| 37 | 
            +
                  Yandex-Cloud-Request-Id:
         | 
| 38 | 
            +
                  - omitted
         | 
| 39 | 
            +
                  Cache-Control:
         | 
| 40 | 
            +
                  - no-cache
         | 
| 41 | 
            +
                  Access-Control-Allow-Origin:
         | 
| 42 | 
            +
                  - "*"
         | 
| 43 | 
            +
                  Access-Control-Allow-Headers:
         | 
| 44 | 
            +
                  - Accept-Language, Accept, X-Uid, X-HTTP-Method, X-Requested-With, Content-Type,
         | 
| 45 | 
            +
                    Authorization
         | 
| 46 | 
            +
                body:
         | 
| 47 | 
            +
                  encoding: UTF-8
         | 
| 48 | 
            +
                  string: ''
         | 
| 49 | 
            +
              recorded_at: Wed, 05 Oct 2022 14:32:20 GMT
         | 
| 50 | 
            +
            recorded_with: VCR 6.1.0
         |