postgrest 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: e8867bc92b600721834d706875e5d96805c88f3c7314088c81547e907da741d7
4
+ data.tar.gz: c215c962a1d3f530da16bc8d16e34089438f5ee2acef7f551aa76870c60ad6dd
5
+ SHA512:
6
+ metadata.gz: 49e2357915dd71ce84a784749e98c7d6d0c40f323bc2dafc4b4dc8335518b5e6e3a3af44ba87bf3e881b2f0fd5e498246eef19798999ceb9df9b995990467935
7
+ data.tar.gz: 9d169f87f00c70c50a2a058a099c3eed8e5d0f18e185a08df13bea077dca4a9147d64b581977294415419962e33e8739f4c5273cc1673929175fc60d09d6ce7e
data/.codeclimate.yml ADDED
@@ -0,0 +1,9 @@
1
+ version: "2"
2
+ plugins:
3
+ rubocop:
4
+ enabled: true
5
+ channel: rubocop-0-60
6
+ config:
7
+ file: ".rubocop.yml"
8
+ exclude_patterns:
9
+ - "spec/"
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ bin/private_console
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ SuggestExtensions: false
3
+ Exclude:
4
+ - 'spec/**/*.rb'
5
+ - 'bin/**/*'
6
+ Style/Documentation:
7
+ Enabled: false
8
+ Style/HashEachMethods:
9
+ Enabled: false
10
+ Lint/MissingSuper:
11
+ Enabled: false
12
+ Lint/UriEscapeUnescape:
13
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
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 postgrest.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ postgrest (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.4.4)
12
+ docile (1.3.5)
13
+ method_source (1.0.0)
14
+ parallel (1.20.1)
15
+ parser (3.0.1.0)
16
+ ast (~> 2.4.1)
17
+ pry (0.14.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
20
+ rainbow (3.0.0)
21
+ rake (12.3.3)
22
+ regexp_parser (2.1.1)
23
+ rexml (3.2.5)
24
+ rspec (3.10.0)
25
+ rspec-core (~> 3.10.0)
26
+ rspec-expectations (~> 3.10.0)
27
+ rspec-mocks (~> 3.10.0)
28
+ rspec-core (3.10.1)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-expectations (3.10.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-mocks (3.10.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-support (3.10.2)
37
+ rubocop (1.13.0)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.0.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.2.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 3.0)
46
+ rubocop-ast (1.4.1)
47
+ parser (>= 2.7.1.5)
48
+ ruby-progressbar (1.11.0)
49
+ simplecov (0.21.2)
50
+ docile (~> 1.1)
51
+ simplecov-html (~> 0.11)
52
+ simplecov_json_formatter (~> 0.1)
53
+ simplecov-html (0.12.3)
54
+ simplecov_json_formatter (0.1.2)
55
+ unicode-display_width (2.0.0)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ postgrest!
62
+ pry
63
+ rake (~> 12.0)
64
+ rspec (~> 3.5)
65
+ rubocop (~> 1.13.0)
66
+ simplecov (~> 0.21.2)
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Marcelo Barreto
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,103 @@
1
+ # PostgREST
2
+
3
+ ## Status
4
+
5
+ [![Build Status](https://api.travis-ci.org/marcelobarreto/postgrest.svg?branch=master)](https://travis-ci.org/marcelobarreto/postgrest-rb)
6
+ [![Code Climate](https://codeclimate.com/github/marcelobarreto/postgrest-rb.svg)](https://codeclimate.com/github/marcelobareto/postgrest-rb)
7
+ [![Code Climate](https://codeclimate.com/github/marcelobarreto/postgrest-rb/coverage.svg)](https://codeclimate.com/github/marcelobarreto/postgrest-rb)
8
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
9
+ [![RubyGems](http://img.shields.io/gem/dt/postgrest-rb.svg?style=flat)](http://rubygems.org/gems/postgrest-rb)
10
+
11
+ Ruby client for [PostgREST](https://postgrest.org/)
12
+
13
+ This gem is under development, any help are welcome :muscle:
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'postgrest'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ `$ bundle install`
26
+
27
+ Or install it yourself as:
28
+
29
+ `$ gem install postgrest`
30
+
31
+ ## Usage
32
+
33
+ ### Configuration
34
+
35
+ ```ruby
36
+ db = Postgrest::Client.new(url: url, headers: headers, schema: schema)
37
+ ```
38
+
39
+ ### Selecting
40
+
41
+ ```ruby
42
+ # Basic select
43
+
44
+ db.from('todos').select('*')
45
+
46
+ <Postgrest::Response:0x0000556745caa420
47
+ @body={:select=>"*"},
48
+ @count=1,
49
+ @data=[{"id"=>1, "title"=>"Go to the gym", "completed"=>false}],
50
+ @error=true,
51
+ @status=200,
52
+ @status_text="OK">
53
+
54
+ # Selecting just one or more fields
55
+
56
+ db.from('todos').select('title, completed')
57
+
58
+ <Postgrest::Response:0x0000556745590770
59
+ @body={:select=>"title, completed"},
60
+ @count=1,
61
+ @data=[{"title"=>"Go to the gym", "completed"=>false}],
62
+ @error=true,
63
+ @status=200,
64
+ @status_text="OK">
65
+
66
+ ```
67
+
68
+ ### Inserting
69
+
70
+ ```ruby
71
+ db.from('todos').insert({ title: 'Go to the gym', completed: false })
72
+
73
+ <Postgrest::Response:0x00005567458aebf0 @body={:title=>"Go to the gym", :completed=>false}, @count=nil, @data="", @error=false, @status=201, @status_text="Created">
74
+
75
+ # Inserting multiple rows at once
76
+ db.from('todos').insert([
77
+ { title: 'Go to the gym', completed: false },
78
+ { title: 'Walk in the park', completed: true },
79
+ ])
80
+
81
+ <Postgrest::Response:0x0000556745cdd118
82
+ @body=[{:title=>"Go to the gym", :completed=>false}, {:title=>"Walk in the park", :completed=>true}],
83
+ @count=nil,
84
+ @data="",
85
+ @error=true,
86
+ @status=201,
87
+ @status_text="Created">
88
+
89
+ ```
90
+
91
+ ## Development
92
+
93
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
94
+
95
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
96
+
97
+ ## Contributing
98
+
99
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcelobarreto/postgrest.
100
+
101
+ ## License
102
+
103
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'postgrest'
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
+ require 'irb'
15
+ 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/lib/postgrest.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'postgrest/version'
4
+ require 'postgrest/http'
5
+ require 'postgrest/client'
6
+ require 'postgrest/query_builder'
7
+ require 'postgrest/response'
8
+
9
+ module Postgrest
10
+ class MissingTableError < StandardError; end
11
+
12
+ class RequestError < StandardError; end
13
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Postgrest
4
+ class Client
5
+ DEFAULT_SCHEMA = 'public'
6
+
7
+ attr_reader :url, :headers, :schema
8
+
9
+ def initialize(url:, headers: {}, schema: DEFAULT_SCHEMA)
10
+ @url = URI(url)
11
+ @headers = headers
12
+ @schema = schema
13
+ end
14
+
15
+ def from(table)
16
+ raise MissingTableError if table.nil? || table.empty?
17
+
18
+ Postgrest::QueryBuilder.new(url: "#{@url}/#{table}", headers: headers, schema: schema)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module Postgrest
7
+ class HTTP
8
+ class << self
9
+ def set_request_headers(request, headers = {})
10
+ headers.each do |key, value|
11
+ request[key] = value
12
+ end
13
+
14
+ headers['Content-Type'] = 'application/json'
15
+
16
+ nil
17
+ end
18
+
19
+ def get(uri:, query: {}, headers: {})
20
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
21
+ uri.query = URI.encode_www_form(query)
22
+ req = Net::HTTP::Get.new(uri)
23
+ set_request_headers(req, headers)
24
+ http.request(req)
25
+ end
26
+
27
+ data = response_is_successful?(res) ? JSON.parse(res.body) : []
28
+
29
+ Response.new({
30
+ error: !response_is_successful?(res),
31
+ data: data,
32
+ count: data.count,
33
+ status: res.code.to_i,
34
+ status_text: res.message,
35
+ body: query
36
+ })
37
+ end
38
+
39
+ def post(uri:, body: {}, headers: {})
40
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
41
+ req = Net::HTTP::Post.new(uri)
42
+ req.body = body.to_json
43
+ req.content_type = 'application/json'
44
+ set_request_headers(req, headers)
45
+ http.request(req)
46
+ end
47
+
48
+ Response.new({
49
+ error: !response_is_successful?(res),
50
+ data: res.body,
51
+ count: nil,
52
+ status: res.code.to_i,
53
+ status_text: res.message,
54
+ body: body
55
+ })
56
+ end
57
+
58
+ private
59
+
60
+ def response_is_successful?(response)
61
+ response.class.ancestors.include?(Net::HTTPSuccess)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Postgrest
4
+ class QueryBuilder
5
+ attr_accessor :uri, :headers, :schema
6
+
7
+ def initialize(url:, headers:, schema:)
8
+ @uri = URI(url)
9
+ @headers = headers
10
+ @schema = schema
11
+ end
12
+
13
+ def select(columns = '*')
14
+ Postgrest::HTTP.get(uri: uri, query: { select: columns }, headers: headers)
15
+ end
16
+
17
+ # @client.from('todos').insert([
18
+ # { title: 'The Shire', completed: false },
19
+ # { title: 'The Shire', completed: false },
20
+ # ])
21
+
22
+ # @client.from('todos').insert({ name: 'The Shire', country_id: 554 })
23
+
24
+ def insert(values)
25
+ Postgrest::HTTP.post(uri: uri, body: values, headers: headers)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Postgrest
4
+ class Response
5
+ attr_reader :error, :data, :count, :status, :status_text, :body
6
+
7
+ def initialize(params = {})
8
+ @error = params[:error]
9
+ @data = params[:data]
10
+ @count = params[:count]
11
+ @status = params[:status]
12
+ @status_text = params[:status_text]
13
+ @body = params[:body]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Postgrest
4
+ VERSION = '0.1.0'
5
+ end
data/postgrest.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/postgrest/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'postgrest'
7
+ spec.version = Postgrest::VERSION
8
+ spec.authors = ['Marcelo Barreto']
9
+ spec.email = ['marcelobarretojunior@gmail.com']
10
+
11
+ spec.summary = 'Ruby client for PostgREST'
12
+ spec.description = 'Ruby client for PostgREST'
13
+ spec.homepage = 'https://github.com/marcelobarreto/postgrest-rb'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/marcelobarreto/postgrest-rb'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'rspec', '~> 3.5'
31
+ spec.add_development_dependency 'rubocop', '~> 1.13.0'
32
+ spec.add_development_dependency 'simplecov', '~> 0.21.2'
33
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Barreto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
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: '3.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.13.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.13.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.21.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.21.2
69
+ description: Ruby client for PostgREST
70
+ email:
71
+ - marcelobarretojunior@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".codeclimate.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/postgrest.rb
89
+ - lib/postgrest/client.rb
90
+ - lib/postgrest/http.rb
91
+ - lib/postgrest/query_builder.rb
92
+ - lib/postgrest/response.rb
93
+ - lib/postgrest/version.rb
94
+ - postgrest.gemspec
95
+ homepage: https://github.com/marcelobarreto/postgrest-rb
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ homepage_uri: https://github.com/marcelobarreto/postgrest-rb
100
+ source_code_uri: https://github.com/marcelobarreto/postgrest-rb
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.5.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.0.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Ruby client for PostgREST
120
+ test_files: []