api_auth-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +24 -0
- data/.gitignore +52 -0
- data/.rspec +3 -0
- data/.rubocop.yml +52 -0
- data/.rubocop_todo.yml +16 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/Gemfile +8 -0
- data/Guardfile +30 -0
- data/README.md +102 -0
- data/Rakefile +8 -0
- data/api_auth-client.gemspec +35 -0
- data/api_auth-client.sublime-project +13 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/bump +70 -0
- data/lib/api-auth-client.rb +3 -0
- data/lib/api_auth-client.rb +3 -0
- data/lib/api_auth/client.rb +14 -0
- data/lib/api_auth/client/base.rb +33 -0
- data/lib/api_auth/client/connection.rb +80 -0
- data/lib/api_auth/client/error_with_json.rb +16 -0
- data/lib/api_auth/client/response.rb +56 -0
- data/lib/api_auth/client/version.rb +7 -0
- metadata +208 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e8971d12299355746c2105b860d727dd53ccb0f
|
4
|
+
data.tar.gz: 1799f574233186f9a684215c1a7d0812ea8e2b20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 856c0df82b6d75937b380bc874ca6061d1b3d1d80d29ce239a5eb6d9742ff34c87d23ed8d0676ae498a1ed22ea69ac47f98461413eb837f8ce7f326f3ae4bc44
|
7
|
+
data.tar.gz: e4987909bac40b0cfa7722e5a8cee13f541013cd2c92e1eae36bfa04dd469f8b3b8554e24e6e10125f9235b78403effba7bfc7341f4480d43a8ce1966983d891
|
data/.editorconfig
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
|
7
|
+
[*]
|
8
|
+
|
9
|
+
# Change these settings to your own preference
|
10
|
+
indent_style = space
|
11
|
+
indent_size = 2
|
12
|
+
|
13
|
+
# We recommend you to keep these unchanged
|
14
|
+
end_of_line = lf
|
15
|
+
charset = utf-8
|
16
|
+
trim_trailing_whitespace = true
|
17
|
+
insert_final_newline = true
|
18
|
+
|
19
|
+
[*.md]
|
20
|
+
trim_trailing_whitespace = false
|
21
|
+
|
22
|
+
[nginx.conf.erb]
|
23
|
+
indent_style = tabs
|
24
|
+
indent_size = 4
|
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
51
|
+
Gemfile.lock
|
52
|
+
*.sublime-workspace
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
require: rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.3.5
|
7
|
+
|
8
|
+
Style/Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/HashSyntax:
|
12
|
+
EnforcedStyle: ruby19
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
Exclude:
|
16
|
+
- Guardfile
|
17
|
+
- 'spec/**/*'
|
18
|
+
|
19
|
+
Naming/FileName:
|
20
|
+
Exclude:
|
21
|
+
- 'lib/api-auth-client.rb'
|
22
|
+
- 'lib/api_auth-client.rb'
|
23
|
+
|
24
|
+
Style/TrailingCommaInArguments:
|
25
|
+
EnforcedStyleForMultiline: consistent_comma
|
26
|
+
|
27
|
+
Style/TrailingCommaInArrayLiteral:
|
28
|
+
EnforcedStyleForMultiline: consistent_comma
|
29
|
+
|
30
|
+
Style/TrailingCommaInHashLiteral:
|
31
|
+
EnforcedStyleForMultiline: consistent_comma
|
32
|
+
|
33
|
+
Layout/AccessModifierIndentation:
|
34
|
+
EnforcedStyle: outdent
|
35
|
+
|
36
|
+
Style/SignalException:
|
37
|
+
EnforcedStyle: semantic
|
38
|
+
|
39
|
+
Metrics/LineLength:
|
40
|
+
Max: 120
|
41
|
+
|
42
|
+
Metrics/ClassLength:
|
43
|
+
Max: 150
|
44
|
+
|
45
|
+
Metrics/ModuleLength:
|
46
|
+
Max: 150
|
47
|
+
|
48
|
+
Style/Documentation:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/HashSyntax:
|
52
|
+
EnforcedStyle: ruby19
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-11-07 21:02:20 -0600 using RuboCop version 0.60.0.
|
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: 6
|
10
|
+
# Configuration parameters: AggregateFailuresByDefault.
|
11
|
+
RSpec/MultipleExpectations:
|
12
|
+
Max: 4
|
13
|
+
|
14
|
+
# Offense count: 1
|
15
|
+
RSpec/NestedGroups:
|
16
|
+
Max: 4
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
api_auth-client
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
notification :terminal_notifier
|
4
|
+
|
5
|
+
rspec_options = {
|
6
|
+
all_after_pass: true,
|
7
|
+
cmd: 'rspec spec',
|
8
|
+
failed_mode: :focus,
|
9
|
+
}
|
10
|
+
|
11
|
+
clearing :on
|
12
|
+
|
13
|
+
guard :rspec, rspec_options do
|
14
|
+
require 'ostruct'
|
15
|
+
|
16
|
+
# Generic Ruby apps
|
17
|
+
rspec = OpenStruct.new
|
18
|
+
rspec.spec = ->(m) { "spec/#{m}_spec.rb" }
|
19
|
+
rspec.spec_dir = 'spec'
|
20
|
+
rspec.spec_helper = 'spec/spec_helper.rb'
|
21
|
+
|
22
|
+
watch(%r{^spec/.+_spec\.rb$})
|
23
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
24
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
25
|
+
end
|
26
|
+
|
27
|
+
guard :rubocop, all_on_start: true do
|
28
|
+
watch(/.+\.rb$/)
|
29
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
30
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# ApiAuth::Client
|
2
|
+
|
3
|
+
[![Gem Version][gem-version-image]][gem-version-url]
|
4
|
+
[![Build Status][travis-image]][travis-url]
|
5
|
+
[![Coverage Status][coveralls-image]][coveralls-url]
|
6
|
+
[![security][security-image]][security-url]
|
7
|
+
|
8
|
+
Use this gem to create simple API Client classes.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'api_auth-client'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install api_auth-client
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class VolabitClient < ApiAuth::Client::Base
|
30
|
+
connect url: 'https://www.volabit.com/api/v1'
|
31
|
+
|
32
|
+
def tickers
|
33
|
+
connection.get('/tickers')
|
34
|
+
end
|
35
|
+
|
36
|
+
# bang method, it will raise an error if it fails
|
37
|
+
def tickers!
|
38
|
+
connection.get!('/tickers')
|
39
|
+
end
|
40
|
+
|
41
|
+
# reqires auth
|
42
|
+
def me
|
43
|
+
connection.get('/users/me')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### Access to the response
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
client = VolabitClient.new
|
52
|
+
response = client.tickers
|
53
|
+
#<ApiAuth::Client::Response btc_mxn_buy="123255.81", btc_mxn_sell="127187.57", ltc_mxn_buy="999.96", ltc_mxn_sell="1033.07", bch_mxn_buy="10522.97", bch_mxn_sell="10873.06", xrp_mxn_buy="9.58", xrp_mxn_sell="9.9">
|
54
|
+
response[:btc_mxn_buy] == response['btc_mxn_buy'] == btc_mxn_buy.btc_mxn_buy
|
55
|
+
|
56
|
+
response[:unknown] == response['unknown'] == nil
|
57
|
+
response.unknown # raises NoMethodError
|
58
|
+
```
|
59
|
+
|
60
|
+
### Bang methods, RoR style
|
61
|
+
|
62
|
+
You can use bang `!` methods if you want the method to raise an error if the requests fails due to server error, bad requests or a bad connection.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
client = VolabitClient.new
|
66
|
+
begin
|
67
|
+
response = client.tickers!
|
68
|
+
rescue ApiAuth::Client::ConnectionError =>
|
69
|
+
e # => #<ApiAuth::Client::ConnectionError: Connection Error>
|
70
|
+
e.response
|
71
|
+
# { message: 'Failed to open TCP connection' }
|
72
|
+
e.code # nil
|
73
|
+
rescue ApiAuth::Client::ApiEndpointError => e
|
74
|
+
e # => #<ApiAuth::Client::ApiEndpointError: 500 Internal Server Error>
|
75
|
+
e.response
|
76
|
+
# <ApiAuth::Client::Response error="Bad JSON", body="500 Internal Server Error...">
|
77
|
+
e.response.body
|
78
|
+
e.code # 400
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
85
|
+
|
86
|
+
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).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_auth-client.
|
91
|
+
|
92
|
+
[gem-version-image]: https://badge.fury.io/rb/api_auth-client.svg
|
93
|
+
[gem-version-url]: https://badge.fury.io/rb/api_auth-client
|
94
|
+
|
95
|
+
[security-url]: https://hakiri.io/github/Mifiel/api-auth-client/master
|
96
|
+
[security-image]: https://hakiri.io/github/Mifiel/api-auth-client/master.svg
|
97
|
+
|
98
|
+
[travis-image]: https://travis-ci.org/Mifiel/api-auth-client.svg?branch=master
|
99
|
+
[travis-url]: https://travis-ci.org/Mifiel/api-auth-client
|
100
|
+
|
101
|
+
[coveralls-image]: https://coveralls.io/repos/github/Mifiel/api-auth-client/badge.svg?branch=master
|
102
|
+
[coveralls-url]: https://coveralls.io/github/Mifiel/api-auth-client?branch=master
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'api_auth/client/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'api_auth-client'
|
9
|
+
spec.version = ApiAuth::Client::VERSION
|
10
|
+
spec.authors = ['Genaro Madrid']
|
11
|
+
spec.email = ['genmadrid@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Base ApiClient'
|
14
|
+
spec.description = 'ApiClient for simple api calls integrated with api-auth gem'
|
15
|
+
spec.homepage = 'https://github.com/Mifiel/api-auth-client'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'api-auth', '> 1.4'
|
25
|
+
spec.add_dependency 'rest-client', '> 1.7'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
28
|
+
spec.add_development_dependency 'coveralls'
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rspec'
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.60'
|
33
|
+
spec.add_development_dependency 'rubocop-rspec'
|
34
|
+
spec.add_development_dependency 'webmock', '~> 3.4'
|
35
|
+
end
|
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 'api_auth/client'
|
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
data/bump
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
import re, glob, sys, os
|
3
|
+
|
4
|
+
__USAGE__ = \
|
5
|
+
"""BUMP is a semantic versioning bump script which accepts the following
|
6
|
+
mutually exclusive arguments:
|
7
|
+
-m - a "major" version bump equal to +1.0.0
|
8
|
+
-n - a "minor" version bump equal to +0.1.0
|
9
|
+
-p - a "patch" version bump equal to +0.0.1
|
10
|
+
-h - a "hot fix" version bump equal to +0.0.1
|
11
|
+
|
12
|
+
All of these options allow for the -r flag, which indicates that the state
|
13
|
+
is a RELEASE not a SNAPSHOT. If -r is not specified, then -SNAPSHOT is
|
14
|
+
appended to the updated version string."""
|
15
|
+
|
16
|
+
__INITIAL__ = ['0', '0', '1']
|
17
|
+
|
18
|
+
|
19
|
+
if __name__ == "__main__":
|
20
|
+
v = []
|
21
|
+
try:
|
22
|
+
version_file = glob.glob("lib/api_auth/client/version.rb")[0]
|
23
|
+
raw_v = re.search(r'VERSION = \'(.*)\'', open(version_file).read(), re.M|re.I|re.S).group(1)
|
24
|
+
v = re.split(re.compile("\.|-"), raw_v)
|
25
|
+
v = v[0:3]
|
26
|
+
map(int, v)
|
27
|
+
|
28
|
+
except ValueError:
|
29
|
+
print("failed to parse the existing VERSION file, assuming v 0.0.1")
|
30
|
+
v = ['0', '0', '1']
|
31
|
+
|
32
|
+
except FileNotFoundError:
|
33
|
+
print("failed to find a VERSION file, assuming v 0.0.0")
|
34
|
+
v = ['0', '0', '0']
|
35
|
+
|
36
|
+
op = ''
|
37
|
+
try:
|
38
|
+
op = sys.argv[1]
|
39
|
+
except:
|
40
|
+
print(__USAGE__)
|
41
|
+
sys.exit(-1)
|
42
|
+
|
43
|
+
if(op == '-m'):
|
44
|
+
v = [str(int(v[0])+1), '0', '0']
|
45
|
+
|
46
|
+
elif(op == '-n'):
|
47
|
+
v = [v[0], str(int(v[1])+1), '0']
|
48
|
+
|
49
|
+
elif(op == '-p' or op == '-h'):
|
50
|
+
v = [v[0], v[1], str(int(v[2])+1)]
|
51
|
+
|
52
|
+
else:
|
53
|
+
print(__USAGE__)
|
54
|
+
sys.exit(-1)
|
55
|
+
|
56
|
+
v = '.'.join(v)
|
57
|
+
|
58
|
+
if(op == '-h'):
|
59
|
+
os.system("git checkout -b hotfix/v%s master" % v)
|
60
|
+
|
61
|
+
else:
|
62
|
+
os.system("git checkout -b release/v%s develop" % v)
|
63
|
+
|
64
|
+
os.system("bump set %s" % v)
|
65
|
+
|
66
|
+
v += "\n"
|
67
|
+
|
68
|
+
print(v)
|
69
|
+
|
70
|
+
sys.exit(0)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'client/base'
|
4
|
+
require_relative 'client/connection'
|
5
|
+
require_relative 'client/error_with_json'
|
6
|
+
require_relative 'client/response'
|
7
|
+
require_relative 'client/version'
|
8
|
+
|
9
|
+
module ApiAuth
|
10
|
+
module Client
|
11
|
+
class ConnectionError < ErrorWithJson; end
|
12
|
+
class ApiEndpointError < ErrorWithJson; end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiAuth
|
4
|
+
module Client
|
5
|
+
class Base
|
6
|
+
def self.inherited(child_class)
|
7
|
+
child_class.class_eval do
|
8
|
+
extend ClassMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
attr_reader :attr_url, :attr_app_id, :attr_secret_key
|
14
|
+
|
15
|
+
def connect(url:, app_id: nil, secret_key: nil)
|
16
|
+
@attr_url = url
|
17
|
+
@attr_app_id = app_id
|
18
|
+
@attr_secret_key = secret_key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def connection
|
25
|
+
@connection ||= Connection.new(
|
26
|
+
url: self.class.attr_url,
|
27
|
+
app_id: self.class.attr_app_id,
|
28
|
+
secret_key: self.class.attr_secret_key,
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rest-client'
|
4
|
+
require 'api-auth'
|
5
|
+
|
6
|
+
module ApiAuth
|
7
|
+
module Client
|
8
|
+
class Connection
|
9
|
+
attr_reader :url, :app_id, :secret_key
|
10
|
+
|
11
|
+
def initialize(url:, app_id: nil, secret_key: nil)
|
12
|
+
@url = url
|
13
|
+
@app_id = app_id
|
14
|
+
@secret_key = secret_key
|
15
|
+
end
|
16
|
+
|
17
|
+
%i[
|
18
|
+
get
|
19
|
+
post
|
20
|
+
put
|
21
|
+
delete
|
22
|
+
].each do |mtd|
|
23
|
+
define_method("#{mtd}!") do |path, payload = {}|
|
24
|
+
query(mtd, path, payload)
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method(mtd) do |path, payload = {}|
|
28
|
+
begin
|
29
|
+
send("#{mtd}!", path, payload)
|
30
|
+
rescue ConnectionError, ApiEndpointError => e
|
31
|
+
e.response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def query(mtd, path, payload = {}) # rubocop:disable Metrics/AbcSize
|
37
|
+
req = build_request(mtd, path, payload)
|
38
|
+
Response.new(execute(req))
|
39
|
+
rescue RestClient::RequestTimeout, RestClient::Exceptions::OpenTimeout
|
40
|
+
raise ConnectionError.new('Error', { errors: ['Connection timeout'] }, nil)
|
41
|
+
rescue Errno::ECONNREFUSED, SocketError => e
|
42
|
+
raise ConnectionError.new('Connection Error', { message: e.message }, nil)
|
43
|
+
rescue RestClient::ExceptionWithResponse => e
|
44
|
+
response = Response.new(e.response)
|
45
|
+
msg = response[:errors] || e.message
|
46
|
+
raise ApiEndpointError.new(msg, response, response.code)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def build_request(mtd, path, payload)
|
52
|
+
params = {
|
53
|
+
method: mtd,
|
54
|
+
url: endpoint_uri(path),
|
55
|
+
ssl_version: 'SSLv23',
|
56
|
+
headers: json_headers,
|
57
|
+
}
|
58
|
+
params[:payload] = payload.to_json if mtd == :post
|
59
|
+
RestClient::Request.new(params)
|
60
|
+
end
|
61
|
+
|
62
|
+
def json_headers
|
63
|
+
{
|
64
|
+
content_type: :json,
|
65
|
+
accept: :json,
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute(req)
|
70
|
+
return req.execute if app_id.nil? || app_id.empty?
|
71
|
+
|
72
|
+
::ApiAuth.sign!(req, app_id, secret_key, with_http_method: true).execute
|
73
|
+
end
|
74
|
+
|
75
|
+
def endpoint_uri(path = '')
|
76
|
+
"#{url}/#{path.gsub(%r{^\/}, '')}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiAuth
|
4
|
+
module Client
|
5
|
+
class ErrorWithJson < StandardError
|
6
|
+
attr_accessor :response, :status, :json
|
7
|
+
|
8
|
+
def initialize(message = nil, response = nil, status = nil)
|
9
|
+
super(message)
|
10
|
+
self.response = response
|
11
|
+
self.status = status || (response.is_a?(RestClient::Response) && response.code)
|
12
|
+
self.json = response.to_json
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module ApiAuth
|
6
|
+
module Client
|
7
|
+
class Response
|
8
|
+
attr_reader :response, :parsed, :attrs
|
9
|
+
|
10
|
+
# @param response [Hash | RestClient::Response]
|
11
|
+
def initialize(response)
|
12
|
+
fail ArgumentError, 'response is not a RestClient::Response' unless response.is_a?(RestClient::Response)
|
13
|
+
|
14
|
+
@response ||= response
|
15
|
+
parse
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](arg)
|
19
|
+
parsed.send(arg) if parsed.respond_to?(arg)
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json
|
23
|
+
JSON.parse(response)
|
24
|
+
rescue JSON::ParserError
|
25
|
+
{ 'error' => 'Bad JSON', 'body' => response.body }
|
26
|
+
end
|
27
|
+
|
28
|
+
def ok?
|
29
|
+
response.code >= 200 && response.code < 400
|
30
|
+
end
|
31
|
+
|
32
|
+
def inspect
|
33
|
+
parsed.inspect.gsub('OpenStruct', 'ApiAuth::Client::Response')
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(meth, *args, &blk)
|
37
|
+
return response.send(meth, *args, &blk) if response.respond_to?(meth)
|
38
|
+
return parsed.send(meth) if parsed.respond_to?(meth)
|
39
|
+
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
def respond_to_missing?(meth, include_private = false)
|
44
|
+
response.respond_to?(meth) || parsed.respond_to?(meth) || super
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def parse
|
50
|
+
@parsed ||= JSON.parse(response, object_class: OpenStruct)
|
51
|
+
rescue JSON::ParserError
|
52
|
+
@parsed = OpenStruct.new(error: 'Bad JSON', body: response.body)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_auth-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genaro Madrid
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: api-auth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.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.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
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: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
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: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.60'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.60'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: webmock
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '3.4'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '3.4'
|
153
|
+
description: ApiClient for simple api calls integrated with api-auth gem
|
154
|
+
email:
|
155
|
+
- genmadrid@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".editorconfig"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- ".rubocop_todo.yml"
|
165
|
+
- ".ruby-gemset"
|
166
|
+
- ".ruby-version"
|
167
|
+
- ".travis.yml"
|
168
|
+
- Gemfile
|
169
|
+
- Guardfile
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- api_auth-client.gemspec
|
173
|
+
- api_auth-client.sublime-project
|
174
|
+
- bin/console
|
175
|
+
- bin/setup
|
176
|
+
- bump
|
177
|
+
- lib/api-auth-client.rb
|
178
|
+
- lib/api_auth-client.rb
|
179
|
+
- lib/api_auth/client.rb
|
180
|
+
- lib/api_auth/client/base.rb
|
181
|
+
- lib/api_auth/client/connection.rb
|
182
|
+
- lib/api_auth/client/error_with_json.rb
|
183
|
+
- lib/api_auth/client/response.rb
|
184
|
+
- lib/api_auth/client/version.rb
|
185
|
+
homepage: https://github.com/Mifiel/api-auth-client
|
186
|
+
licenses: []
|
187
|
+
metadata: {}
|
188
|
+
post_install_message:
|
189
|
+
rdoc_options: []
|
190
|
+
require_paths:
|
191
|
+
- lib
|
192
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
193
|
+
requirements:
|
194
|
+
- - ">="
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
version: '0'
|
197
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
requirements: []
|
203
|
+
rubyforge_project:
|
204
|
+
rubygems_version: 2.5.2.1
|
205
|
+
signing_key:
|
206
|
+
specification_version: 4
|
207
|
+
summary: Base ApiClient
|
208
|
+
test_files: []
|