-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement base classes for yandex disk client
- Loading branch information
Showing
18 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ PLATFORMS | |
DEPENDENCIES | ||
coveralls_reborn | ||
rspec (~> 3.9, < 4.0) | ||
simplecov | ||
yadisk! | ||
|
||
BUNDLED WITH | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
end | ||
|
||
require 'yadisk/version' | ||
require 'yadisk/client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'yadisk/client/configuration' | ||
require 'yadisk/client/resources' | ||
require 'yadisk/client/request' | ||
require 'yadisk/client/response' | ||
|
||
module Yadisk | ||
class Client | ||
attr_reader :config | ||
|
||
def initialize(config = nil) | ||
@config = config || self.class.configuration | ||
@v1 = Resources::V1.new(self) | ||
end | ||
|
||
class << self | ||
def configuration | ||
@_configuration ||= Configuration.new | ||
end | ||
|
||
def configure | ||
yield(configuration) | ||
end | ||
|
||
def default | ||
new(configuration) | ||
end | ||
end | ||
|
||
def v1 = @v1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
class Client | ||
class Configuration | ||
attr_accessor :oauth_token | ||
|
||
def initialize(oauth_token: nil) | ||
@oauth_token = oauth_token | ||
end | ||
|
||
def self.build(&block) | ||
config = self.new | ||
block.call(config) | ||
config | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Yadisk | ||
class Client | ||
class Request | ||
# TODO | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
class Client | ||
module Resources | ||
end | ||
end | ||
end | ||
|
||
require 'yadisk/client/resources/resource' | ||
require 'yadisk/client/resources/v1' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
class Client | ||
module Resources | ||
class Resource | ||
attr_reader :client, :parent_resource | ||
|
||
def initialize(client, parent_resource = nil) | ||
@client = client | ||
@parent_resource = parent_resource | ||
end | ||
|
||
def path = nil | ||
|
||
def paths = parent_resource.paths + [path] | ||
|
||
def to_url = "#{parent_resource.to_url}/#{path}" | ||
|
||
private | ||
|
||
def default_headers | ||
{ | ||
"Content-Type" => "application/json", | ||
"User-Agent" => user_agent | ||
} | ||
end | ||
|
||
def user_agent | ||
"Yadisk/#{Yadisk::VERSION}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
class Client | ||
module Resources | ||
class V1 < Resource | ||
BASE_URL = 'https://cloud-api.yandex.net' | ||
|
||
def initialize(*args) | ||
super | ||
@disk = Disk.new(client, self) | ||
end | ||
|
||
def base_url = BASE_URL | ||
|
||
def path = 'v1' | ||
|
||
def paths = [path] | ||
|
||
def to_url = "#{base_url}/#{path}" | ||
|
||
def disk = @disk | ||
end | ||
end | ||
end | ||
end | ||
|
||
require 'yadisk/client/resources/v1/disk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
class Client | ||
module Resources | ||
class V1 | ||
class Disk < Resource | ||
def path = 'disk' | ||
|
||
def call | ||
# Create Request | ||
url = to_url | ||
headers = default_headers | ||
puts url | ||
puts headers | ||
puts client.config.oauth_token | ||
# headers = to_headers | ||
# Request.new(method: :get, url: url, headers: headers, query: query, body: body) | ||
Response.new # TODO | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Yadisk | ||
class Client | ||
class Response | ||
# TODO | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yadisk | ||
VERSION = '1.0.0.dev' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
RSpec.describe Yadisk::Client::Configuration do | ||
let(:oauth_token) { 'token123' } | ||
subject { described_class.new } | ||
|
||
it "has empty oauth_token" do | ||
expect(subject.oauth_token).to be_nil | ||
end | ||
|
||
context "initialize with .new" do | ||
subject { described_class.new(oauth_token: oauth_token) } | ||
|
||
it "has oauth_token" do | ||
expect(subject.oauth_token).to eq(oauth_token) | ||
end | ||
end | ||
|
||
context "initialize with .build method" do | ||
subject do | ||
described_class.build do |c| | ||
c.oauth_token = oauth_token | ||
end | ||
end | ||
|
||
it "has oauth_token" do | ||
expect(subject.oauth_token).to eq(oauth_token) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
RSpec.describe Yadisk::Client::Resources::V1::Disk do | ||
subject { described_class.new(client, parent) } | ||
let(:parent) { Yadisk::Client::Resources::V1.new(client) } | ||
let(:client) { Yadisk::Client.default } | ||
before do | ||
Yadisk::Client.configure do |c| | ||
c.oauth_token = 'token123' | ||
end | ||
end | ||
|
||
after do | ||
Yadisk::Client.instance_eval do | ||
@_configuration = nil | ||
end | ||
end | ||
|
||
it "#path" do | ||
expect(subject.path).to eq('disk') | ||
end | ||
|
||
it "#paths" do | ||
expect(subject.paths).to match_array(['v1', 'disk']) | ||
end | ||
|
||
it "#to_url" do | ||
expect(subject.to_url).to eq('https://cloud-api.yandex.net/v1/disk') | ||
end | ||
|
||
it "#call" do | ||
expect(subject.call).to be_a(Yadisk::Client::Response) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
RSpec.describe Yadisk::Client::Resources::V1 do | ||
subject { described_class.new(client) } | ||
let(:client) { Yadisk::Client.default } | ||
before do | ||
Yadisk::Client.configure do |c| | ||
c.oauth_token = 'token123' | ||
end | ||
end | ||
|
||
after do | ||
Yadisk::Client.instance_eval do | ||
@_configuration = nil | ||
end | ||
end | ||
|
||
it "#base_url" do | ||
expect(subject.base_url).to eq(Yadisk::Client::Resources::V1::BASE_URL) | ||
end | ||
|
||
it "#path" do | ||
expect(subject.path).to eq('v1') | ||
end | ||
|
||
it "#paths" do | ||
expect(subject.paths).to match_array(['v1']) | ||
end | ||
|
||
it "#to_url" do | ||
expect(subject.to_url).to eq('https://cloud-api.yandex.net/v1') | ||
end | ||
|
||
it "#disk" do | ||
expect(subject.disk).to be_a(Yadisk::Client::Resources::V1::Disk) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
RSpec.describe Yadisk::Client do | ||
let(:default_client) { described_class.default } | ||
|
||
it "has configure method" do | ||
expect(described_class).to respond_to(:configure) | ||
end | ||
|
||
it "not empty config" do | ||
expect(default_client.config).not_to be_nil | ||
end | ||
|
||
it "has empty oauth_token" do | ||
expect(default_client.config.oauth_token).to be_nil | ||
end | ||
|
||
describe 'with default config' do | ||
let(:oauth_token) { 'y0_AgAAAAA' } | ||
before do | ||
described_class.configure do |c| | ||
c.oauth_token = oauth_token | ||
end | ||
end | ||
after do | ||
Yadisk::Client.instance_eval do | ||
@_configuration = nil | ||
end | ||
end | ||
|
||
it "default client has correct oauth_token" do | ||
expect(default_client.config.oauth_token).to eq(oauth_token) | ||
end | ||
end | ||
|
||
describe "with custom config" do | ||
let(:config1) { Yadisk::Client::Configuration.new(oauth_token: 'oauth_token1') } | ||
let(:config2) { Yadisk::Client::Configuration.new(oauth_token: 'oauth_token2') } | ||
|
||
let(:client1) { described_class.new(config1) } | ||
let(:client2) { described_class.new(config2) } | ||
|
||
it "client1 and client2 has different oauth tokens" do | ||
expect(client1.config.oauth_token).not_to eq(client2.config.oauth_token) | ||
end | ||
end | ||
|
||
it "has v1 method" do | ||
expect(default_client).to respond_to(:v1) | ||
expect(default_client.v1).to be_a(Yadisk::Client::Resources::Resource) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters