activerequest 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 +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/activerequest.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/README.md +52 -0
- data/examples/blog.rb +14 -0
- data/examples/config.rb +4 -0
- data/examples/post.rb +14 -0
- data/examples/sinatra/app.rb +63 -0
- data/lib/active_request/attributes.rb +25 -0
- data/lib/active_request/base.rb +33 -0
- data/lib/active_request/belongs_to.rb +40 -0
- data/lib/active_request/configuration.rb +11 -0
- data/lib/active_request/errors.rb +15 -0
- data/lib/active_request/has_many.rb +50 -0
- data/lib/active_request/queries.rb +109 -0
- data/lib/active_request/version.rb +3 -0
- data/lib/active_request.rb +30 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e72048573158f04d9620ed07b6971c7745b35742
|
4
|
+
data.tar.gz: b17c27e862fbcf8c09cccafdff1065ea06be5cbc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1124ed19a6d12fa5055670916d4c27c8c3d7c8a082cb05e239ab03975af0c3095e4e0897efd283f8ba77f769a85f713c6b5fadbaa831d248bf2f12d4c49d764
|
7
|
+
data.tar.gz: 8673f1fcebe8f7c33e56495fbc06bff18e496a242b538872b6b4920026ef20b2e9f4e3048ac1f02e4afbd3442d17da5d95660a384f21529b8a4c505de5510682
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alexandre Tavares
|
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,49 @@
|
|
1
|
+
# Activerequest
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_request`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'activerequest'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install activerequest
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
ActiveRequest.configure do |config|
|
27
|
+
config.uri = 'localhost:4567'
|
28
|
+
config.api_version = 'v1'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
[Examples](https://github.com/xptavares/activerequest/blob/master/examples/README.md)
|
34
|
+
|
35
|
+
|
36
|
+
## Development
|
37
|
+
|
38
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
39
|
+
|
40
|
+
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).
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerequest.
|
45
|
+
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_request/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerequest"
|
8
|
+
spec.version = ActiveRequest::VERSION
|
9
|
+
spec.authors = ["Alexandre Tavares"]
|
10
|
+
spec.email = ["xptavares@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Like ActiveRecord but from request}
|
13
|
+
spec.description = %q{Like ActiveRecord but from request}
|
14
|
+
spec.homepage = "http://rubygems.org/gems/activerequest"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "httparty"
|
23
|
+
spec.add_dependency "activesupport", '>= 4.2.7'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
spec.add_development_dependency 'mocha', '~> 1.1'
|
29
|
+
spec.add_development_dependency "minitest-vcr", "~> 1.4.0"
|
30
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_request"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/examples/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Examples
|
2
|
+
|
3
|
+
## Create Class
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
class Blog < ActiveRequest::Base
|
7
|
+
attr_accessor :id, :title
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
#### Find All
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
blogs = Blog.all # "GET /v1/blogs.json HTTP/1.1" 200
|
15
|
+
```
|
16
|
+
|
17
|
+
#### Find
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
blog = Blog.find(1) # "GET /v1/blogs/1.json HTTP/1.1" 200
|
21
|
+
```
|
22
|
+
|
23
|
+
#### Where
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
blogs = Blog.where(title: "title 1") # "GET /v1/blogs.json?title=title%201 HTTP/1.1" 200
|
27
|
+
```
|
28
|
+
|
29
|
+
#### Create
|
30
|
+
```ruby
|
31
|
+
blog = Blog.new(title: "test") # Blog.new("title" => "test")
|
32
|
+
blog.save # => true | "POST /v1/blogs.json?blog[title]=test&blog[posts_attributes][]= HTTP/1.1" 201
|
33
|
+
```
|
34
|
+
or
|
35
|
+
```ruby
|
36
|
+
blog = Blog.create(title: "Test") # "POST /v1/blogs.json?blog[title]=Test&blog[posts_attributes][]= HTTP/1.1" 201
|
37
|
+
```
|
38
|
+
|
39
|
+
#### Update
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
blog = Blog.find(1)
|
43
|
+
blog.title = "new title"
|
44
|
+
blog.save # "PUT /v1/blogs/1.json?blog[id]=1&blog[title]=new%20title&blog[posts_attributes][]= HTTP/1.1" 200
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Delete
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
blog = Blog.find(1)
|
51
|
+
blog.delete # "DELETE /v1/blogs/1.json HTTP/1.1" 200
|
52
|
+
```
|
data/examples/blog.rb
ADDED
data/examples/config.rb
ADDED
data/examples/post.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'active_request')
|
3
|
+
|
4
|
+
class Post < ActiveRequest::Base
|
5
|
+
|
6
|
+
def self.model_name
|
7
|
+
"post"
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :id, :title, :body
|
11
|
+
|
12
|
+
belongs_to :blog
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require "sinatra"
|
4
|
+
require "sinatra/json"
|
5
|
+
|
6
|
+
get '/' do
|
7
|
+
json foo: 'bar'
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/v1/blogs.json' do
|
11
|
+
b1 = { id: 1, title: 'title 1' }
|
12
|
+
b2 = { id: 2, title: 'title 2' }
|
13
|
+
blogs = [b1, b2]
|
14
|
+
blogs = blogs.select { |b| b[:id].to_s == params[:id] } if params[:id]
|
15
|
+
blogs = blogs.select { |b| b[:title] == params[:title] } if params[:title]
|
16
|
+
json blogs: blogs
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/v1/blogs/:id.json' do
|
20
|
+
id = params[:id]
|
21
|
+
json blog: { id: id.to_i, title: "title #{id}" }
|
22
|
+
end
|
23
|
+
|
24
|
+
post '/v1/blogs.json' do
|
25
|
+
status 201
|
26
|
+
json blog: { id: rand(1..9999) }
|
27
|
+
end
|
28
|
+
|
29
|
+
put '/v1/blogs/:id.json' do
|
30
|
+
id = params[:id]
|
31
|
+
json blog: { id: id.to_i }
|
32
|
+
end
|
33
|
+
|
34
|
+
delete '/v1/blogs/:id.json' do
|
35
|
+
id = params[:id]
|
36
|
+
json blog: { id: id.to_i }
|
37
|
+
end
|
38
|
+
|
39
|
+
get '/v1/posts.json' do
|
40
|
+
t1 = { id: 1, title: 'title 1', body: 'body 1', blog_id: 1 }
|
41
|
+
t2 = { id: 2, title: 'title 2', body: 'body 2', blog_id: 1 }
|
42
|
+
t3 = { id: 3, title: 'title 3', body: 'body 3', blog_id: 2 }
|
43
|
+
posts = [t1, t2, t3]
|
44
|
+
posts = posts.select { |b| b[:id].to_s == params[:id] } if params[:id]
|
45
|
+
posts = posts.select { |b| b[:title] == params[:title] } if params[:title]
|
46
|
+
posts = posts.select { |b| b[:body] == params[:body] } if params[:body]
|
47
|
+
json posts: posts
|
48
|
+
end
|
49
|
+
|
50
|
+
get '/v1/posts/:id.json' do
|
51
|
+
id = params[:id]
|
52
|
+
json post: { id: id.to_i, title: "title #{id}", body: "body #{id}", blog_id: id.to_i }
|
53
|
+
end
|
54
|
+
|
55
|
+
post '/v1/posts.json' do
|
56
|
+
status 201
|
57
|
+
json post: { id: rand(1..9999) }
|
58
|
+
end
|
59
|
+
|
60
|
+
put '/v1/posts/:id.json' do
|
61
|
+
id = params[:id]
|
62
|
+
json post: { id: id.to_i }
|
63
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveRequest
|
2
|
+
module Attributes
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
module ClassMethods
|
8
|
+
def attr_accessor(*vars)
|
9
|
+
@attributes ||= []
|
10
|
+
@attributes.concat vars
|
11
|
+
super(*vars)
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes
|
15
|
+
@attributes
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def attributes
|
21
|
+
self.class.attributes
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActiveRequest
|
2
|
+
class Base
|
3
|
+
include HTTParty
|
4
|
+
include Errors
|
5
|
+
include HasMany
|
6
|
+
include Attributes
|
7
|
+
include Queries
|
8
|
+
|
9
|
+
attr_reader :errors
|
10
|
+
|
11
|
+
def initialize(options = nil)
|
12
|
+
if options.present?
|
13
|
+
options.symbolize_keys!
|
14
|
+
attributes.each do |att|
|
15
|
+
send("#{att}=", options[att]) if options[att].present?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
self.class.base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.model_name
|
22
|
+
name.underscore.pluralize
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.new( *args, &blk )
|
26
|
+
alloc = allocate
|
27
|
+
alloc.instance_eval { initialize(*args, &blk) }
|
28
|
+
build_has_manys(alloc)
|
29
|
+
build_belongs_tos(alloc)
|
30
|
+
alloc
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveRequest
|
2
|
+
module HasMany
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
module ClassMethods
|
8
|
+
def belongs_to(association, options = nil)
|
9
|
+
@belongs_tos ||= []
|
10
|
+
class_name = !options.nil? && !options[:class_name].nil? ? options[:class_name] : (association.to_s.split.map(&:capitalize)*' ')
|
11
|
+
@belongs_tos << { association: association, class_name: class_name }
|
12
|
+
# define_method("#{association}=") do |association|
|
13
|
+
# set(association)
|
14
|
+
# end
|
15
|
+
end
|
16
|
+
|
17
|
+
def belongs_tos
|
18
|
+
@belongs_tos
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_belongs_tos(alloc)
|
22
|
+
alloc.belongs_tos.each do |many|
|
23
|
+
alloc.instance_variable_set("@#{many[:association]}", nil)
|
24
|
+
define_method(many[:association]) do
|
25
|
+
alloc.instance_variable_get("@#{many[:association]}")
|
26
|
+
end
|
27
|
+
define_method("#{many[:association]}=") do |many_setter|
|
28
|
+
alloc.instance_variable_set("@#{many[:association]}", many_setter)
|
29
|
+
end
|
30
|
+
end if alloc.belongs_tos
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
def belongs_tos
|
36
|
+
self.class.belongs_tos
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveRequest
|
2
|
+
module HasMany
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
module ClassMethods
|
8
|
+
def has_many(association, options = nil)
|
9
|
+
@has_manys ||= []
|
10
|
+
class_name = !options.nil? && !options[:class_name].nil? ? options[:class_name] : (association.to_s.split.map(&:capitalize)*' ').singularize
|
11
|
+
@has_manys << { association: association, class_name: class_name }
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_manys
|
15
|
+
@has_manys
|
16
|
+
end
|
17
|
+
|
18
|
+
def build_has_manys(alloc)
|
19
|
+
alloc.has_manys.each do |many|
|
20
|
+
alloc.instance_variable_set("@#{many[:association]}", [])
|
21
|
+
define_method(many[:association]) do
|
22
|
+
variable = alloc.instance_variable_get("@#{many[:association]}")
|
23
|
+
if id && variable.blank?
|
24
|
+
father_ojb = Object.const_get(many[:class_name])
|
25
|
+
father_model_name = father_ojb.model_name.pluralize
|
26
|
+
self.class.base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
27
|
+
response = self.class.get("/#{self.class.model_name.pluralize}/#{id}/#{father_model_name}.json", headers: self.class.headers)
|
28
|
+
return [] unless 200 == response.code
|
29
|
+
body = JSON.parse(response.body)
|
30
|
+
# TODO era para ser assim mesmo?
|
31
|
+
children = body["#{self.class.model_name}/#{father_model_name}"].map { |params| father_ojb.new(params) }
|
32
|
+
send("#{many[:association]}=", children)
|
33
|
+
variable = children
|
34
|
+
end
|
35
|
+
variable
|
36
|
+
end
|
37
|
+
define_method("#{many[:association]}=") do |many_setter|
|
38
|
+
alloc.instance_variable_set("@#{many[:association]}", many_setter)
|
39
|
+
end
|
40
|
+
end if alloc.has_manys
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
def has_manys
|
46
|
+
self.class.has_manys
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module ActiveRequest
|
2
|
+
module Queries
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
module ClassMethods
|
8
|
+
def headers
|
9
|
+
return {} unless ActiveRequest.configuration.headers
|
10
|
+
ActiveRequest.configuration.headers
|
11
|
+
end
|
12
|
+
|
13
|
+
def last
|
14
|
+
all.last
|
15
|
+
end
|
16
|
+
|
17
|
+
def first
|
18
|
+
all.first
|
19
|
+
end
|
20
|
+
|
21
|
+
def all
|
22
|
+
base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
23
|
+
response = get("/#{model_name.pluralize}.json", headers: headers)
|
24
|
+
return [] unless 200 == response.code
|
25
|
+
body = JSON.parse(response.body)
|
26
|
+
body[model_name.pluralize].map { |params| new(params) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def where(query)
|
30
|
+
base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
31
|
+
response = get("/#{model_name.pluralize}.json", query: query, headers: headers)
|
32
|
+
return [] unless 200 == response.code
|
33
|
+
body = JSON.parse(response.body)
|
34
|
+
body[model_name.pluralize].map { |params| new(params) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def find(id)
|
38
|
+
base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
|
39
|
+
response = get("/#{model_name.pluralize}/#{id}.json", headers: headers)
|
40
|
+
return [] unless 200 == response.code
|
41
|
+
body = JSON.parse(response.body)
|
42
|
+
new(body[model_name])
|
43
|
+
end
|
44
|
+
|
45
|
+
def create(query)
|
46
|
+
new_obj = new(query)
|
47
|
+
new_obj.save
|
48
|
+
new_obj
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module InstanceMethods
|
53
|
+
|
54
|
+
def delete
|
55
|
+
instance_variable_set("@errors", [])
|
56
|
+
response = self.class.delete("/#{self.class.model_name.pluralize}/#{id}.json", headers: self.class.headers)
|
57
|
+
unless 200 == response.code
|
58
|
+
instance_variable_set("@errors", response["errors"])
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
body = JSON.parse(response.body)
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
65
|
+
def save
|
66
|
+
instance_variable_set("@errors", [])
|
67
|
+
response = id.present? ? do_put : do_post
|
68
|
+
return false unless response
|
69
|
+
body = JSON.parse(response.body)
|
70
|
+
instance_variable_set("@id", body[self.class.model_name]["id"])
|
71
|
+
true
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def do_post
|
77
|
+
response = self.class.post("/#{self.class.model_name.pluralize}.json", query: query, headers: self.class.headers)
|
78
|
+
unless 201 == response.code
|
79
|
+
instance_variable_set("@errors", response["errors"])
|
80
|
+
return false
|
81
|
+
end
|
82
|
+
response
|
83
|
+
end
|
84
|
+
|
85
|
+
def do_put
|
86
|
+
response = self.class.put("/#{self.class.model_name.pluralize}/#{id}.json", query: query, headers: self.class.headers)
|
87
|
+
unless 200 == response.code
|
88
|
+
instance_variable_set("@errors", response["errors"])
|
89
|
+
return false
|
90
|
+
end
|
91
|
+
response
|
92
|
+
end
|
93
|
+
|
94
|
+
def query
|
95
|
+
many_atts = query_for_manys.reduce(:merge)
|
96
|
+
local_atts = attributes.map { |att| add_key_value(att, send(att)) }.reject(&:blank?).reduce(:merge)
|
97
|
+
{ self.class.model_name => local_atts.merge(many_atts) }
|
98
|
+
end
|
99
|
+
|
100
|
+
def query_for_manys
|
101
|
+
has_manys.map { |many| { "#{Object.const_get(many[:class_name]).model_name.pluralize}_attributes" => send(many[:association]).map { |item| item.attributes.map { |att| add_key_value(att, item.send(att)) }.reject(&:blank?).reduce(:merge) } } }
|
102
|
+
end
|
103
|
+
|
104
|
+
def add_key_value(key, value)
|
105
|
+
{ key => value } if value
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/core_ext/hash'
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
require "active_request/configuration"
|
6
|
+
require "active_request/version"
|
7
|
+
require "active_request/errors"
|
8
|
+
require "active_request/queries"
|
9
|
+
require "active_request/belongs_to"
|
10
|
+
require "active_request/has_many"
|
11
|
+
require "active_request/attributes"
|
12
|
+
require "active_request/base"
|
13
|
+
|
14
|
+
module ActiveRequest
|
15
|
+
class << self
|
16
|
+
attr_writer :configuration
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.reset
|
24
|
+
@configuration = Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure
|
28
|
+
yield(configuration)
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerequest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandre Tavares
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.2.7
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.2.7
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mocha
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: minitest-vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.4.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.4.0
|
111
|
+
description: Like ActiveRecord but from request
|
112
|
+
email:
|
113
|
+
- xptavares@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".travis.yml"
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- activerequest.gemspec
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- examples/README.md
|
128
|
+
- examples/blog.rb
|
129
|
+
- examples/config.rb
|
130
|
+
- examples/post.rb
|
131
|
+
- examples/sinatra/app.rb
|
132
|
+
- lib/active_request.rb
|
133
|
+
- lib/active_request/attributes.rb
|
134
|
+
- lib/active_request/base.rb
|
135
|
+
- lib/active_request/belongs_to.rb
|
136
|
+
- lib/active_request/configuration.rb
|
137
|
+
- lib/active_request/errors.rb
|
138
|
+
- lib/active_request/has_many.rb
|
139
|
+
- lib/active_request/queries.rb
|
140
|
+
- lib/active_request/version.rb
|
141
|
+
homepage: http://rubygems.org/gems/activerequest
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.4.5
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Like ActiveRecord but from request
|
165
|
+
test_files: []
|
166
|
+
has_rdoc:
|