open_recycling 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9aeb270870058d43ca07e5f7101fc67d285c6a6f45c0975616daad99bae696c
4
- data.tar.gz: 49d889f06db0eee34e7dac7d692a5e41b7a967bb3f46755a3ed1815becfdb93b
3
+ metadata.gz: a44449639927e8adf871ffd260f6f65cff7e353dde55d96ff7b21da3dfef6b7d
4
+ data.tar.gz: 5bd9bef0572adc15f64129b9fc797cc8224c03af595dc2306c52341e0b8819a4
5
5
  SHA512:
6
- metadata.gz: 25fabde8d4b4c5f478d5538394e85c6a0de503ccb5f6b6ac889b3351898df3454d4088bac1a7d0bdeb1a6e060007fd1fd9179f0a8badb1cfa0ed0917a1c0523e
7
- data.tar.gz: 6471e085191ed2c7b533db0306b6f33d037a0b179f46de2c1bed49335340638e33970e308c5316b2f7db971f7f05ec44e9139b12eb2bb5d2ea3a5a30306fe7b7
6
+ metadata.gz: 21b84535fe05629fcbf50f74183bb43d6824ab870d69a3647aacb8d9a8828eb7c1498ea2355066f7b104a97781cdee81b5469e9f9ec5f5bb99b0c27519792313
7
+ data.tar.gz: 8e3a7674a29d750c2891a225e339f59028a6a8bb256066665d96e1142e28b81ae4f064538db9da03e32f1c527e1913378e1672492425cc3e24e2c255c5f530d7
@@ -0,0 +1,20 @@
1
+ require_relative "applications"
2
+
3
+ module OpenRecycling
4
+ module Apps
5
+ class Client < OpenRecycling::ModuleClient
6
+ def applications
7
+ @applications ||= OpenRecycling::Org::Applications.new(
8
+ base_url: module_url,
9
+ jwt_token: jwt_token
10
+ )
11
+ end
12
+
13
+ private
14
+
15
+ def module_url
16
+ "#{base_url}/apps/v1"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,8 +1,18 @@
1
+ require_relative "apps/client"
1
2
  require_relative "org/client"
2
3
  require_relative "core/client"
4
+ require_relative "documents/client"
3
5
 
4
6
  module OpenRecycling
5
7
  class Client < OpenRecycling::ModuleClient
8
+
9
+ def apps
10
+ @apps ||= OpenRecycling::Apps::Client.new(
11
+ base_url: base_url,
12
+ jwt_token: jwt_token
13
+ )
14
+ end
15
+
6
16
  def org
7
17
  @org ||= OpenRecycling::Org::Client.new(
8
18
  base_url: base_url,
@@ -16,5 +26,12 @@ module OpenRecycling
16
26
  jwt_token: jwt_token
17
27
  )
18
28
  end
29
+
30
+ def documents
31
+ @documents ||= OpenRecycling::Documents::Client.new(
32
+ base_url: base_url,
33
+ jwt_token: jwt_token
34
+ )
35
+ end
19
36
  end
20
37
  end
