deta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35687498010f2d7c0724fcf993a800269d6c2f61aa4f4716371018f46774d373
4
+ data.tar.gz: e9b04aadc3bbc443df406a61c070188de2029d62b0cb191713785043ddf9e9f0
5
+ SHA512:
6
+ metadata.gz: 708c43716d995f5a5edc8399e39a68a16ff4b738442c25ffd1ddb370dd00013027c0bb78dd3ef849761700ae4a5fa6edae799faf99d94f62287e52e390df3aa1
7
+ data.tar.gz: 3a6a9f168d1d3c0758adeea5354128ce369932e0b0aa716c7350edcaae1dc26a4c1bf1e556e826f3dda7921271410a6de3d2154b9b2813eebe1c7661bc90d45b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-01-28
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at arubinofaux@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in deta.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ deta (0.1.0)
5
+ faraday (~> 2.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ faraday (2.1.0)
11
+ faraday-net_http (~> 2.0)
12
+ ruby2_keywords (>= 0.0.4)
13
+ faraday-net_http (2.0.1)
14
+ rake (13.0.6)
15
+ ruby2_keywords (0.0.5)
16
+
17
+ PLATFORMS
18
+ x86_64-linux
19
+
20
+ DEPENDENCIES
21
+ deta!
22
+ rake (~> 13.0)
23
+
24
+ BUNDLED WITH
25
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dainel Vera
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,101 @@
1
+ # Deta-Ruby
2
+
3
+ deta-ruby is an unofficial Deta Gem for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'deta'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install deta
20
+
21
+ ## Usage
22
+
23
+ To access the API, you'll need to create a `Deta::Client` and pass in your project key. You can find your project key at [https://web.deta.sh](https://web.deta.sh)
24
+
25
+ ```ruby
26
+ client = Deta::Client.new(project_key: ENV["DETA_PROJECT_KEY"])
27
+ ```
28
+
29
+ The client then gives you access to each of the resources.
30
+
31
+ Responses are created as objects like `Deta::BaseObject`. Having types like `Deta::DriveObject` is handy for understanding what type of object you're working with. They're built using OpenStruct so you can easily access data in a Ruby-ish way.
32
+
33
+ ## Base
34
+
35
+ ```ruby
36
+ detabase = client.base("base_name")
37
+ ```
38
+
39
+ ### Put Items
40
+
41
+ ```ruby
42
+ # single item
43
+ put = detabase.put({name: "Mike Jones", age: 31, hometown: "Denver"})
44
+ put.processed.items.first.key
45
+
46
+ # multi items
47
+ puts = detabase.put({name: "Mike Jones", age: 31, hometown: "Denver"}, {name: "Mike Dow", age: 25, hometown: "Miami"})
48
+ puts.processed
49
+ ```
50
+
51
+ ### Get Item
52
+
53
+ ```ruby
54
+ detabase.get(key)
55
+ ```
56
+
57
+ ### Delete Item
58
+
59
+ ```ruby
60
+ detabase.delete(key)
61
+ ```
62
+
63
+ ### Insert Item
64
+
65
+ ```ruby
66
+ TODO:
67
+ ```
68
+
69
+ ### Update Item
70
+
71
+ ```ruby
72
+ detabase.update(key, set: {hometown: "Miami"})
73
+ ```
74
+
75
+ ### Query Items
76
+
77
+ ```ruby
78
+ TODO:
79
+ ```
80
+
81
+ ## Drive
82
+
83
+ `TODO:`
84
+
85
+ ## Development
86
+
87
+ 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.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arubinofaux/deta-ruby. 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/arubinofaux/deta-ruby/blob/main/CODE_OF_CONDUCT.md).
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
98
+
99
+ ## Code of Conduct
100
+
101
+ Everyone interacting in the Deta project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/arubinofaux/deta-ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "deta"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/deta.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/deta/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "deta"
7
+ spec.version = Deta::VERSION
8
+ spec.authors = ["Dainel Vera"]
9
+ spec.email = ["arubinofaux@gmail.com"]
10
+
11
+ spec.summary = "Ruby bindings for deta.sh"
12
+ spec.description = "Ruby bindings for deta.sh. deta APIs can be found here: https://docs.deta.sh/docs/home"
13
+ spec.homepage = "https://github.com/arubinofaux/deta-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = "https://github.com/arubinofaux/deta-ruby/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ spec.add_dependency "faraday", "~> 2.1.0"
40
+ end
@@ -0,0 +1,27 @@
1
+ module Deta
2
+ class Client
3
+ BASE_URL = "https://database.deta.sh/v1/"
4
+
5
+ attr_reader :project_id, :project_key, :adapter
6
+
7
+ def initialize(project_key:, adapter: Faraday.default_adapter)
8
+ @project_id = project_key.split("_").first
9
+ @project_key = project_key
10
+ @adapter = adapter
11
+ end
12
+
13
+ def base(base_db = nil)
14
+ BaseResource.new(self, base_db)
15
+ end
16
+
17
+ def connection
18
+ @connection ||= Faraday.new do |conn|
19
+ conn.url_prefix = BASE_URL
20
+ conn.headers['X-API-Key'] = project_key
21
+ conn.request :json
22
+ conn.response :json, content_type: "application/json"
23
+ conn.adapter adapter
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/deta/error.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Deta
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module Deta
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
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module Deta
2
+ class BaseObject < Object
3
+ end
4
+ end
@@ -0,0 +1,47 @@
1
+ module Deta
2
+ class Resource
3
+ attr_reader :client, :base_db
4
+
5
+ def initialize(client, base_db)
6
+ @client = client
7
+ @base_db = base_db
8
+ end
9
+
10
+ private
11
+
12
+ def put_request(url, body:, headers: {})
13
+ handle_response client.connection.put(url, body, headers)
14
+ end
15
+
16
+ def get_request(url, params: {}, headers: {})
17
+ handle_response client.connection.get(url, params, headers)
18
+ end
19
+
20
+ def patch_request(url, body:, headers: {})
21
+ handle_response client.connection.patch(url, body, headers)
22
+ end
23
+
24
+ def delete_request(url, params: {}, headers: {})
25
+ handle_response client.connection.delete(url, params, headers)
26
+ end
27
+
28
+ def handle_response(response)
29
+ case response.status
30
+ when 400
31
+ raise Error, "Your request was malformed. #{response.body["errors"]}"
32
+ when 401
33
+ raise Error, "You did not supply valid authentication credentials. #{response.body["errors"]}"
34
+ when 403
35
+ raise Error, "You are not allowed to perform that action. #{response.body["errors"]}"
36
+ when 404
37
+ raise Error, "No results were found for your request. #{response.body["errors"]}"
38
+ when 429
39
+ raise Error, "Your request exceeded the API rate limit. #{response.body["errors"]}"
40
+ when 500
41
+ raise Error, "We were unable to perform the request due to server-side problems. #{response.body["errors"]}"
42
+ end
43
+
44
+ response
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ module Deta
2
+ class BaseResource < Resource
3
+ def put(*attributes)
4
+ items = {items: attributes}
5
+
6
+ BaseObject.new put_request("#{client.project_id}/#{base_db}/items", body: items).body
7
+ end
8
+
9
+ def update(key = nil, set: nil, increment: nil, append: nil, prepend: nil, delete: nil)
10
+ request = {}
11
+ request[:set] = set if set
12
+ request[:increment] = increment if increment
13
+ request[:append] = append if append
14
+ request[:prepend] = prepend if prepend
15
+ request[:delete] = delete if delete
16
+
17
+ BaseObject.new patch_request("#{client.project_id}/#{base_db}/items/#{key}", body: request).body
18
+ end
19
+
20
+ def get(key = nil)
21
+ BaseObject.new get_request("#{client.project_id}/#{base_db}/items/#{key}").body
22
+ end
23
+
24
+ def delete(key = nil)
25
+ BaseObject.new delete_request("#{client.project_id}/#{base_db}/items/#{key}").body
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Deta
4
+ VERSION = "0.1.0"
5
+ end
data/lib/deta.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+
5
+ require_relative "deta/version"
6
+
7
+ module Deta
8
+ autoload :Client, "deta/client"
9
+ autoload :Error, "deta/error"
10
+ autoload :Resource, "deta/resource"
11
+ autoload :Object, "deta/object"
12
+
13
+ autoload :BaseResource, "deta/resources/base"
14
+
15
+ autoload :BaseObject, "deta/objects/base"
16
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dainel Vera
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-29 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.1.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.1.0
27
+ description: 'Ruby bindings for deta.sh. deta APIs can be found here: https://docs.deta.sh/docs/home'
28
+ email:
29
+ - arubinofaux@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - CODE_OF_CONDUCT.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - deta.gemspec
44
+ - lib/deta.rb
45
+ - lib/deta/client.rb
46
+ - lib/deta/error.rb
47
+ - lib/deta/object.rb
48
+ - lib/deta/objects/base.rb
49
+ - lib/deta/resource.rb
50
+ - lib/deta/resources/base.rb
51
+ - lib/deta/version.rb
52
+ homepage: https://github.com/arubinofaux/deta-ruby
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/arubinofaux/deta-ruby
57
+ source_code_uri: https://github.com/arubinofaux/deta-ruby
58
+ changelog_uri: https://github.com/arubinofaux/deta-ruby/blob/main/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.3.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Ruby bindings for deta.sh
78
+ test_files: []