base-api-io 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 +8 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +67 -0
- data/LICENSE.txt +21 -0
- data/README.md +174 -0
- data/Rakefile +8 -0
- data/base-api-io.gemspec +35 -0
- data/lib/base/client.rb +74 -0
- data/lib/base/endpoint.rb +70 -0
- data/lib/base/endpoints/emails.rb +36 -0
- data/lib/base/endpoints/files.rb +61 -0
- data/lib/base/endpoints/images.rb +90 -0
- data/lib/base/endpoints/passwords.rb +40 -0
- data/lib/base/endpoints/sessions.rb +29 -0
- data/lib/base/endpoints/users.rb +47 -0
- data/lib/base/errors/invalid_request.rb +20 -0
- data/lib/base/errors/unauthorized.rb +7 -0
- data/lib/base/errors/unkown_error.rb +12 -0
- data/lib/base/models/crop.rb +22 -0
- data/lib/base/models/resize.rb +18 -0
- data/lib/base/version.rb +5 -0
- data/lib/base.rb +6 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 93b0b1b6ea7a7180f59d6910a25cea3f5f1a7ca6b417d2e081c29350d9c80e4b
|
4
|
+
data.tar.gz: a36ae63196190a583e604cd5c6bdd1276fe5a77282eb2fbc3c00693bffd36981
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9966cf37c70308251eae9bdccbc098f1a38f7a932510ffd0b9fdf9b89833bf1161164694e1109a5f6f97bf8e6479fb326b4677595d4da8e96275780e353e37da
|
7
|
+
data.tar.gz: 5d9d85ed1bbbca40e653461dd2046e5b99bc7168272fce8f016ef5bf3f0120da9ebfd5b32f0b75b39f3b2d17cb380172493b453deb454ca0a9c05eb0c17ed778
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at guszti5@hotmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
base-api-io (0.1.0)
|
5
|
+
faraday (~> 0.15.4)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.6.0)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
ast (2.4.0)
|
13
|
+
crack (0.4.3)
|
14
|
+
safe_yaml (~> 1.0.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
faraday (0.15.4)
|
17
|
+
multipart-post (>= 1.2, < 3)
|
18
|
+
hashdiff (1.0.0)
|
19
|
+
jaro_winkler (1.5.3)
|
20
|
+
multipart-post (2.1.1)
|
21
|
+
parallel (1.17.0)
|
22
|
+
parser (2.6.3.0)
|
23
|
+
ast (~> 2.4.0)
|
24
|
+
public_suffix (3.1.1)
|
25
|
+
rainbow (3.0.0)
|
26
|
+
rake (10.5.0)
|
27
|
+
rspec (3.8.0)
|
28
|
+
rspec-core (~> 3.8.0)
|
29
|
+
rspec-expectations (~> 3.8.0)
|
30
|
+
rspec-mocks (~> 3.8.0)
|
31
|
+
rspec-core (3.8.2)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-expectations (3.8.4)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.8.0)
|
36
|
+
rspec-mocks (3.8.1)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.8.0)
|
39
|
+
rspec-support (3.8.2)
|
40
|
+
rubocop (0.74.0)
|
41
|
+
jaro_winkler (~> 1.5.1)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 2.6)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
ruby-progressbar (~> 1.7)
|
46
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
47
|
+
ruby-progressbar (1.10.1)
|
48
|
+
safe_yaml (1.0.5)
|
49
|
+
unicode-display_width (1.6.0)
|
50
|
+
webmock (3.6.2)
|
51
|
+
addressable (>= 2.3.6)
|
52
|
+
crack (>= 0.3.2)
|
53
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
base-api-io!
|
60
|
+
bundler (~> 1.17)
|
61
|
+
rake (~> 10.0)
|
62
|
+
rspec (~> 3.0)
|
63
|
+
rubocop (~> 0.74.0)
|
64
|
+
webmock (~> 3.6)
|
65
|
+
|
66
|
+
BUNDLED WITH
|
67
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Gusztáv Szikszai
|
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,174 @@
|
|
1
|
+
# Base
|
2
|
+
|
3
|
+
Ruby client for the [Base API](https://www.base-api.io) service, with it you
|
4
|
+
can manage authentication, email sending, files and images of your application.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
### Using RubyGems
|
9
|
+
|
10
|
+
Just run this command: `gem install base-api-io`
|
11
|
+
|
12
|
+
### Using Bundler
|
13
|
+
|
14
|
+
1. Add the dependency to your `Gemfile`:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'base-api-io', '~> 1.0.0'
|
18
|
+
```
|
19
|
+
|
20
|
+
2. Run `bundle install`
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
1. Sign up on [www.base-api.io](https://www.base-api.io) and create an
|
25
|
+
application and copy its access token.
|
26
|
+
|
27
|
+
2. Require the shard:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'base'
|
31
|
+
```
|
32
|
+
|
33
|
+
3. Create a client:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
client =
|
37
|
+
Base::Client.new(access_token: "your_access_token")
|
38
|
+
```
|
39
|
+
|
40
|
+
### Sending email
|
41
|
+
|
42
|
+
Using the `emails` endpoint on the client you can send emails:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Sending an email
|
46
|
+
email =
|
47
|
+
client.emails.send(
|
48
|
+
from: "from@example.com",
|
49
|
+
to: "to@example.com",
|
50
|
+
subject: "Test Email",
|
51
|
+
html: "<b>Html message</b>",
|
52
|
+
text: "Text message")
|
53
|
+
```
|
54
|
+
|
55
|
+
### Users
|
56
|
+
|
57
|
+
Using the `users` endpoint on the client you can create / get or delete users:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# Create a user with email / password
|
61
|
+
user =
|
62
|
+
client.users.create(
|
63
|
+
email: "test@user.com",
|
64
|
+
confirmation: "12345",
|
65
|
+
password: "12345")
|
66
|
+
|
67
|
+
# Get a users details by the id
|
68
|
+
user =
|
69
|
+
client.users.get("user_id")
|
70
|
+
|
71
|
+
# Delete a user by id
|
72
|
+
user =
|
73
|
+
client.users.delete("user_id")
|
74
|
+
```
|
75
|
+
|
76
|
+
### Sessions
|
77
|
+
|
78
|
+
Using the `sessions` endpoint on the client you can authenticate a user.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# Create a user with email / password
|
82
|
+
user =
|
83
|
+
client.sessions.authenticate(
|
84
|
+
email: "test@user.com",
|
85
|
+
password: "12345")
|
86
|
+
```
|
87
|
+
|
88
|
+
### Forgot Password Flow
|
89
|
+
|
90
|
+
Using the `passwords` endpoint on the client you can perform a forgot password flow.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# Create an forgot password token for the user with the given email address.
|
94
|
+
token =
|
95
|
+
client.passwords.forgot_password(email: "test@user.com")
|
96
|
+
|
97
|
+
# Using that token set a new password.
|
98
|
+
user =
|
99
|
+
client.passwords.set_password(
|
100
|
+
forgot_password_token: token.forgot_password_token,
|
101
|
+
confirmation: "123456",
|
102
|
+
password: "123456")
|
103
|
+
```
|
104
|
+
|
105
|
+
### Files
|
106
|
+
|
107
|
+
Using the `files` endpoint on the client you can create / get / delete or
|
108
|
+
download files:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# Create a file
|
112
|
+
file =
|
113
|
+
client.files.create(file: File.open("/path/to/file"))
|
114
|
+
|
115
|
+
# Get a file by id
|
116
|
+
file =
|
117
|
+
client.files.get("file_id")
|
118
|
+
|
119
|
+
# Delete a file by id
|
120
|
+
file =
|
121
|
+
client.files.delete("file_id")
|
122
|
+
|
123
|
+
# Get a download URL to the file by id
|
124
|
+
url =
|
125
|
+
client.files.download_url("file_id")
|
126
|
+
|
127
|
+
# Download the file by id into an IO
|
128
|
+
io =
|
129
|
+
client.files.download("file_id")
|
130
|
+
```
|
131
|
+
|
132
|
+
### Images
|
133
|
+
|
134
|
+
Using the `images` endpoint on the client you can create / get / delete or
|
135
|
+
process images:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
# Create an image
|
139
|
+
image =
|
140
|
+
client.images.create(image: File.open("/path/to/image"))
|
141
|
+
|
142
|
+
# Get a image by id
|
143
|
+
image =
|
144
|
+
client.images.get("image_id")
|
145
|
+
|
146
|
+
# Delete a image by id
|
147
|
+
image =
|
148
|
+
client.images.delete("image_id")
|
149
|
+
|
150
|
+
# Get a link to a prcessed version (crop & resize) of the image by id
|
151
|
+
url =
|
152
|
+
client.images.image_url(i.id,
|
153
|
+
crop: Base::Crop.new(width: 100, height: 100, top: 0, left: 0),
|
154
|
+
resize: Base::Resize.new(width: 100, height: 100),
|
155
|
+
format: "jpg",
|
156
|
+
quality: 10)
|
157
|
+
```
|
158
|
+
|
159
|
+
## Development
|
160
|
+
|
161
|
+
This library uses [Faraday](https://lostisland.github.io/faraday/), you can run the
|
162
|
+
specs locally with `rspec`.
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
1. Fork it (<https://github.com/base-api-io/base-cr/fork>)
|
167
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
168
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
169
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
170
|
+
5. Create a new Pull Request
|
171
|
+
|
172
|
+
## Contributors
|
173
|
+
|
174
|
+
- [Gusztav Szikszai](https://github.com/gdotdesign) - creator and maintainer
|
data/Rakefile
ADDED
data/base-api-io.gemspec
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 'base/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'base-api-io'
|
9
|
+
spec.version = Base::VERSION
|
10
|
+
spec.authors = ['Gusztáv Szikszai']
|
11
|
+
spec.email = ['guszti5@hotmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Ruby client library for the Base API'
|
14
|
+
spec.description = 'Ruby client library for the Base API'
|
15
|
+
spec.homepage = 'https://www.base-api.io'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
f.match(%r{^(test|spec|features)/})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_dependency 'faraday', '~> 0.15.4'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
33
|
+
spec.add_development_dependency 'rubocop', '~> 0.74.0'
|
34
|
+
spec.add_development_dependency 'webmock', '~> 3.6'
|
35
|
+
end
|
data/lib/base/client.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'models/crop'
|
4
|
+
require_relative 'models/resize'
|
5
|
+
|
6
|
+
require_relative 'endpoint'
|
7
|
+
require_relative 'endpoints/passwords'
|
8
|
+
require_relative 'endpoints/sessions'
|
9
|
+
require_relative 'endpoints/images'
|
10
|
+
require_relative 'endpoints/emails'
|
11
|
+
require_relative 'endpoints/users'
|
12
|
+
require_relative 'endpoints/files'
|
13
|
+
|
14
|
+
module Base
|
15
|
+
# A client containing all the endpoints.
|
16
|
+
class Client
|
17
|
+
# Endpoint for the forgot password flow.
|
18
|
+
attr_reader :passwords
|
19
|
+
|
20
|
+
# Endpoint for the sessions.
|
21
|
+
attr_reader :sessions
|
22
|
+
|
23
|
+
# Endpoint for the emails.
|
24
|
+
attr_reader :emails
|
25
|
+
|
26
|
+
# Endpoint for the images.
|
27
|
+
attr_reader :images
|
28
|
+
|
29
|
+
# Endpoint for the users.
|
30
|
+
attr_reader :users
|
31
|
+
|
32
|
+
# Endpoint for the files.
|
33
|
+
attr_reader :files
|
34
|
+
|
35
|
+
# Initializes a new client with an access_token and optional url.
|
36
|
+
def initialize(access_token:, url: 'https://api.base-api.io')
|
37
|
+
@users =
|
38
|
+
Endpoints::Users.new(
|
39
|
+
access_token: access_token,
|
40
|
+
url: url
|
41
|
+
)
|
42
|
+
|
43
|
+
@files =
|
44
|
+
Endpoints::Files.new(
|
45
|
+
access_token: access_token,
|
46
|
+
url: url
|
47
|
+
)
|
48
|
+
|
49
|
+
@images =
|
50
|
+
Endpoints::Images.new(
|
51
|
+
access_token: access_token,
|
52
|
+
url: url
|
53
|
+
)
|
54
|
+
|
55
|
+
@sessions =
|
56
|
+
Endpoints::Sessions.new(
|
57
|
+
access_token: access_token,
|
58
|
+
url: url
|
59
|
+
)
|
60
|
+
|
61
|
+
@emails =
|
62
|
+
Endpoints::Emails.new(
|
63
|
+
access_token: access_token,
|
64
|
+
url: url
|
65
|
+
)
|
66
|
+
|
67
|
+
@passwords =
|
68
|
+
Endpoints::Passwords.new(
|
69
|
+
access_token: access_token,
|
70
|
+
url: url
|
71
|
+
)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'errors/invalid_request'
|
4
|
+
require_relative 'errors/unauthorized'
|
5
|
+
require_relative 'errors/unkown_error'
|
6
|
+
|
7
|
+
module Base
|
8
|
+
# Middleware to raise error on 422 and 401
|
9
|
+
class RaiseError < Faraday::Response::Middleware
|
10
|
+
def on_complete(env)
|
11
|
+
case env[:status]
|
12
|
+
when 422
|
13
|
+
raise InvalidRequest, JSON.parse(env.body)
|
14
|
+
when 401
|
15
|
+
raise Unauthorized
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Base
|
22
|
+
# The base class for an endpoint.
|
23
|
+
#
|
24
|
+
# It handles request lifecycle and error handling and offers a
|
25
|
+
# Faraday::Connection
|
26
|
+
class Endpoint
|
27
|
+
attr_reader :connection
|
28
|
+
attr_reader :path
|
29
|
+
|
30
|
+
# Initializes the endpoint with an access_token and url.
|
31
|
+
def initialize(access_token:, url:)
|
32
|
+
@connection =
|
33
|
+
Faraday.new(
|
34
|
+
"#{url}/v1/#{path}/",
|
35
|
+
headers: { 'Authorization' => "Bearer #{access_token}" }
|
36
|
+
) do |conn|
|
37
|
+
conn.use RaiseError
|
38
|
+
conn.use Faraday::Adapter::NetHttp
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Handles errors that happen in its block.
|
43
|
+
def request
|
44
|
+
yield
|
45
|
+
rescue Unauthorized, InvalidRequest => e
|
46
|
+
raise e
|
47
|
+
rescue StandardError => e
|
48
|
+
raise UnkownError.new(e)
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse(body)
|
52
|
+
object = JSON.parse(body, object_class: OpenStruct)
|
53
|
+
|
54
|
+
object.created_at = Time.rfc2822(object.created_at) if object.created_at
|
55
|
+
|
56
|
+
object
|
57
|
+
end
|
58
|
+
|
59
|
+
def io(body)
|
60
|
+
case body
|
61
|
+
when IO
|
62
|
+
body
|
63
|
+
when String
|
64
|
+
StringIO.new(body)
|
65
|
+
else
|
66
|
+
IO.try_convert(body)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains methods for sending emails.
|
6
|
+
class Emails < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'email'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Sends an email with the given parameters.
|
14
|
+
#
|
15
|
+
# If there is no sending domain set up all emails will use the
|
16
|
+
# `proxy@base-api.io` sender and ignore the given one, also in this case
|
17
|
+
# there is a rate limit which is 30 emails in an hour.
|
18
|
+
#
|
19
|
+
# If there is a sending domain, the sender must match that domain
|
20
|
+
# otherwise it will return an error.
|
21
|
+
def send(subject:, from:, html:, text:, to:)
|
22
|
+
request do
|
23
|
+
response =
|
24
|
+
connection.post('',
|
25
|
+
'from' => from,
|
26
|
+
'to' => to,
|
27
|
+
'subject' => subject,
|
28
|
+
'html' => html,
|
29
|
+
'text' => text)
|
30
|
+
|
31
|
+
parse(response.body)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains methods for uploading and managing files.
|
6
|
+
class Files < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'files'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Uploads the given file and returns its metadata.
|
14
|
+
def create(file:)
|
15
|
+
request do
|
16
|
+
response =
|
17
|
+
connection.post('', 'file' => file)
|
18
|
+
|
19
|
+
parse(response.body)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the publicly accessible download URL of the file with the
|
24
|
+
# given ID.
|
25
|
+
def download_url(id)
|
26
|
+
"#{connection.url_prefix}#{id}/download"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Downloads the file with the given ID into an IO.
|
30
|
+
def download(id)
|
31
|
+
response =
|
32
|
+
Faraday.new(download_url(id)) do |conn|
|
33
|
+
conn.use RaiseError
|
34
|
+
conn.use Faraday::Adapter::NetHttp
|
35
|
+
end.get
|
36
|
+
|
37
|
+
io(response.body)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the metadata of the file with the given ID.
|
41
|
+
def get(id)
|
42
|
+
request do
|
43
|
+
response =
|
44
|
+
connection.get id
|
45
|
+
|
46
|
+
parse(response.body)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Deletes the file with the given ID.
|
51
|
+
def delete(id)
|
52
|
+
request do
|
53
|
+
response =
|
54
|
+
connection.delete id
|
55
|
+
|
56
|
+
parse(response.body)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains methods for uploading and managing images.
|
6
|
+
class Images < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'images'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Uploads the given image and returns its metadata.
|
14
|
+
#
|
15
|
+
# Only images with ImageMagick understands can be uploaded otherwise it
|
16
|
+
# will raise an error.
|
17
|
+
def create(image:)
|
18
|
+
request do
|
19
|
+
response =
|
20
|
+
connection.post('', 'image' => image)
|
21
|
+
|
22
|
+
parse(response.body)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the image url of the image with the given ID.
|
27
|
+
#
|
28
|
+
# It is possible to crop and resize the image and change its format
|
29
|
+
# and quality.
|
30
|
+
def image_url(id,
|
31
|
+
quality: nil,
|
32
|
+
resize: nil,
|
33
|
+
format: nil,
|
34
|
+
crop: nil)
|
35
|
+
params = {}
|
36
|
+
|
37
|
+
quality && params[:quality] = quality.to_s
|
38
|
+
format && params[:format] = format.to_s
|
39
|
+
resize && params[:resize] = resize.to_s
|
40
|
+
crop && params[:crop] = crop.to_s
|
41
|
+
|
42
|
+
"#{connection.url_prefix}#{id}/version?#{URI.encode_www_form(params)}"
|
43
|
+
end
|
44
|
+
|
45
|
+
# Downloads the image with the given ID.
|
46
|
+
#
|
47
|
+
# It is possible to crop and resize the image and change its format
|
48
|
+
# and quality.
|
49
|
+
def download(id,
|
50
|
+
quality: nil,
|
51
|
+
resize: nil,
|
52
|
+
format: nil,
|
53
|
+
crop: nil)
|
54
|
+
url =
|
55
|
+
image_url(id, quality: quality,
|
56
|
+
resize: resize,
|
57
|
+
format: format,
|
58
|
+
crop: crop)
|
59
|
+
|
60
|
+
response =
|
61
|
+
Faraday.new(url) do |conn|
|
62
|
+
conn.use RaiseError
|
63
|
+
conn.use Faraday::Adapter::NetHttp
|
64
|
+
end.get
|
65
|
+
|
66
|
+
io(response.body)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns the metadata of the image with the given ID.
|
70
|
+
def get(id)
|
71
|
+
request do
|
72
|
+
response =
|
73
|
+
connection.get id
|
74
|
+
|
75
|
+
parse(response.body)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Deletes the image with the given ID.
|
80
|
+
def delete(id)
|
81
|
+
request do
|
82
|
+
response =
|
83
|
+
connection.delete id
|
84
|
+
|
85
|
+
parse(response.body)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains methods for handling the forgot password flow.
|
6
|
+
class Passwords < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'password'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Generates a forgot password token for the user with the given email.
|
14
|
+
def forgot_password(email:)
|
15
|
+
request do
|
16
|
+
response =
|
17
|
+
connection.post('',
|
18
|
+
'email' => email)
|
19
|
+
|
20
|
+
parse(response.body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sets the password of a user with the given forgot password token.
|
25
|
+
def set_password(forgot_password_token:,
|
26
|
+
confirmation:,
|
27
|
+
password:)
|
28
|
+
request do
|
29
|
+
response =
|
30
|
+
connection.put('',
|
31
|
+
'forgot_password_token' => forgot_password_token,
|
32
|
+
'confirmation' => confirmation,
|
33
|
+
'password' => password)
|
34
|
+
|
35
|
+
parse(response.body)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains a method for authenticating a user.
|
6
|
+
class Sessions < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'sessions'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Tries to authenticate (log in) the user with email and password.
|
14
|
+
#
|
15
|
+
# For security reasons if the email address is not registered or the
|
16
|
+
# password is incorrect, "INVALID_CREDENTIALS" error will be returned.
|
17
|
+
def authenticate(email:, password:)
|
18
|
+
request do
|
19
|
+
response =
|
20
|
+
connection.post('',
|
21
|
+
'password' => password,
|
22
|
+
'email' => email)
|
23
|
+
|
24
|
+
parse(response.body)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
module Endpoints
|
5
|
+
# This endpoint contains methods for creating and managing users.
|
6
|
+
class Users < Endpoint
|
7
|
+
# Initializes this endpoint.
|
8
|
+
def initialize(access_token:, url:)
|
9
|
+
@path = 'users'
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Creates a user with the given credentials.
|
14
|
+
def create(email:, password:, confirmation:)
|
15
|
+
request do
|
16
|
+
response =
|
17
|
+
connection.post('',
|
18
|
+
'confirmation' => confirmation,
|
19
|
+
'password' => password,
|
20
|
+
'email' => email)
|
21
|
+
|
22
|
+
parse(response.body)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Gets the details of the user with the given ID.
|
27
|
+
def get(id)
|
28
|
+
request do
|
29
|
+
response =
|
30
|
+
connection.get id
|
31
|
+
|
32
|
+
parse(response.body)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Deletes the user with the given ID.
|
37
|
+
def delete(id)
|
38
|
+
request do
|
39
|
+
response =
|
40
|
+
connection.delete id
|
41
|
+
|
42
|
+
parse(response.body)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
# The error for invalid API requests, which are returned from API in the
|
5
|
+
# following JSON format:
|
6
|
+
#
|
7
|
+
# {
|
8
|
+
# "error": "TYPE_OF_ERROR",
|
9
|
+
# "data": {
|
10
|
+
# "key": "Additional information."
|
11
|
+
# }
|
12
|
+
# }
|
13
|
+
class InvalidRequest < StandardError
|
14
|
+
attr_reader :data
|
15
|
+
|
16
|
+
def initialize(data)
|
17
|
+
@data = data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
# Class for the crop data.
|
5
|
+
class Crop
|
6
|
+
attr_reader :height
|
7
|
+
attr_reader :width
|
8
|
+
attr_reader :left
|
9
|
+
attr_reader :top
|
10
|
+
|
11
|
+
def initialize(width:, height:, left:, top:)
|
12
|
+
@height = height
|
13
|
+
@width = width
|
14
|
+
@left = left
|
15
|
+
@top = top
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
[@width, @height, @left, @top].join('_')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Base
|
4
|
+
# Class for the resize data.
|
5
|
+
class Resize
|
6
|
+
attr_reader :height
|
7
|
+
attr_reader :width
|
8
|
+
|
9
|
+
def initialize(width:, height:)
|
10
|
+
@height = height
|
11
|
+
@width = width
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
[@width, @height].join('_')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/base/version.rb
ADDED
data/lib/base.rb
ADDED
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: base-api-io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gusztáv Szikszai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.15.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.15.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.74.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.74.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.6'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.6'
|
97
|
+
description: Ruby client library for the Base API
|
98
|
+
email:
|
99
|
+
- guszti5@hotmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- base-api-io.gemspec
|
114
|
+
- lib/base.rb
|
115
|
+
- lib/base/client.rb
|
116
|
+
- lib/base/endpoint.rb
|
117
|
+
- lib/base/endpoints/emails.rb
|
118
|
+
- lib/base/endpoints/files.rb
|
119
|
+
- lib/base/endpoints/images.rb
|
120
|
+
- lib/base/endpoints/passwords.rb
|
121
|
+
- lib/base/endpoints/sessions.rb
|
122
|
+
- lib/base/endpoints/users.rb
|
123
|
+
- lib/base/errors/invalid_request.rb
|
124
|
+
- lib/base/errors/unauthorized.rb
|
125
|
+
- lib/base/errors/unkown_error.rb
|
126
|
+
- lib/base/models/crop.rb
|
127
|
+
- lib/base/models/resize.rb
|
128
|
+
- lib/base/version.rb
|
129
|
+
homepage: https://www.base-api.io
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata: {}
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
requirements: []
|
148
|
+
rubygems_version: 3.0.3
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Ruby client library for the Base API
|
152
|
+
test_files: []
|