Skip to content

Commit

Permalink
Merge pull request #7 from marcelobarreto/feat/version-0.3.0
Browse files Browse the repository at this point in the history
Version 0.3.0 Release
  • Loading branch information
marcelobarreto authored May 5, 2021
2 parents df2d9f8 + 24a3250 commit 7d780dd
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 78 deletions.
9 changes: 5 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
inherit_mode:
merge:
- Exclude
AllCops:
SuggestExtensions: false
Exclude:
- 'spec/**/*.rb'
- 'bin/**/*'
Style/Documentation:
Enabled: false
Style/HashEachMethods:
Style/MissingRespondToMissing:
Enabled: false
Lint/MissingSuper:
Naming/MethodParameterName:
Enabled: false
Lint/UriEscapeUnescape:
Enabled: false
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ before_script:
- ./cc-test-reporter before-build
- gem install bundler:2.1.4
script:
- bundle exec rubocop
- bundle exec rspec
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
postgrest (0.2.0)
postgrest (0.3.0)

GEM
remote: https://rubygems.org/
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ db.from('todos').select(:title).execute

```

#### Renaming a column name

You have the ability to alias the name of the column you want doing as follows:

```ruby
db.from('todos').select('name:title').eq(id: 112).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"name"=>"Go to the gym"}]>
```

### Querying

```ruby
Expand All @@ -64,6 +73,22 @@ db.from('todos').select('*').neq(id: 100).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"id" => 101, "title"=>"foo", "completed" => true}}]>
```

### Ordering

TODO

### Relationships

TODO

### Full query example

```ruby
db.from('todos').select(:id, :title).owners(:name, as: :owner).workers(:name, as: :worker).in(id: [112, 113]).order(id: :asc).execute

#<Postgrest::Responses::GetResponse GET OK data=[{"id"=>112, "title"=>"Eat something", "owner"=>{"name"=>"Marcelo"}, "worker"=>{"name"=>"Marcelo"}}, {"id"=>113, "title"=>"Go to the gym", "owner"=>{"name"=>"Marcelo"}, "worker"=>nil}]>
```

### Inserting

```ruby
Expand Down
4 changes: 4 additions & 0 deletions lib/postgrest.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

require 'net/http'
require 'json'
require 'cgi'

require 'postgrest/version'

# Builders
Expand Down
12 changes: 11 additions & 1 deletion lib/postgrest/builders/base_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
module Postgrest
module Builders
class BaseBuilder
def self.before_execute_hooks
@before_execute ||= []
@before_execute
end

def self.before_execute(*values)
@before_execute = values
end

attr_reader :http

def initialize(http)
@http = http
end

def call
self.class.before_execute_hooks.each { |method_name| send(method_name) }

http.call
end

alias execute call
end
end
Expand Down
86 changes: 76 additions & 10 deletions lib/postgrest/builders/filter_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,108 @@
module Postgrest
module Builders
class FilterBuilder < BaseBuilder
before_execute :set_limit, :set_offset, :update_http_instance

SIMPLE_MATCHERS = %i[eq neq gt gte lt lte like is ilike fts plfts phfts wfts].freeze
RANGE_MATCHERS = %i[sl sr nxr nxl adj].freeze

def initialize(http)
super
@inverse_next = false
end

%i[
eq neq gt gte lt lte like
ilike fts plfts phfts wfts
].each do |method_name|
SIMPLE_MATCHERS.each do |method_name|
define_method(method_name) do |values|
transform_params(method_name: method_name, values: values)

self
end
end

RANGE_MATCHERS.each do |method_name|
define_method(method_name) do |values|
query = URI.decode_www_form(http.uri.query)
values.each { |key, value| query << [key, "#{method_key(method_name)}.#{value}"] }
reset_inverse_next
http.update_query_params(query)
transform_params(method_name: method_name, values: values) do |key, value|
[key, "range=#{method_key(method_name)}.(#{value.join(',')})"]
end

self
end
end

def in(values = [])
transform_params(method_name: __method__, values: values) do |key, value|
[key, "#{method_key(__method__)}.(#{value.join(',')})"]
end

self
end

def order(values)
transform_params(method_name: __method__, values: values) do |key, value|
asc = value.to_sym != :desc
asc = !asc if should_invert?

[__method__, "#{key}.#{asc ? 'asc' : 'desc'}"]
end

self
end

def method_missing(method_name, *columns, as: nil)
decoded_query['select'] += as ? ",#{as}:#{method_name}" : ",#{method_name}"
decoded_query['select'] += columns.empty? ? '(*)' : "(#{columns.join(',')})"

self
end

def query
http.uri.query
end

def decoded_query
@decoded_query ||= URI.decode_www_form(query).to_h
end

def limit(number = 0)
decoded_query['limit'] = number
self
end

def offset(number = 0)
decoded_query['offset'] = number
self
end

def not
@inverse_next = !@inverse_next
self
end

private

def reset_inverse_next
def should_invert?
@inverse_next
end

def transform_params(values:, method_name:)
values.each do |k, v|
key, value = yield(k, v) if block_given?
key ||= k
value ||= "#{method_key(method_name)}.#{v}"

decoded_query[key.to_s] = value
end

@inverse_next = false
end

def method_key(name)
@inverse_next ? "not.#{name}" : name
should_invert? ? "not.#{name}" : name
end

# Before execute callback
def update_http_instance
http.update_query_params(decoded_query)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/postgrest/builders/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def update(values, extra_headers: {})

def delete(extra_headers: {})
extra_headers[:Prefer] ||= 'return=representation'
request = HTTP.new(uri: uri, query: {}, http_method: :delete, headers: headers.merge(extra_headers))
request = HTTP.new(uri: uri, http_method: :delete, headers: headers.merge(extra_headers))

FilterBuilder.new(request)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/postgrest/http.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require 'net/http'
require 'json'

module Postgrest
class HTTP
METHODS = {
Expand Down Expand Up @@ -33,12 +30,11 @@ def initialize(uri:, query: {}, body: {}, headers: {}, http_method: :get)
@http_method = http_method.to_sym
@response = nil
@request = nil

uri.query = URI.encode_www_form(query)
uri.query = decode_query_params(query)
end

def update_query_params(new_value = {})
@uri.query = URI.encode_www_form(new_value)
@uri.query = decode_query_params(new_value)
rescue NoMethodError
@uri.query
end
Expand All @@ -57,6 +53,10 @@ def call

private

def decode_query_params(query_params)
CGI.unescape(URI.encode_www_form(query_params))
end

def create_request
request = METHODS[http_method].new(uri)
request.body = body.to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/postgrest/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Postgrest
VERSION = '0.2.0'
VERSION = '0.3.0'
end
Loading

0 comments on commit 7d780dd

Please sign in to comment.