ecologi 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: d9463079cfabd5f778fe294f8e133c7078dcaf25c9fc619ec0c55c791b650518
4
+ data.tar.gz: ded8a55cee96108e54d91d4a0a7ceb91621e1b59624060bee34d06abc4e30fcf
5
+ SHA512:
6
+ metadata.gz: edb7ded5902efc78cb1812fab3a864ed54afc8f01f6e1c72b0ce4cf3f7dadd5bc1eb35fcaffa1ce50ef1087911380b79e5d3e3e7720760494ccd1581cef9f3c6
7
+ data.tar.gz: 8a1739fb807c082a5081a0f3a15c82572a38f445256c65c0da14a0d6ab800131778403702830e162a74c4b40850c9a26d23a272664d74ea32fabe384d673611e
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ API_KEY=
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ecologi.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ecologi (0.1.0)
5
+ faraday (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dotenv (2.7.6)
11
+ faraday (2.2.0)
12
+ faraday-net_http (~> 2.0)
13
+ ruby2_keywords (>= 0.0.4)
14
+ faraday-net_http (2.0.1)
15
+ rake (13.0.6)
16
+ ruby2_keywords (0.0.5)
17
+
18
+ PLATFORMS
19
+ x86_64-linux
20
+
21
+ DEPENDENCIES
22
+ dotenv
23
+ ecologi!
24
+ rake (~> 13.0)
25
+
26
+ BUNDLED WITH
27
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dean Perry
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Ecologi
2
+
3
+ This is a Ruby library for interacting with the Ecologi API.
4
+
5
+ ## Usage
6
+
7
+ To get started, install the gem as normal:
8
+
9
+ ```
10
+ gem "ecologi"
11
+ ```
12
+
13
+ Then you'll need to grab an API key from your [Impact API page](https://ecologi.com/account/api).
14
+
15
+ Once you've got an API key, setup a client like so:
16
+
17
+ ```ruby
18
+ @client = Ecologi::Client.new(api_key: ENV["API_KEY"])
19
+ ```
20
+
21
+ You can then use the following methods.
22
+
23
+ ### Purchasing Trees
24
+
25
+ To purchase trees for an account. Requires a number. The name parameter is optional. Returns a `Ecologi::TreePurchase` response.
26
+
27
+ ```ruby
28
+ @client.trees.purchase number: 2, name: "Tree's for user"
29
+
30
+ #=> #<Ecologi::TreePurchase amount=0.38...
31
+ ```
32
+
33
+ ### Purchasing Carbon Offsets
34
+
35
+ To purchase carbon offsets for an account. Requires number and units. Returns a `Ecologi::CarbonOffsetPurchase` response.
36
+
37
+ Units must be either KG or Tonnes.
38
+
39
+ ```ruby
40
+ @client.carbon_offsets.purchase number: 2, units: "KG"
41
+
42
+ #=> #<Ecologi::CarbonOffsetPurchase number=2...
43
+ ```
44
+
45
+ ### Reporting - Total Impact
46
+
47
+ Returns a `Ecologi::Report` object with the total number of trees and carbon offset for a user.
48
+
49
+ ```ruby
50
+ @client.reporting.total_impact username: "deanpcmad"
51
+
52
+ #=> #<Ecologi::Report trees=1337...
53
+ ```
54
+
55
+ ### Reporting - Total Trees
56
+
57
+ Returns a `Ecologi::Report` object with the total number of trees for a user.
58
+
59
+ ```ruby
60
+ @client.reporting.total_trees username: "deanpcmad"
61
+
62
+ #=> #<Ecologi::Report total=1337...
63
+ ```
64
+
65
+ ### Reporting - Total Carbon Offset
66
+
67
+ Returns a `Ecologi::Report` object with the total amount of carbon offset for a user.
68
+
69
+ ```ruby
70
+ @client.reporting.total_carbon username: "deanpcmad"
71
+
72
+ #=> #<Ecologi::Report total=1337...
73
+ ```
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/ecologi.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/ecologi.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ecologi/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ecologi"
7
+ spec.version = Ecologi::VERSION
8
+ spec.authors = ["Dean Perry"]
9
+ spec.email = ["dean@deanpcmad.com"]
10
+
11
+ spec.summary = "A Ruby library for interacting with the Ecologi API"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://deanpcmad.com"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "faraday", "~> 2.0"
31
+ end
@@ -0,0 +1,39 @@
1
+ module Ecologi
2
+ class Client
3
+ BASE_URL = "https://public.ecologi.com"
4
+
5
+ attr_reader :api_key, :adapter
6
+
7
+ def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
8
+ @api_key = api_key
9
+ @adapter = adapter
10
+
11
+ # Test stubs for requests
12
+ @stubs = stubs
13
+ end
14
+
15
+ def trees
16
+ TreesResource.new(self)
17
+ end
18
+
19
+ def carbon_offsets
20
+ CarbonOffsetsResource.new(self)
21
+ end
22
+
23
+ def reporting
24
+ ReportingResource.new(self)
25
+ end
26
+
27
+ def connection
28
+ @connection ||= Faraday.new(BASE_URL) do |conn|
29
+ conn.request :authorization, :Bearer, api_key
30
+ conn.request :json
31
+
32
+ conn.response :json, content_type: "application/json"
33
+
34
+ conn.adapter adapter, @stubs
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module Ecologi
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 { |key, val| [key, to_ostruct(val)] }.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 Ecologi
2
+ class CarbonOffsetPurchase < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Ecologi
2
+ class Report < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Ecologi
2
+ class TreePurchase < Object
3
+ end
4
+ end
@@ -0,0 +1,42 @@
1
+ module Ecologi
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: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def handle_response(response)
20
+ case response.status
21
+ when 400
22
+ raise Error, "Error 400: Your request was malformed. '#{response.body["responseText"]}'"
23
+ when 401
24
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["message"]}'"
25
+ when 403
26
+ raise Error, "Error 403: You are not allowed to perform that action."
27
+ when 404
28
+ raise Error, "Error 404: No results were found for your request."
29
+ when 409
30
+ raise Error, "Error 409: Your request was a conflict."
31
+ when 429
32
+ raise Error, "Error 429: Your request exceeded the API rate limit."
33
+ when 500
34
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems."
35
+ when 503
36
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second."
37
+ end
38
+
39
+ response
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ module Ecologi
2
+ class CarbonOffsetsResource < Resource
3
+
4
+ def purchase(number:, units:)
5
+ response = post_request("impact/carbon", body: {number: number, units: units})
6
+ CarbonOffsetPurchase.new response.body
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Ecologi
2
+ class ReportingResource < Resource
3
+
4
+ def total_impact(username:)
5
+ response = get_request("users/#{username}/impact")
6
+ Report.new response.body
7
+ end
8
+
9
+ def total_trees(username:)
10
+ response = get_request("users/#{username}/trees")
11
+ Report.new response.body
12
+ end
13
+
14
+ def total_carbon(username:)
15
+ response = get_request("users/#{username}/carbon-offset")
16
+ Report.new response.body
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module Ecologi
2
+ class TreesResource < Resource
3
+
4
+ def purchase(number:, name: nil)
5
+ response = post_request("impact/trees", body: {number: number, name: name})
6
+ TreePurchase.new response.body
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ecologi
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ecologi.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "faraday"
2
+ require "json"
3
+ require_relative "ecologi/version"
4
+
5
+ module Ecologi
6
+ class Error < StandardError; end
7
+
8
+ autoload :Client, "ecologi/client"
9
+ autoload :Resource, "ecologi/resource"
10
+ autoload :Object, "ecologi/object"
11
+
12
+ autoload :TreesResource, "ecologi/resources/trees"
13
+ autoload :CarbonOffsetsResource, "ecologi/resources/carbon_offsets"
14
+ autoload :ReportingResource, "ecologi/resources/reporting"
15
+
16
+ autoload :TreePurchase, "ecologi/objects/tree_purchase"
17
+ autoload :CarbonOffsetPurchase, "ecologi/objects/carbon_offset_purchase"
18
+ autoload :Report, "ecologi/objects/report"
19
+
20
+ end
data/sig/ecologi.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ecologi
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecologi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: A Ruby library for interacting with the Ecologi API
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - ecologi.gemspec
41
+ - lib/ecologi.rb
42
+ - lib/ecologi/client.rb
43
+ - lib/ecologi/object.rb
44
+ - lib/ecologi/objects/carbon_offset_purchase.rb
45
+ - lib/ecologi/objects/report.rb
46
+ - lib/ecologi/objects/tree_purchase.rb
47
+ - lib/ecologi/resource.rb
48
+ - lib/ecologi/resources/carbon_offsets.rb
49
+ - lib/ecologi/resources/reporting.rb
50
+ - lib/ecologi/resources/trees.rb
51
+ - lib/ecologi/version.rb
52
+ - sig/ecologi.rbs
53
+ homepage: https://deanpcmad.com
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://deanpcmad.com
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.6.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.2.22
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: A Ruby library for interacting with the Ecologi API
77
+ test_files: []