open_recycling 0.0.1 → 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: 4353807923307e777bfd3b7f64f2bf76eb47397a7d70b9c32f5ae43e19a345e6
4
- data.tar.gz: ca572c16f265cce9eb9f365f4d936b1209fd159ac25ea826143b87852382169f
3
+ metadata.gz: a44449639927e8adf871ffd260f6f65cff7e353dde55d96ff7b21da3dfef6b7d
4
+ data.tar.gz: 5bd9bef0572adc15f64129b9fc797cc8224c03af595dc2306c52341e0b8819a4
5
5
  SHA512:
6
- metadata.gz: 2bbff8d0802a87243494be7f36047f230adc485d8e6a034c75830fc3add85c26e911af30d6994871fa8d4331fc22c616a30342c23094c30e306434d1f8579ff6
7
- data.tar.gz: 0f49efc3c727ac4217ec3dd8ddee1cc0b4494a4f55897c8983990f1aad32c806ee49643916c4746f008a6a4160a979a0d19eae2dfdb220d8eb31ebf9d355951c
6
+ metadata.gz: 21b84535fe05629fcbf50f74183bb43d6824ab870d69a3647aacb8d9a8828eb7c1498ea2355066f7b104a97781cdee81b5469e9f9ec5f5bb99b0c27519792313
7
+ data.tar.gz: 8e3a7674a29d750c2891a225e339f59028a6a8bb256066665d96e1142e28b81ae4f064538db9da03e32f1c527e1913378e1672492425cc3e24e2c255c5f530d7
data/README.md CHANGED
@@ -1,31 +1,33 @@
1
- # OpenRecycling
1
+ # Open Recycling API Client
2
2
 
3
- API client for OpenRecycling API
3
+ Ruby client for the Open Recycling API. You can use this gem to develop applications that interact with the Open Recycling Platform.
4
4
 
5
5
  ## Installation
6
6
 
7
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
-
9
7
  Install the gem and add to the application's Gemfile by executing:
10
8
 
11
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
9
+ $ bundle add open_recycling
10
+
11
+ Or add it manually to the Gemfile:
12
+
13
+ ```ruby
14
+ gem 'open_recycling'
15
+ ```
12
16
 
13
17
  If bundler is not being used to manage dependencies, install the gem by executing:
14
18
 
15
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ $ gem install open_recycling
16
20
 
17
21
  ## Usage
18
22
 
19
- Register a new application on OpenRecycling and get the application id, api key and secret key.
20
-
21
- Furthermore, you need to have an application user id as the application always acts on behalf of a user.
23
+ Register a new application on the Open Recycling Platform and get the application id, api key and secret key. Furthermore, you need to have an application user id as the application always acts on behalf of a user.
22
24
 
23
25
  With the above information, you can generate a JWT token using the `OpenRecycling.generate_token` method.
24
26
 
25
27
  ```
26
28
  require 'open_recycling'
27
29
 
28
- OpenRecycling.api_url = "<>"
30
+ OpenRecycling.api_url = "<open recycling url>"
29
31
  OpenRecycling.jwt_token = OpenRecycling.generate_token(
30
32
  application_id: "<application id>",
31
33
  api_key: "<api key>",
@@ -40,26 +42,16 @@ You can now create a new client instance.
40
42
  client = OpenRecycling::Client.new
41
43
  ```
42
44
 
43
- With the client setup, you can now make requests to the OpenRecycling API.
45
+ With the client setup, you can now make requests to the OpenRecycling API, e.g. to fetch all organizations.
44
46
 
45
47
  ```ruby
46
48
  organizations = client.org.organizations.all
47
49
  ```
48
50
 
49
- ## Development
50
-
51
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
-
53
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
-
55
- ## Contributing
56
-
57
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/open_recycling. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/open_recycling/blob/main/CODE_OF_CONDUCT.md).
58
-
59
51
  ## License
60
52
 
61
53
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
54
 
63
55
  ## Code of Conduct
64
56
 
65
- Everyone interacting in the OpenRecycling project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/open_recycling/blob/main/CODE_OF_CONDUCT.md).
57
+ Everyone interacting in the Open Recycling project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/open_recycling/blob/main/CODE_OF_CONDUCT.md).
@@ -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.1"
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.1
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