grape-client 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/.rspec +2 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +33 -0
- data/.travis.yml +4 -0
- data/Gemfile +16 -0
- data/LICENSE +21 -0
- data/README.md +32 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/grape-client.gemspec +27 -0
- data/lib/grape_client.rb +12 -0
- data/lib/grape_client/accessors.rb +14 -0
- data/lib/grape_client/base.rb +81 -0
- data/lib/grape_client/cache.rb +40 -0
- data/lib/grape_client/collection.rb +101 -0
- data/lib/grape_client/connection.rb +69 -0
- data/lib/grape_client/response_parser.rb +40 -0
- data/lib/grape_client/rest_methods_collection.rb +60 -0
- data/lib/grape_client/rest_methods_member.rb +54 -0
- data/lib/grape_client/version.rb +3 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de222ff72b1fd8635c6551dda5832a403a867955
|
4
|
+
data.tar.gz: b708ca40554c08395acaf0c75e45f38233ca04a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc3cf3857176e2e3d02e2cf1e0cc2299921301287c228060e0234a46f67f671a7f3d4b1f585e5da26fb1049c68ba1cb08be426c648f17bd7d956cf4b9acfda32
|
7
|
+
data.tar.gz: 420adb29ac9fb753d150c46efb8f922d9b49e7a73529f54c35ab2ecca17311a49472d7f5eb603dcf583fc355b68eb6b4265a17f2ebb35b98f5c5c7ba0771d1d8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2016-03-22 11:41:17 +0200 using RuboCop version 0.37.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 3
|
10
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
11
|
+
# URISchemes: http, https
|
12
|
+
Metrics/LineLength:
|
13
|
+
Max: 104
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
# Configuration parameters: CountComments.
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Max: 15
|
19
|
+
|
20
|
+
# Offense count: 9
|
21
|
+
Style/Documentation:
|
22
|
+
Exclude:
|
23
|
+
- 'spec/**/*'
|
24
|
+
- 'test/**/*'
|
25
|
+
- 'lib/grape_client.rb'
|
26
|
+
- 'lib/grape_client/accessors.rb'
|
27
|
+
- 'lib/grape_client/base.rb'
|
28
|
+
- 'lib/grape_client/cache.rb'
|
29
|
+
- 'lib/grape_client/collection.rb'
|
30
|
+
- 'lib/grape_client/connection.rb'
|
31
|
+
- 'lib/grape_client/response_parser.rb'
|
32
|
+
- 'lib/grape_client/rest_methods_collection.rb'
|
33
|
+
- 'lib/grape_client/rest_methods_member.rb'
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem 'rubocop', '~> 0.37.0'
|
5
|
+
gem 'pry'
|
6
|
+
end
|
7
|
+
|
8
|
+
group :test do
|
9
|
+
gem 'rspec', '~> 3.4.0'
|
10
|
+
gem 'vcr', '~> 3.0.0'
|
11
|
+
gem 'webmock', '~> 1.22.0'
|
12
|
+
gem 'codeclimate-test-reporter', require: nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# Specify your gem's dependencies in grape_client.gemspec
|
16
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 DeSofto
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# GrapeClient
|
2
|
+
[](https://travis-ci.org/desofto/grape-client)
|
3
|
+
[](https://codeclimate.com/github/desofto/grape-client)
|
4
|
+
[](https://codeclimate.com/github/desofto/grape-client/coverage)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'grape-client'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install grape-client
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
## Development
|
25
|
+
|
26
|
+
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.
|
27
|
+
|
28
|
+
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).
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/desofto/grape-client.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'grape_client'
|
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
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grape_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'grape-client'
|
8
|
+
spec.version = GrapeClient::VERSION
|
9
|
+
spec.authors = ['Dmitry Silchenko']
|
10
|
+
spec.email = ['dmitry@desofto.com']
|
11
|
+
|
12
|
+
spec.summary = 'Simple access from your client to Grape APIs.'
|
13
|
+
spec.description = 'Simple access from your client to Grape APIs.'
|
14
|
+
spec.homepage = 'https://github.com/desofto/grape-client'
|
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_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
|
26
|
+
spec.add_dependency('activesupport', '~> 4.2')
|
27
|
+
end
|
data/lib/grape_client.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'grape_client/version'
|
2
|
+
require 'active_support/all'
|
3
|
+
|
4
|
+
module GrapeClient
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
Dir["#{File.dirname(__FILE__)}/grape_client/*.rb"].each do |f|
|
8
|
+
file = File.basename(f, File.extname(f))
|
9
|
+
|
10
|
+
autoload file.camelize, "grape_client/#{file}"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
module Accessors
|
3
|
+
def mattr_accessor(*attr_names)
|
4
|
+
attr_names.each do |attr_name|
|
5
|
+
define_singleton_method(attr_name) do
|
6
|
+
class_variable_get("@@#{attr_name}")
|
7
|
+
end
|
8
|
+
define_singleton_method("#{attr_name}=") do |value|
|
9
|
+
class_variable_set("@@#{attr_name}", value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
class Base
|
3
|
+
extend Accessors
|
4
|
+
mattr_accessor :site, :user, :password, :prefix
|
5
|
+
|
6
|
+
extend RestMethodsCollection
|
7
|
+
include RestMethodsMember
|
8
|
+
|
9
|
+
attr_reader :attributes
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def field_accessor(*names)
|
13
|
+
attributes = self.attributes
|
14
|
+
names.each do |name|
|
15
|
+
attributes << name.to_sym
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def connection
|
20
|
+
@connection ||= Connection.new(user, password)
|
21
|
+
end
|
22
|
+
|
23
|
+
def cache
|
24
|
+
Cache.instance
|
25
|
+
end
|
26
|
+
|
27
|
+
def entity_name
|
28
|
+
name.split('::').last.underscore
|
29
|
+
end
|
30
|
+
|
31
|
+
def endpoint
|
32
|
+
site + prefix + entity_name.pluralize
|
33
|
+
end
|
34
|
+
|
35
|
+
def attributes
|
36
|
+
class_variable_set('@@attributes', []) unless class_variable_defined?('@@attributes')
|
37
|
+
class_variable_get('@@attributes')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(attrs = {})
|
42
|
+
@attributes = {}
|
43
|
+
self.attributes = attrs
|
44
|
+
end
|
45
|
+
|
46
|
+
def attributes=(attrs)
|
47
|
+
attributes = self.class.attributes
|
48
|
+
attrs.each do |name, value|
|
49
|
+
next unless attributes.include? name.to_sym
|
50
|
+
send("#{name}=", value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](name)
|
55
|
+
name = name.to_sym
|
56
|
+
raise NameError, name unless self.class.attributes.include? name
|
57
|
+
@attributes[name]
|
58
|
+
end
|
59
|
+
|
60
|
+
def []=(name, value)
|
61
|
+
name = name.to_sym
|
62
|
+
raise NameError, name unless self.class.attributes.include? name
|
63
|
+
@attributes[name] = value
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(m, *args)
|
67
|
+
m = m.to_s
|
68
|
+
if m.last == '='
|
69
|
+
name = m[0..-2]
|
70
|
+
self[name] = args.first
|
71
|
+
else
|
72
|
+
self[m]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_post
|
77
|
+
entity_name = self.class.entity_name
|
78
|
+
attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
class Cache
|
3
|
+
def initialize
|
4
|
+
clear
|
5
|
+
end
|
6
|
+
|
7
|
+
def clear
|
8
|
+
@objects = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def fetch(params)
|
12
|
+
id = params.is_a?(Hash) ? params[:id] : params
|
13
|
+
object = @objects[id] if id.present?
|
14
|
+
unless object.present?
|
15
|
+
object = yield
|
16
|
+
store(object)
|
17
|
+
end
|
18
|
+
object
|
19
|
+
end
|
20
|
+
|
21
|
+
def store(object)
|
22
|
+
id = object.try(:id)
|
23
|
+
return unless id.present?
|
24
|
+
@objects[id] = object
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove(object)
|
28
|
+
id = object.try(:id)
|
29
|
+
return unless id.present?
|
30
|
+
@objects[id] = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
@instance = Cache.new
|
34
|
+
private_class_method :new
|
35
|
+
|
36
|
+
class << self
|
37
|
+
attr_reader :instance
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
class Collection
|
3
|
+
attr_reader :count
|
4
|
+
|
5
|
+
def initialize(clazz, elements, headers = nil)
|
6
|
+
@is_first_page = true
|
7
|
+
@clazz = clazz
|
8
|
+
update(elements, headers)
|
9
|
+
end
|
10
|
+
|
11
|
+
def first
|
12
|
+
@clazz.new(@elements.first) if @elements.any?
|
13
|
+
end
|
14
|
+
|
15
|
+
def each(&_block)
|
16
|
+
prepare_for_iteration
|
17
|
+
loop do
|
18
|
+
@elements.each do |attrs|
|
19
|
+
yield @clazz.new(attrs)
|
20
|
+
end
|
21
|
+
break unless load_next_page
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def map(&_block)
|
26
|
+
prepare_for_iteration
|
27
|
+
result = []
|
28
|
+
loop do
|
29
|
+
result += @elements.map do |attrs|
|
30
|
+
yield @clazz.new(attrs)
|
31
|
+
end
|
32
|
+
break unless load_next_page
|
33
|
+
end
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
def collect
|
38
|
+
prepare_for_iteration
|
39
|
+
result = []
|
40
|
+
loop do
|
41
|
+
result += @elements
|
42
|
+
break unless load_next_page
|
43
|
+
end
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def update(elements, headers = nil)
|
50
|
+
@elements = elements
|
51
|
+
@count = headers['total'].first.try(:to_i) if headers
|
52
|
+
@count ||= @elements.length
|
53
|
+
@first_page_link = extract_first_page_link(headers)
|
54
|
+
@next_page_link = extract_next_page_link(headers)
|
55
|
+
end
|
56
|
+
|
57
|
+
def prepare_for_iteration
|
58
|
+
return if @is_first_page
|
59
|
+
load_first_page
|
60
|
+
end
|
61
|
+
|
62
|
+
def load_next_page
|
63
|
+
return unless load_page @next_page_link
|
64
|
+
@is_first_page = false
|
65
|
+
true
|
66
|
+
end
|
67
|
+
|
68
|
+
def load_first_page
|
69
|
+
return unless load_page @first_page_link
|
70
|
+
@is_first_page = true
|
71
|
+
true
|
72
|
+
end
|
73
|
+
|
74
|
+
def load_page(link)
|
75
|
+
return unless link.present?
|
76
|
+
response = @clazz.connection.request :get, link
|
77
|
+
update ResponseParser.new(response, @clazz).collection, @clazz.connection.headers
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
def extract_first_page_link(headers)
|
82
|
+
extract_page_link(headers, 'first')
|
83
|
+
end
|
84
|
+
|
85
|
+
def extract_next_page_link(headers)
|
86
|
+
extract_page_link(headers, 'next')
|
87
|
+
end
|
88
|
+
|
89
|
+
def extract_page_link(headers, title)
|
90
|
+
return unless headers.present?
|
91
|
+
links = headers['link'].try(:first)
|
92
|
+
return unless links.present?
|
93
|
+
links = links.split(',').map do |link|
|
94
|
+
link = link.split(';').map(&:strip)
|
95
|
+
link.delete("rel=\"#{title}\"") ? link : nil
|
96
|
+
end.compact.flatten
|
97
|
+
return unless links.any?
|
98
|
+
links.first[1..-2]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module GrapeClient
|
4
|
+
class Connection
|
5
|
+
class UndefinedMethod < StandardError; end
|
6
|
+
class Unauthorized < StandardError; end
|
7
|
+
class InvalidEntity < StandardError; end
|
8
|
+
class UnknownError < StandardError; end
|
9
|
+
|
10
|
+
attr_reader :headers
|
11
|
+
|
12
|
+
def initialize(user, password)
|
13
|
+
@user = user
|
14
|
+
@password = password
|
15
|
+
end
|
16
|
+
|
17
|
+
def request(method, url, params = {})
|
18
|
+
uri = prepare_uri(method, url, params)
|
19
|
+
req = prepare_request(method, uri, params)
|
20
|
+
|
21
|
+
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
22
|
+
http.request(req)
|
23
|
+
end
|
24
|
+
|
25
|
+
@headers = res.to_hash
|
26
|
+
validate_response res
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def prepare_uri(method, url, params = {})
|
32
|
+
uri = URI(url)
|
33
|
+
if params.any? && [:get, :delete].include?(method.to_sym)
|
34
|
+
uri.query = URI.encode_www_form(params)
|
35
|
+
end
|
36
|
+
|
37
|
+
uri
|
38
|
+
end
|
39
|
+
|
40
|
+
def prepare_request(method, uri, params = {})
|
41
|
+
req = connector(method).new(uri)
|
42
|
+
req.basic_auth @user, @password
|
43
|
+
if params.any? && [:put, :post].include?(method.to_sym)
|
44
|
+
req.set_form_data(params)
|
45
|
+
end
|
46
|
+
|
47
|
+
req
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_response(res)
|
51
|
+
case res
|
52
|
+
when Net::HTTPUnauthorized then raise Unauthorized
|
53
|
+
when Net::HTTPSuccess then res.body
|
54
|
+
when Net::HTTPUnprocessableEntity then raise InvalidEntity
|
55
|
+
else raise UnknownError, res.body
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def connector(method)
|
60
|
+
case method.to_sym
|
61
|
+
when :get then Net::HTTP::Get
|
62
|
+
when :put then Net::HTTP::Put
|
63
|
+
when :post then Net::HTTP::Post
|
64
|
+
when :delete then Net::HTTP::Delete
|
65
|
+
else raise UndefinedMethod
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module GrapeClient
|
4
|
+
class ResponseParser
|
5
|
+
def initialize(response, receiver)
|
6
|
+
@response = response
|
7
|
+
@receiver = receiver
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse
|
11
|
+
if @receiver.is_a? Class
|
12
|
+
elements = collection
|
13
|
+
if elements.nil?
|
14
|
+
if parsed.present?
|
15
|
+
@receiver.new(parsed)
|
16
|
+
else
|
17
|
+
@response
|
18
|
+
end
|
19
|
+
else
|
20
|
+
Collection.new(@receiver, elements,
|
21
|
+
@receiver.connection.headers)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
@receiver.attributes = parsed
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def collection
|
29
|
+
parsed[@receiver.entity_name.pluralize] if parsed.is_a? Hash
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parsed
|
35
|
+
@parsed ||= JSON.parse @response
|
36
|
+
rescue JSON::ParserError
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
module RestMethodsCollection
|
3
|
+
def create(attrs)
|
4
|
+
object = new(attrs)
|
5
|
+
object.save!
|
6
|
+
object
|
7
|
+
end
|
8
|
+
|
9
|
+
def all
|
10
|
+
get
|
11
|
+
end
|
12
|
+
|
13
|
+
def find(params)
|
14
|
+
if params.is_a? Hash
|
15
|
+
cache.fetch(params) do
|
16
|
+
result = get(nil, params)
|
17
|
+
result.is_a?(Collection) ? result.first : result
|
18
|
+
end
|
19
|
+
else
|
20
|
+
cache.fetch(params) do
|
21
|
+
get(params)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def where(conditions)
|
27
|
+
get(nil, conditions)
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def get(method = nil, params = {}, &block)
|
33
|
+
request :get, method, params, &block
|
34
|
+
end
|
35
|
+
|
36
|
+
def put(method = nil, params = {}, &block)
|
37
|
+
request :put, method, params, &block
|
38
|
+
end
|
39
|
+
|
40
|
+
def post(method = nil, params = {}, &block)
|
41
|
+
request :post, method, params, &block
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete(method = nil, params = {}, &block)
|
45
|
+
request :delete, method, params, &block
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def request(method, url, params = {}, &_block)
|
51
|
+
url = [endpoint, url].compact.join('/')
|
52
|
+
response = connection.request method, url, params
|
53
|
+
if block_given?
|
54
|
+
yield response
|
55
|
+
else
|
56
|
+
ResponseParser.new(response, self).parse
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module GrapeClient
|
2
|
+
module RestMethodsMember
|
3
|
+
def save!
|
4
|
+
if id.present?
|
5
|
+
put(nil, to_post)
|
6
|
+
else
|
7
|
+
post(nil, to_post) do |response|
|
8
|
+
ResponseParser.new(response, self).parse
|
9
|
+
end
|
10
|
+
end
|
11
|
+
self.class.cache.store(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def save
|
15
|
+
save!
|
16
|
+
rescue Connection::InvalidEntity
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def reload
|
21
|
+
get(nil) do |response|
|
22
|
+
ResponseParser.new(response, self).parse
|
23
|
+
end
|
24
|
+
self.class.cache.store(self)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy
|
29
|
+
self.class.cache.remove(self)
|
30
|
+
delete
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def get(method, params = {}, &block)
|
36
|
+
method = [id, method].compact.join('/')
|
37
|
+
self.class.send(:get, method, params, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def put(method, params = {}, &block)
|
41
|
+
method = [id, method].compact.join('/')
|
42
|
+
self.class.send(:put, method, params, &block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def post(method, params = {}, &block)
|
46
|
+
method = [id, method].compact.join('/')
|
47
|
+
self.class.send(:post, method, params, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(params = {}, &block)
|
51
|
+
self.class.send(:delete, id, params, &block)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Silchenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.2'
|
69
|
+
description: Simple access from your client to Grape APIs.
|
70
|
+
email:
|
71
|
+
- dmitry@desofto.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".rubocop_todo.yml"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- grape-client.gemspec
|
88
|
+
- lib/grape_client.rb
|
89
|
+
- lib/grape_client/accessors.rb
|
90
|
+
- lib/grape_client/base.rb
|
91
|
+
- lib/grape_client/cache.rb
|
92
|
+
- lib/grape_client/collection.rb
|
93
|
+
- lib/grape_client/connection.rb
|
94
|
+
- lib/grape_client/response_parser.rb
|
95
|
+
- lib/grape_client/rest_methods_collection.rb
|
96
|
+
- lib/grape_client/rest_methods_member.rb
|
97
|
+
- lib/grape_client/version.rb
|
98
|
+
homepage: https://github.com/desofto/grape-client
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.4.6
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: Simple access from your client to Grape APIs.
|
122
|
+
test_files: []
|