@@ -0,0 +1,29 @@
1
+ require_relative "uploads"
2
+ require_relative "documents"
3
+
4
+ module OpenRecycling
5
+ module Documents
6
+ class Client < OpenRecycling::ModuleClient
7
+
8
+ def documents
9
+ @documents ||= OpenRecycling::Documents::Documents.new(
10
+ base_url: module_url,
11
+ jwt_token: jwt_token
12
+ )
13
+ end
14
+
15
+ def uploads
16
+ @uploads ||= OpenRecycling::Documents::Uploads.new(
17
+ base_url: module_url,
18
+ jwt_token: jwt_token
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def module_url
25
+ "#{base_url}/documents/v1"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenRecycling
4
+ module Documents
5
+ class Documents < OpenRecycling::Resource
6
+ only :all, :find
7
+
8
+ def initialize(base_url: nil, jwt_token: nil)
9
+ super(
10
+ root_node_singular: "document",
11
+ root_node_plural: "document",
12
+ api_url: "#{base_url}/document",
13
+ jwt_token: jwt_token
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenRecycling
4
+ module Documents
5
+ class Uploads < OpenRecycling::Resource
6
+ only :create, :all
7
+
8
+ def initialize(base_url: nil, jwt_token: nil)
9
+ super(
10
+ root_node_singular: "upload",
11
+ root_node_plural: "uploads",
12
+ api_url: "#{base_url}/uploads",
13
+ jwt_token: jwt_token
14
+ )
15
+ end
16
+
17
+ def create(file_path:, mime_type:, organization_id:)
18
+ uri = URI(api_url)
19
+
20
+ # Prepare the file and boundary
21
+ boundary = "----Upload#{rand(1000000)}"
22
+ file = File.open(file_path, 'rb')
23
+ file_content = file.read
24
+
25
+ # Prepare multipart body
26
+ post_body = []
27
+ post_body << "--#{boundary}\r\n"
28
+ post_body << "Content-Disposition: form-data; name=\"upload[file]\"; filename=\"#{File.basename(file_path)}\"\r\n"
29
+ post_body << "Content-Type: #{mime_type}\r\n\r\n"
30
+ post_body << file_content
31
+ post_body << "\r\n"
32
+
33
+ # Add the `upload[organizationId]` part
34
+ post_body << "--#{boundary}\r\n"
35
+ post_body << "Content-Disposition: form-data; name=\"upload[organizationId]\"\r\n\r\n"
36
+ post_body << organization_id.to_s
37
+ post_body << "\r\n"
38
+
39
+ post_body << "--#{boundary}--\r\n"
40
+
41
+ # Create and send the POST request
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+ http.use_ssl = (uri.scheme == 'https')
44
+
45
+ request = Net::HTTP::Post.new(uri.path)
46
+ request['Content-Type'] = "multipart/form-data; boundary=#{boundary}"
47
+ request["Authorization"] = "Bearer #{jwt_token}"
48
+ request["Accept"] = "application/json"
49
+ request.body = post_body.join
50
+
51
+ # Execute the request and get the response
52
+ response = http.request(request)
53
+
54
+ if response.is_a?(Net::HTTPSuccess)
55
+ parse_resource(response.body)
56
+ else
57
+ handle_error(response)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,4 @@
1
1
  require_relative "organizations"
2
- require_relative "applications"
3
2
 
4
3
  module OpenRecycling
5
4
  module Org
@@ -11,13 +10,6 @@ module OpenRecycling
11
10
  )
12
11
  end
13
12
 
14
- def applications
15
- @applications ||= OpenRecycling::Org::Applications.new(
16
- base_url: module_url,
17
- jwt_token: jwt_token
18
- )
19
- end
20
-
21
13
  private
22
14
 
23
15
  def module_url
@@ -12,6 +12,8 @@ module OpenRecycling
12
12
  end
13
13
 
14
14
  def all(options = {})
15
+ ensure_supported_method(:all)
16
+
15
17
  uri = URI(build_uri_with_params(api_url, options))
16
18
  response = execute_request(Net::HTTP::Get, uri)
17
19
 
@@ -23,6 +25,8 @@ module OpenRecycling
23
25
  end
24
26
 
25
27
  def find(id)
28
+ ensure_supported_method(:find)
29
+
26
30
  uri = URI("#{api_url}/#{id}")
27
31
  response = execute_request(Net::HTTP::Get, uri)
28
32
 
@@ -34,6 +38,8 @@ module OpenRecycling
34
38
  end
35
39
 
36
40
  def create(attributes)
41
+ ensure_supported_method(:create)
42
+
37
43
  uri = URI(api_url)
38
44
  response = execute_request(Net::HTTP::Post, uri, { root_node_singular => attributes }.to_json)
39
45
 
@@ -45,6 +51,8 @@ module OpenRecycling
45
51
  end
46
52
 
47
53
  def update(id, attributes)
54
+ ensure_supported_method(:update)
55
+
48
56
  uri = URI("#{api_url}/#{id}")
49
57
  response = execute_request(Net::HTTP::Patch, uri, { root_node_singular => attributes }.to_json)
50
58
 
@@ -56,6 +64,8 @@ module OpenRecycling
56
64
  end
57
65
 
58
66
  def destroy(id)
67
+ ensure_supported_method(:destroy)
68
+
59
69
  uri = URI("#{api_url}/#{id}")
60
70
  response = execute_request(Net::HTTP::Delete, uri)
61
71
 
@@ -66,6 +76,20 @@ module OpenRecycling
66
76
  end
67
77
  end
68
78
 
79
+ def ensure_supported_method(method)
80
+ return if self.class.supported_methods.include?(method)
81
+
82
+ raise NotImplementedError.new("Method `#{method}` is not supported for this resource")
83
+ end
84
+
85
+ def self.only(*methods)
86
+ @only = methods
87
+ end
88
+
89
+ def self.supported_methods
90
+ @only ||= [:all, :find, :create, :update, :destroy]
91
+ end
92
+
69
93
  private
70
94
 
71
95
  def execute_request(cls, uri, body = nil)
@@ -88,11 +112,16 @@ module OpenRecycling
88
112
  end
89
113
 
90
114
  def parse_resources(body)
91
- parse_body(body)[root_node_plural] || []
115
+ parsed_response = parse_body(body)
116
+ {
117
+ data: parsed_response[root_node_plural.to_sym] || [],
118
+ }
92
119
  end
93
120
 
94
121
  def parse_resource(body)
95
- parse_body(body)[root_node_singular]
122
+ {
123
+ data: parse_body(body)[root_node_singular.to_sym],
124
+ }
96
125
  end
97
126
 
98
127
  def parse_body(body)
@@ -102,7 +131,7 @@ module OpenRecycling
102
131
  def underscored(hash_or_array)
103
132
  if hash_or_array.is_a?(Hash)
104
133
  hash_or_array.reduce({}) do |memo, (k, v)|
105
- memo.merge(underscore(k) => underscored(v))
134
+ memo.merge(underscore(k).to_sym => underscored(v))
106
135
  end
107
136
  elsif hash_or_array.is_a?(Array)
108
137
  hash_or_array.map { |hash| underscored(hash) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenRecycling
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_recycling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Open Recycling
@@ -43,12 +43,16 @@ files:
43
43
  - README.md
44
44
  - Rakefile
45
45
  - lib/open_recycling.rb
46
+ - lib/open_recycling/apps/applications.rb
47
+ - lib/open_recycling/apps/client.rb
46
48
  - lib/open_recycling/client.rb
47
49
  - lib/open_recycling/client_error.rb
48
50
  - lib/open_recycling/core/client.rb
49
51
  - lib/open_recycling/core/invoices.rb
52
+ - lib/open_recycling/documents/client.rb
53
+ - lib/open_recycling/documents/documents.rb
54
+ - lib/open_recycling/documents/uploads.rb
50
55
  - lib/open_recycling/module_client.rb
51
- - lib/open_recycling/org/applications.rb
52
56
  - lib/open_recycling/org/client.rb
53
57
  - lib/open_recycling/org/organizations.rb
54
58
  - lib/open_recycling/resource.rb
File without changes