rest-api-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rest/api/client/config.rb +32 -0
- data/lib/rest/api/client/json_parser.rb +26 -0
- data/lib/rest/api/client/request_handler.rb +55 -0
- data/lib/rest/api/client/version.rb +3 -0
- data/lib/rest/api/client.rb +90 -0
- data/lib/rest/api/exceptions/service_url_exception.rb +4 -0
- data/rest-api-client.gemspec +32 -0
- metadata +186 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4362dca96fd11280131e0db43bb36aca549c118d
|
4
|
+
data.tar.gz: d6f0dcddcca97eafcf0c98ff8ecf563f0d35f3e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad0493709c322a34d167c72741c1df4eae1042e4ef602fe3d0d9599d0587b9d7ab73dbc1e23292a6f24f761a5b9a0addaa96636b8f8ca8b875fd3a696dda5970
|
7
|
+
data.tar.gz: 6d3133951d53dafc8b9a5d1d0a61a3ace4f1a2d4d4f92a3d1142594a62feefc7220437cbce07e824323932aaf161f29d6ffae109b2a8ac970ddbd8415d0fb83e
|
data/.gitignore
ADDED
data/.rspec
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) 2015 Victor
|
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,39 @@
|
|
1
|
+
# RestApiClient
|
2
|
+
[![Build Status](https://travis-ci.org/victor0402/rest-api-client.svg?branch=master)](https://travis-ci.org/victor0402/rest-api-client)
|
3
|
+
|
4
|
+
Common classes and methods to handle rest communication
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rest-api-client'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rest-api-client
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/victor0402/rest-api-client/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rest/api/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,32 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module RestApiClient
|
4
|
+
|
5
|
+
# Configuration defaults
|
6
|
+
@config = {
|
7
|
+
:log_level => 'verbose',
|
8
|
+
:service_key => ''
|
9
|
+
}
|
10
|
+
|
11
|
+
# Configure through hash
|
12
|
+
def self.configure(opts = {})
|
13
|
+
opts.each { |k, v| @config[k.to_sym] = v }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Configure through yaml file
|
17
|
+
def self.configure_with(path_to_yaml_file)
|
18
|
+
begin
|
19
|
+
config = YAML::load(IO.read(path_to_yaml_file))
|
20
|
+
rescue Errno::ENOENT
|
21
|
+
log(:warning, "YAML configuration file couldn't be found. Using defaults."); return
|
22
|
+
rescue Psych::SyntaxError
|
23
|
+
log(:warning, 'YAML configuration file contains invalid syntax. Using defaults.'); return
|
24
|
+
end
|
25
|
+
|
26
|
+
configure(config)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.config
|
30
|
+
@config
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RestApiClient
|
4
|
+
|
5
|
+
def self.parse_json(json, opts = {})
|
6
|
+
json_response = JSON.parse json
|
7
|
+
|
8
|
+
data_type = opts[:type]
|
9
|
+
|
10
|
+
json_data = {}
|
11
|
+
if json_response.kind_of?(Hash) && json_response.has_key?('data')
|
12
|
+
json_data = json_response['data']
|
13
|
+
end
|
14
|
+
|
15
|
+
if json_data.kind_of?(Array) && data_type
|
16
|
+
return json_data.map { |data| data_type.new data }
|
17
|
+
elsif json_data.kind_of?(Hash) && data_type
|
18
|
+
return data_type.new json_data
|
19
|
+
else
|
20
|
+
return json_data unless json_data.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
json_response
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
module RestApiClient
|
6
|
+
|
7
|
+
class RequestsHandler
|
8
|
+
|
9
|
+
def self.perform_get(service_key, path, args = {:params => {}})
|
10
|
+
RestClient.get(get_service_url(service_key) + path, args[:params]) { |response, request, result, &block|
|
11
|
+
get_response_callback(args).call(response, request, result, &block)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.perform_post(service_key, path, args = {})
|
16
|
+
RestClient.post(get_service_url(service_key) + path, args[:params]) { |response, request, result, &block|
|
17
|
+
get_response_callback(args).call(response, request, result, &block)
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.perform_put(service_key, path, args = {})
|
22
|
+
RestClient.put(get_service_url(service_key) + path, args[:params]) { |response, request, result, &block|
|
23
|
+
get_response_callback(args).call(response, request, result, &block)
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.perform_delete(service_key, path, args = {})
|
28
|
+
RestClient.delete(get_service_url(service_key) + path, args[:params]) { |response, request, result, &block|
|
29
|
+
get_response_callback(args).call(response, request, result, &block)
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.get_service_url(service_key)
|
34
|
+
redis = Redis.new
|
35
|
+
path = redis.get service_key
|
36
|
+
raise RestApiClient::ServiceUrlException.new('You must need to set the service key') unless path
|
37
|
+
path << '/' unless path.end_with?('/')
|
38
|
+
path
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.get_response_callback(args)
|
42
|
+
lambda do |response, request, result, &block|
|
43
|
+
if response.code >= 200 && response.code < 300
|
44
|
+
RestApiClient.parse_json response, args
|
45
|
+
|
46
|
+
elsif [301, 302, 307].include? response.code
|
47
|
+
response.follow_redirection(request, result, &block)
|
48
|
+
|
49
|
+
else
|
50
|
+
response.return!(request, result, &block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'rest/api/client/version'
|
3
|
+
require 'rest/api/client/json_parser'
|
4
|
+
require 'rest/api/client/config'
|
5
|
+
require 'rest/api/exceptions/service_url_exception'
|
6
|
+
require 'rest/api/client/request_handler'
|
7
|
+
|
8
|
+
module RestApiClient
|
9
|
+
|
10
|
+
class RestModel
|
11
|
+
include Virtus.model
|
12
|
+
|
13
|
+
PATH = ''
|
14
|
+
SERVICE_KEY = ''
|
15
|
+
|
16
|
+
attribute :id, Integer
|
17
|
+
attribute :created_at, Date
|
18
|
+
attribute :updated_at, Date
|
19
|
+
|
20
|
+
def self.list
|
21
|
+
perform_get path, {:type => self}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.find(id)
|
25
|
+
perform_get "#{path}/#{id}", {:type => self}
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get(id)
|
29
|
+
self.find id
|
30
|
+
end
|
31
|
+
|
32
|
+
def save!
|
33
|
+
perform_post path, {:type => self, :params => self.attributes}
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete
|
37
|
+
perform_delete "#{path}/#{id}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def update!
|
41
|
+
perform_put "#{path}/#{id}", {:type => self, :params => self.attributes}
|
42
|
+
end
|
43
|
+
|
44
|
+
def perform_get(path, args = {})
|
45
|
+
RequestsHandler.perform_get(service_key, path, args)
|
46
|
+
end
|
47
|
+
|
48
|
+
def perform_post(path, args = {})
|
49
|
+
RequestsHandler.perform_post(service_key, path, args)
|
50
|
+
end
|
51
|
+
|
52
|
+
def perform_put(path, args = {})
|
53
|
+
RequestsHandler.perform_put(service_key, path, args)
|
54
|
+
end
|
55
|
+
|
56
|
+
def perform_delete(path, args = {})
|
57
|
+
RequestsHandler.perform_delete(service_key, path, args)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.perform_get(path, args = {})
|
61
|
+
RequestsHandler.perform_get(service_key, path, args)
|
62
|
+
end
|
63
|
+
|
64
|
+
# default service_key to instance methods
|
65
|
+
def service_key
|
66
|
+
SERVICE_KEY
|
67
|
+
end
|
68
|
+
|
69
|
+
# default service_key to class methods
|
70
|
+
def self.service_key
|
71
|
+
SERVICE_KEY
|
72
|
+
end
|
73
|
+
|
74
|
+
# default path to instance methods
|
75
|
+
def path
|
76
|
+
PATH
|
77
|
+
end
|
78
|
+
|
79
|
+
# default path to class methods
|
80
|
+
def self.path
|
81
|
+
PATH
|
82
|
+
end
|
83
|
+
|
84
|
+
def ==(other)
|
85
|
+
id == other.id
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rest/api/client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rest-api-client'
|
8
|
+
spec.version = RestApiClient::VERSION
|
9
|
+
spec.authors = ['Victor']
|
10
|
+
spec.email = ['vcarvalho0402@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'rest-api-client'
|
13
|
+
spec.description = 'Common classes and methods to handle rest communication'
|
14
|
+
spec.homepage = 'https://github.com/victor0402/rest-api-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_runtime_dependency 'rest-client', '~> 1.8.0'
|
23
|
+
spec.add_runtime_dependency 'redis', '~> 3.2.1'
|
24
|
+
spec.add_runtime_dependency 'virtus'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.2.0'
|
29
|
+
spec.add_development_dependency 'factory_girl'
|
30
|
+
spec.add_development_dependency 'webmock'
|
31
|
+
spec.add_development_dependency 'simplecov'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rest-api-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Victor
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.8.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: virtus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.2.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.2.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: factory_girl
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Common classes and methods to handle rest communication
|
140
|
+
email:
|
141
|
+
- vcarvalho0402@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- Gemfile
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- bin/console
|
154
|
+
- bin/setup
|
155
|
+
- lib/rest/api/client.rb
|
156
|
+
- lib/rest/api/client/config.rb
|
157
|
+
- lib/rest/api/client/json_parser.rb
|
158
|
+
- lib/rest/api/client/request_handler.rb
|
159
|
+
- lib/rest/api/client/version.rb
|
160
|
+
- lib/rest/api/exceptions/service_url_exception.rb
|
161
|
+
- rest-api-client.gemspec
|
162
|
+
homepage: https://github.com/victor0402/rest-api-client
|
163
|
+
licenses:
|
164
|
+
- MIT
|
165
|
+
metadata: {}
|
166
|
+
post_install_message:
|
167
|
+
rdoc_options: []
|
168
|
+
require_paths:
|
169
|
+
- lib
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
requirements: []
|
181
|
+
rubyforge_project:
|
182
|
+
rubygems_version: 2.4.7
|
183
|
+
signing_key:
|
184
|
+
specification_version: 4
|
185
|
+
summary: rest-api-client
|
186
|
+
test_files: []
|