seaweedfs 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6240ff621132a5819416d4e730e662f9c09cfce0a7d8dead1c447d0d5ea383d
4
+ data.tar.gz: b4977e319565339ade1067b34f4bbaa77a856c04cbcf604651c518b861f96b0b
5
+ SHA512:
6
+ metadata.gz: f05f90030145b996e912d6a2e4a74757e83e686a5a615cdfc7f7f208df284f0cc374a0aa39ae9cab51b4af613509a80ecd4ff100e4068917e628f5b31cba9558
7
+ data.tar.gz: 05e8115a6a95323710b2040ee8680f95a33788d018dadd0247b8f0dc433bc6e6996ffd7089ace8d724dfdb2e5514ea9448bc71a2733b52da99fb54ce2a16d00f
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 John Guest
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ ## Seaweedfs
2
+
3
+ a [seaweedfs](https://github.com/chrislusf/seaweedfs) ruby client
4
+
5
+ ### Getting started
6
+
7
+ Run `gem install seaweedfs` or include it in your project's Gemfile.
8
+
9
+ ```ruby
10
+ require 'seaweedfs'
11
+ client = Seaweedfs::Client.new(endpoint: "http://localhost:9333")
12
+ ```
13
+
14
+ ### Operations
15
+
16
+ ```ruby
17
+ # upload a file
18
+ dir = client.file.dir_assign
19
+ file = client.file.upload('file_path', dir.fid, dir.publicUrl)
20
+
21
+ # file methods
22
+ dir.fid # => "1,01766888e0"
23
+ client.file.url(dir.fid, dir.publicUrl) # => "http://localhost:8080/1,01766888e0"
24
+ client.file.read(dir.fid, dir.publicUrl) # => "hello world!"
25
+ file.name # => "test.txt"
26
+
27
+
28
+ # delete file
29
+ client.file.delete(dir.fid, dir.publicUrl)
30
+ ```
@@ -0,0 +1,45 @@
1
+ module Seaweedfs
2
+ class Client
3
+
4
+ attr_reader :endpoint, :adapter
5
+
6
+ def initialize(endpoint:, adapter: Faraday.default_adapter)
7
+ @endpoint = normalize_endpoint(endpoint)
8
+ @adapter = adapter
9
+ end
10
+
11
+ def connection(other_endpoint=nil)
12
+ @connection ||= Faraday.new(other_endpoint || endpoint) do |conn|
13
+ conn.request :json
14
+
15
+ # conn.response :dates
16
+ conn.response :json, content_type: "application/json"
17
+
18
+ conn.adapter adapter
19
+ end
20
+ end
21
+
22
+ def file
23
+ FileResource.new(self)
24
+ end
25
+
26
+ def status
27
+ Object.new connection.get("cluster/status?pretty=y").body
28
+ end
29
+
30
+ private
31
+
32
+ def normalize_endpoint(endpoint)
33
+ endpoint = "http://#{endpoint}" unless endpoint.start_with?('http')
34
+ uri = URI.parse(endpoint)
35
+ uri = URI.parse("http://#{endpoint}") unless uri.scheme
36
+
37
+ if uri.scheme != 'http' && uri.scheme != 'https'
38
+ fail Error, "Only HTTP and HTTPS endpoint are accepted."
39
+ end
40
+
41
+ uri
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Seaweedfs
2
+ class Error < StandardError
3
+
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'ostruct'
2
+
3
+ module Seaweedfs
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map{|k,v| [k, to_ostruct(v)]}.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map{|o| to_ostruct(o)}
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module Seaweedfs
2
+ class File < Object
3
+ end
4
+ end
@@ -0,0 +1,53 @@
1
+ module Seaweedfs
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {}, other_endpoint: nil)
12
+ handle_response client.connection(other_endpoint).get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body: {}, headers: {}, other_endpoint: nil)
16
+ handle_response client.connection(other_endpoint).post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body: {}, headers: {}, other_endpoint: nil)
20
+ handle_response client.connection(other_endpoint).patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body: {}, headers: {}, other_endpoint: nil)
24
+ handle_response client.connection(other_endpoint).put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {}, other_endpoint: nil)
28
+ handle_response client.connection(other_endpoint).delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Your request was malformed. #{response.body["error"]}"
35
+ when 401
36
+ raise Error, "You did not supply valid authentication credentials. #{response.body["error"]}"
37
+ when 403
38
+ raise Error, "You are not allowed to perform that action. #{response.body["error"]}"
39
+ when 404
40
+ raise Error, "No results were found for your request. #{response.body["error"]}"
41
+ when 429
42
+ raise Error, "Your request exceeded the API rate limit. #{response.body["error"]}"
43
+ when 500
44
+ raise Error, "We were unable to perform the request due to server-side problems. #{response.body["error"]}"
45
+ when 503
46
+ raise Error, "You have been rate limited for sending more than 20 requests per second. #{response.body["error"]}"
47
+ end
48
+
49
+ response
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ module Seaweedfs
2
+ class FileResource < Resource
3
+
4
+ def dir_assign
5
+ Object.new post_request('dir/assign', body: {}).body
6
+ end
7
+
8
+ def upload(file_path, fid, public_url)
9
+ public_url = enforce_uri_schema(public_url)
10
+ url = URI.join(public_url, fid).to_s
11
+
12
+ Faraday::Request.register_middleware(multipart: Faraday::Multipart::Middleware)
13
+
14
+ conn = Faraday.new(url) do |f|
15
+ f.request :multipart
16
+ end
17
+
18
+ payload = { file: Faraday::FilePart.new(file_path, ::MimeMagic.by_path(file_path), ::File.basename(file_path)) }
19
+ response = conn.post('', payload)
20
+
21
+ File.new JSON.parse(response.body) rescue nil
22
+ end
23
+
24
+ def delete(fid, public_url)
25
+ public_url = enforce_uri_schema(public_url)
26
+ url = URI.join(public_url, fid).to_s
27
+ response = Faraday.delete(url)
28
+
29
+ Object.new JSON.parse(response.body)
30
+ end
31
+
32
+ def url(fid, public_url)
33
+ url = "#{public_url}/#{fid}"
34
+ url = "http://#{url}" unless url.start_with?('http')
35
+ url
36
+ end
37
+
38
+ def read(fid, public_url)
39
+ url = url(fid, public_url)
40
+ response = Faraday.get(url)
41
+
42
+ response.body
43
+ end
44
+
45
+ private
46
+
47
+ def enforce_uri_schema(url)
48
+ return url if url.start_with?('http://') || url.start_with?('https://')
49
+ "http://#{url}"
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Seaweedfs
2
+ VERSION = "0.1.0"
3
+ end
data/lib/seaweedfs.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'faraday'
2
+ require 'faraday/multipart'
3
+ require 'seaweedfs/version'
4
+ require 'mimemagic'
5
+
6
+ module Seaweedfs
7
+ autoload :Client, 'seaweedfs/client'
8
+ autoload :Error, 'seaweedfs/error'
9
+ autoload :Resource, 'seaweedfs/resource'
10
+ autoload :Object, 'seaweedfs/object'
11
+
12
+ # api
13
+ autoload :File, 'seaweedfs/resources/file'
14
+ # obj
15
+ autoload :FileResource, 'seaweedfs/objects/file'
16
+
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seaweedfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Drew Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday-multipart
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mimemagic
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.3
83
+ description: A Seaweed-FS Ruby Client
84
+ email: skipmaple@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - LICENSE.md
90
+ - README.md
91
+ - lib/seaweedfs.rb
92
+ - lib/seaweedfs/client.rb
93
+ - lib/seaweedfs/error.rb
94
+ - lib/seaweedfs/object.rb
95
+ - lib/seaweedfs/objects/file.rb
96
+ - lib/seaweedfs/resource.rb
97
+ - lib/seaweedfs/resources/file.rb
98
+ - lib/seaweedfs/version.rb
99
+ homepage: https://rubygems.org/gems/seaweedfs
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 2.0.0
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.4.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: seaweedfs
122
+ test_files: []