trustpilot 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +12 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/trustpilot/api.rb +40 -0
- data/lib/trustpilot/api_resource.rb +56 -0
- data/lib/trustpilot/auth/authenticator.rb +44 -0
- data/lib/trustpilot/auth/token.rb +56 -0
- data/lib/trustpilot/error_checker.rb +45 -0
- data/lib/trustpilot/errors.rb +32 -0
- data/lib/trustpilot/request.rb +67 -0
- data/lib/trustpilot/resources/invitation.rb +24 -0
- data/lib/trustpilot/resources.rb +3 -0
- data/lib/trustpilot/version.rb +5 -0
- data/lib/trustpilot.rb +38 -0
- data/trustpilot.gemspec +49 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1fea718b97b7e83ba6b0501a558101f917e628e8a48d9c9f82a518d404179ef0
|
4
|
+
data.tar.gz: '09d2e7fb779fbebadb365ce347eafe47dad915ed1eb30e0abb112b73a1fd523b'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff4600bded395cf3b0c0c7aed6fd8dccf71b53fc6b8a329e004f0ad7784050f8b412840953d0e15948211d682c34a6ff4eadced65d2f4eaeee1f7ab9a6de7084
|
7
|
+
data.tar.gz: 0a989209fbefb9cae0f61ee5a703076b279effe963d7c5e7285cec8407b27af88f46ff39f0ecb9dfec50fd02a0a4b8889132971a50f2e95d4e2d19b705a6f88c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.5
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Changelog
|
2
|
+
=========
|
3
|
+
|
4
|
+
## 0.1.1 (Feb 22, 2022)
|
5
|
+
[Full Changelog](https://github.com/goldbely/trustpilot-ruby/compare/v0.1.0...v0.1.1)
|
6
|
+
|
7
|
+
- Bumped version number.
|
8
|
+
|
9
|
+
## 0.1.0 (Feb 22, 2022)
|
10
|
+
[Full Changelog](https://github.com/goldbely/trustpilot-ruby/compare/3c6bd281efb10b6f557c05f6445fb9184d8ccdcf...v0.1.0)
|
11
|
+
|
12
|
+
- Initial release.
|
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 federico@goldbelly.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,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
trustpilot (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.8.0)
|
10
|
+
public_suffix (>= 2.0.2, < 5.0)
|
11
|
+
crack (0.4.5)
|
12
|
+
rexml
|
13
|
+
diff-lcs (1.5.0)
|
14
|
+
hashdiff (1.0.1)
|
15
|
+
mock_redis (0.30.0)
|
16
|
+
ruby2_keywords
|
17
|
+
public_suffix (4.0.6)
|
18
|
+
rake (10.5.0)
|
19
|
+
rexml (3.2.5)
|
20
|
+
rspec (3.11.0)
|
21
|
+
rspec-core (~> 3.11.0)
|
22
|
+
rspec-expectations (~> 3.11.0)
|
23
|
+
rspec-mocks (~> 3.11.0)
|
24
|
+
rspec-core (3.11.0)
|
25
|
+
rspec-support (~> 3.11.0)
|
26
|
+
rspec-expectations (3.11.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.11.0)
|
29
|
+
rspec-mocks (3.11.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.11.0)
|
32
|
+
rspec-support (3.11.0)
|
33
|
+
ruby2_keywords (0.0.5)
|
34
|
+
vcr (6.0.0)
|
35
|
+
webmock (3.14.0)
|
36
|
+
addressable (>= 2.8.0)
|
37
|
+
crack (>= 0.3.2)
|
38
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
arm64-darwin-21
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 2.3)
|
45
|
+
mock_redis (~> 0.30)
|
46
|
+
rake (~> 10.0)
|
47
|
+
rspec (~> 3.0)
|
48
|
+
trustpilot!
|
49
|
+
vcr (~> 6.0)
|
50
|
+
webmock (~> 3.14)
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
2.3.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Federico Moretti
|
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,118 @@
|
|
1
|
+
# Trustpilot
|
2
|
+
|
3
|
+
This is a basic integration to [TrustPilot's API](https://developers.trustpilot.com/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'trustpilot'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install trustpilot
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
There are a few settings that have to be set globally:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# In Rails, you could put this in config/initializers/trustpilot.rb
|
27
|
+
Trustpilot.redis = Redis.new( url: REDIS_URL )
|
28
|
+
|
29
|
+
# Required
|
30
|
+
Trustpilot.api_key = MY_API_KEY
|
31
|
+
Trustpilot.api_secret = MY_API_SECRET
|
32
|
+
Trustpilot.api_email = MY_API_EMAIL
|
33
|
+
Trustpilot.api_password = MY_API_PASSWORD
|
34
|
+
|
35
|
+
# Optional
|
36
|
+
Trustpilot.default_business_unit_id = MY_DEFAULT_BUSINESS_UNIT_ID
|
37
|
+
```
|
38
|
+
|
39
|
+
**Important Note:** This gem requires Redis to work. It uses a Redis server to store access tokens.
|
40
|
+
|
41
|
+
## Debugging
|
42
|
+
|
43
|
+
HTTP requests can be logged to `$stdout` by setting:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Trustpilot.debug = true
|
47
|
+
```
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
### Invitations API
|
52
|
+
|
53
|
+
If you set your business unit id globally, you can do:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
api = Trustpilot::Invitation.new
|
57
|
+
api.service_review_link(
|
58
|
+
"locationId": "ABC123",
|
59
|
+
"referenceId": "inv00001",
|
60
|
+
"email": "federico@goldbelly.com",
|
61
|
+
"name": "Fede",
|
62
|
+
"locale": "en-US",
|
63
|
+
"tags": [
|
64
|
+
"tag1",
|
65
|
+
"tag2"
|
66
|
+
],
|
67
|
+
"redirectUri": "https://goldbelly.com"
|
68
|
+
)
|
69
|
+
|
70
|
+
# Check TrustPilot's API docs for required and optional parameters.
|
71
|
+
```
|
72
|
+
|
73
|
+
You can also specify your business unit id, for example:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
api = Trustpilot::Invitation.new "my_business_unit_id"
|
77
|
+
api.service_review_link(
|
78
|
+
"locationId": "ABC123",
|
79
|
+
"referenceId": "inv00001",
|
80
|
+
"email": "federico@goldbelly.com",
|
81
|
+
"name": "Fede",
|
82
|
+
"locale": "en-US",
|
83
|
+
"tags": [
|
84
|
+
"tag1",
|
85
|
+
"tag2"
|
86
|
+
],
|
87
|
+
"redirectUri": "https://goldbelly.com"
|
88
|
+
)
|
89
|
+
|
90
|
+
# Check TrustPilot's API docs for required and optional parameters.
|
91
|
+
```
|
92
|
+
|
93
|
+
## TODO
|
94
|
+
- Implement missing APIs.
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
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.
|
99
|
+
|
100
|
+
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).
|
101
|
+
|
102
|
+
### Test settings
|
103
|
+
|
104
|
+
Make sure you create a file called `spec/settings.yml` with your API configuration values. You can copy `spec/settings.example.yml` and replace the values.
|
105
|
+
|
106
|
+
Unfortunately, at the time of writing this README, TrustPilot does not provide a test environment.
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/goldbely/trustpilot-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
115
|
+
|
116
|
+
## Code of Conduct
|
117
|
+
|
118
|
+
Everyone interacting in the Trustpilot project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/goldbely/trustpilot-ruby/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'trustpilot'
|
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( __FILE__ )
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Trustpilot
|
8
|
+
class Api
|
9
|
+
class << self
|
10
|
+
# Sends a request to Trustpilot's API
|
11
|
+
#
|
12
|
+
# args:
|
13
|
+
# request: Trustpilot::Request
|
14
|
+
def request request
|
15
|
+
client = build_client request
|
16
|
+
|
17
|
+
response = client.request request.to_http_request
|
18
|
+
|
19
|
+
error = ErrorChecker.new( response ).error_if_appropriate
|
20
|
+
raise error if error
|
21
|
+
|
22
|
+
JSON.parse response.body
|
23
|
+
rescue JSON::ParserError
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def build_client request
|
30
|
+
# Build client
|
31
|
+
client = Net::HTTP.new request.uri.host, 443
|
32
|
+
client.use_ssl = true
|
33
|
+
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
34
|
+
client.read_timeout = Trustpilot.api_read_timeout
|
35
|
+
client.set_debug_output $stdout if Trustpilot.debug
|
36
|
+
client
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trustpilot
|
4
|
+
class ApiResource
|
5
|
+
MUTEX = Mutex.new
|
6
|
+
|
7
|
+
# Sends a request to the API
|
8
|
+
#
|
9
|
+
# This method will take care of authentication before calling the API, requesting
|
10
|
+
# a new access token if necessary.
|
11
|
+
#
|
12
|
+
# args:
|
13
|
+
# path: string
|
14
|
+
# auth_method: 'oauth' | 'key'
|
15
|
+
# params: { [key: string]: string }
|
16
|
+
# verb: 'delete', 'get', 'patch', 'post', 'put'
|
17
|
+
def request path, auth_method:, params: {}, verb: 'get'
|
18
|
+
is_oauth = auth_method.to_s == 'oauth'
|
19
|
+
|
20
|
+
MUTEX.synchronize do
|
21
|
+
renew_token if is_oauth && Auth::Token.expired?
|
22
|
+
|
23
|
+
call_api path, verb, auth_method, params
|
24
|
+
rescue AuthenticationError
|
25
|
+
# Invalid token/session, try again
|
26
|
+
renew_token
|
27
|
+
call_api path, verb, auth_method, params
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def call_api path, verb, auth_method, params
|
34
|
+
request = Request.new path, headers: auth_headers( auth_method ), params: params, verb: verb
|
35
|
+
|
36
|
+
Api.request request
|
37
|
+
end
|
38
|
+
|
39
|
+
def auth_headers auth_method
|
40
|
+
case auth_method.to_s
|
41
|
+
when 'oauth'
|
42
|
+
{ 'Authorization' => "Bearer #{ Auth::Token.get }" }
|
43
|
+
when 'key'
|
44
|
+
{ 'apikey' => Trustpilot.api_key }
|
45
|
+
else
|
46
|
+
raise Trustpilot::Error, 'unknown authentication type'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def renew_token
|
51
|
+
response = Auth::Authenticator.new.request_token
|
52
|
+
|
53
|
+
Auth::Token.set response[ 'access_token' ], ( Time.now + response[ 'expires_in' ].to_i )
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
module Trustpilot
|
6
|
+
module Auth
|
7
|
+
class Authenticator
|
8
|
+
def initialize
|
9
|
+
return if Trustpilot.api_email && Trustpilot.api_password
|
10
|
+
|
11
|
+
raise Trustpilot::Error, 'Trustpilot.api_email / Trustpilot.api_password are not set.'
|
12
|
+
end
|
13
|
+
|
14
|
+
# Gets a new access token from the API
|
15
|
+
def request_token
|
16
|
+
request = Request.new(
|
17
|
+
'oauth/oauth-business-users-for-applications/accesstoken',
|
18
|
+
headers: {
|
19
|
+
'Authorization' => auth_header,
|
20
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
21
|
+
},
|
22
|
+
params: {
|
23
|
+
grant_type: 'password',
|
24
|
+
username: Trustpilot.api_email,
|
25
|
+
password: Trustpilot.api_password
|
26
|
+
},
|
27
|
+
verb: 'post'
|
28
|
+
)
|
29
|
+
|
30
|
+
Api.request request
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the value of the authentication header to use
|
34
|
+
# when requesting an access token
|
35
|
+
def auth_header
|
36
|
+
unless Trustpilot.api_key && Trustpilot.api_secret
|
37
|
+
raise Trustpilot::Error, 'Trustpilot.api_key / Trustpilot.api_secret are not set.'
|
38
|
+
end
|
39
|
+
|
40
|
+
"Basic #{ Base64.strict_encode64( "#{ Trustpilot.api_key }:#{ Trustpilot.api_secret }" ) }"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
module Trustpilot
|
6
|
+
module Auth
|
7
|
+
class Token
|
8
|
+
class << self
|
9
|
+
def get
|
10
|
+
Trustpilot.with_redis do |r|
|
11
|
+
r.get _redis_key( 'token' )
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def set value, expires_at
|
16
|
+
Trustpilot.with_redis do |r|
|
17
|
+
r.mset _redis_key( 'token' ), value, _redis_key( 'expires_at' ), expires_at
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
Trustpilot.with_redis do |r|
|
23
|
+
r.del _redis_key( 'token' ), _redis_key( 'expires_at' )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def expired?
|
28
|
+
expiration = _token_expires_at
|
29
|
+
|
30
|
+
return true unless expiration
|
31
|
+
|
32
|
+
# TODO: is this timezone compatible?
|
33
|
+
expiration <= Time.now
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid?
|
37
|
+
!expired?
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def _token_expires_at
|
43
|
+
Trustpilot.with_redis do |r|
|
44
|
+
value = r.get _redis_key( 'expires_at' )
|
45
|
+
|
46
|
+
DateTime.parse( value ) if value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def _redis_key key
|
51
|
+
"trustpilot:auth:#{ key }"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trustpilot
|
4
|
+
class ErrorChecker
|
5
|
+
attr_reader :response
|
6
|
+
|
7
|
+
# args:
|
8
|
+
# response: Net::HTTPResponse
|
9
|
+
def initialize response
|
10
|
+
@response = response
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_if_appropriate
|
14
|
+
if code >= 500
|
15
|
+
init_error ServerError
|
16
|
+
elsif code == 429
|
17
|
+
init_error RateLimitError
|
18
|
+
elsif code == 401
|
19
|
+
init_error AuthenticationError
|
20
|
+
elsif code >= 400
|
21
|
+
init_error ClientError
|
22
|
+
elsif code >= 300
|
23
|
+
init_error ServerError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def code
|
28
|
+
response.code.to_i
|
29
|
+
end
|
30
|
+
|
31
|
+
def body
|
32
|
+
response.body
|
33
|
+
end
|
34
|
+
|
35
|
+
def message
|
36
|
+
response.message
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def init_error klass
|
42
|
+
klass.new code, body, message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trustpilot
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class APIError < ::Trustpilot::Error
|
7
|
+
attr_reader :http_status, :body, :message
|
8
|
+
|
9
|
+
def initialize http_status, body, message = nil
|
10
|
+
super()
|
11
|
+
|
12
|
+
@http_status = http_status
|
13
|
+
@body = body
|
14
|
+
@message = message
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# An error while attempting to request an access token
|
19
|
+
class OAuthTokenRequestError < APIError; end
|
20
|
+
|
21
|
+
# API rate limit
|
22
|
+
class RateLimitError < APIError; end
|
23
|
+
|
24
|
+
# Any error with a 5xx HTTP status code
|
25
|
+
class ServerError < APIError; end
|
26
|
+
|
27
|
+
# Any error with a 4xx HTTP status code
|
28
|
+
class ClientError < APIError; end
|
29
|
+
|
30
|
+
# All API authentication failures
|
31
|
+
class AuthenticationError < ClientError; end
|
32
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Trustpilot
|
6
|
+
class Request
|
7
|
+
attr_reader :headers, :params, :path, :verb
|
8
|
+
|
9
|
+
# Initializes a new request to the API
|
10
|
+
#
|
11
|
+
# args:
|
12
|
+
# path: string
|
13
|
+
# headers: { [key: string]: string }
|
14
|
+
# params: { [key: string]: string }
|
15
|
+
# verb: 'delete' | 'get' | 'patch' | 'post' | 'put'
|
16
|
+
def initialize path, headers: {}, params: {}, verb: 'get'
|
17
|
+
@headers = headers
|
18
|
+
@params = params
|
19
|
+
@path = path
|
20
|
+
@verb = verb
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the request as a Net::HTTPRequest
|
24
|
+
def to_http_request
|
25
|
+
build_request.tap do |req|
|
26
|
+
# Set headers
|
27
|
+
headers.each { |h, v| req[ h ] = v }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the full URI for the request
|
32
|
+
def uri
|
33
|
+
@uri ||=
|
34
|
+
if path.start_with? 'http'
|
35
|
+
URI path
|
36
|
+
else
|
37
|
+
URI( Trustpilot.api_url + path )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def build_request
|
44
|
+
case verb.to_s
|
45
|
+
when 'get'
|
46
|
+
build_get
|
47
|
+
when 'post'
|
48
|
+
build_post
|
49
|
+
else
|
50
|
+
raise Trustpilot::Error, 'unknown request type'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def build_get
|
55
|
+
full_uri = uri.dup
|
56
|
+
full_uri.query = URI.encode_www_form params
|
57
|
+
|
58
|
+
Net::HTTP::Get.new full_uri.request_uri
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_post
|
62
|
+
req = Net::HTTP::Post.new uri.request_uri
|
63
|
+
req.set_form_data params
|
64
|
+
req
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trustpilot
|
4
|
+
class Invitation < ApiResource
|
5
|
+
attr_reader :business_unit_id
|
6
|
+
|
7
|
+
def initialize business_unit_id = nil
|
8
|
+
super()
|
9
|
+
|
10
|
+
@business_unit_id = business_unit_id || Trustpilot.default_business_unit_id
|
11
|
+
end
|
12
|
+
|
13
|
+
# Generate a unique invitation link that can be sent to a consumer by email or website.
|
14
|
+
# https://developers.trustpilot.com/invitation-api#generate-service-review-invitation-link
|
15
|
+
def service_review_link params
|
16
|
+
request(
|
17
|
+
"https://invitations-api.trustpilot.com/v1/private/business-units/#{ business_unit_id }/invitation-links",
|
18
|
+
auth_method: :oauth,
|
19
|
+
params: params,
|
20
|
+
verb: 'post'
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/trustpilot.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Version
|
4
|
+
require 'trustpilot/version'
|
5
|
+
|
6
|
+
# Support classes
|
7
|
+
require 'trustpilot/api'
|
8
|
+
require 'trustpilot/api_resource'
|
9
|
+
require 'trustpilot/auth/authenticator'
|
10
|
+
require 'trustpilot/auth/token'
|
11
|
+
require 'trustpilot/error_checker'
|
12
|
+
require 'trustpilot/errors'
|
13
|
+
require 'trustpilot/request'
|
14
|
+
|
15
|
+
# Resources
|
16
|
+
require 'trustpilot/resources'
|
17
|
+
|
18
|
+
module Trustpilot
|
19
|
+
# Default config
|
20
|
+
@api_url = 'https://api.trustpilot.com/v1/'
|
21
|
+
|
22
|
+
class << self
|
23
|
+
attr_accessor :api_email, :api_password, :api_key, :api_secret, :api_read_timeout, :default_business_unit_id,
|
24
|
+
:debug, :redis
|
25
|
+
|
26
|
+
attr_reader :api_url
|
27
|
+
|
28
|
+
def with_redis &block
|
29
|
+
raise Trustpilot::Error, 'Trustpilot.redis is not set' unless redis
|
30
|
+
|
31
|
+
if redis.respond_to? :with
|
32
|
+
redis.with( &block )
|
33
|
+
else
|
34
|
+
yield redis
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/trustpilot.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
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 'trustpilot/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'trustpilot'
|
9
|
+
spec.version = Trustpilot::VERSION
|
10
|
+
spec.authors = [ 'Federico Moretti' ]
|
11
|
+
spec.email = [ 'federico@goldbelly.com' ]
|
12
|
+
|
13
|
+
spec.summary = 'TrustPilot API integration.'
|
14
|
+
spec.description = 'Basic TrustPilot API integration.'
|
15
|
+
spec.homepage = 'https://github.com/goldbely/trustpilot-ruby'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?( :metadata )
|
21
|
+
spec.metadata[ 'allowed_push_host' ] = 'https://rubygems.org/'
|
22
|
+
|
23
|
+
spec.metadata[ 'homepage_uri' ] = spec.homepage
|
24
|
+
spec.metadata[ 'source_code_uri' ] = 'https://github.com/goldbely/trustpilot-ruby'
|
25
|
+
spec.metadata[ 'changelog_uri' ] = 'https://github.com/goldbely/trustpilot-ruby/blob/master/CHANGELOG.md'
|
26
|
+
else
|
27
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
28
|
+
'public gem pushes.'
|
29
|
+
end
|
30
|
+
|
31
|
+
# Specify which files should be added to the gem when it is released.
|
32
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
33
|
+
spec.files = Dir.chdir( File.expand_path( __dir__ )) do
|
34
|
+
`git ls-files -z`.split( "\x0" ).reject { |f| f.match( %r{^(test|spec|features)/} ) }
|
35
|
+
end
|
36
|
+
spec.bindir = 'exe'
|
37
|
+
spec.executables = spec.files.grep( %r{^exe/} ) { |f| File.basename( f ) }
|
38
|
+
spec.require_paths = [ 'lib' ]
|
39
|
+
|
40
|
+
spec.add_development_dependency 'bundler', '~> 2.3'
|
41
|
+
spec.add_development_dependency 'mock_redis', '~> 0.30'
|
42
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
43
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
44
|
+
spec.add_development_dependency 'vcr', '~> 6.0'
|
45
|
+
spec.add_development_dependency 'webmock', '~> 3.14'
|
46
|
+
|
47
|
+
spec.metadata[ 'rubygems_mfa_required' ] = 'true'
|
48
|
+
spec.required_ruby_version = '>= 2.7.0'
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trustpilot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Federico Moretti
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-23 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: '2.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mock_redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.30'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.30'
|
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: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '6.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '6.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.14'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.14'
|
97
|
+
description: Basic TrustPilot API integration.
|
98
|
+
email:
|
99
|
+
- federico@goldbelly.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".ruby-version"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CHANGELOG.md
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/trustpilot.rb
|
118
|
+
- lib/trustpilot/api.rb
|
119
|
+
- lib/trustpilot/api_resource.rb
|
120
|
+
- lib/trustpilot/auth/authenticator.rb
|
121
|
+
- lib/trustpilot/auth/token.rb
|
122
|
+
- lib/trustpilot/error_checker.rb
|
123
|
+
- lib/trustpilot/errors.rb
|
124
|
+
- lib/trustpilot/request.rb
|
125
|
+
- lib/trustpilot/resources.rb
|
126
|
+
- lib/trustpilot/resources/invitation.rb
|
127
|
+
- lib/trustpilot/version.rb
|
128
|
+
- trustpilot.gemspec
|
129
|
+
homepage: https://github.com/goldbely/trustpilot-ruby
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata:
|
133
|
+
allowed_push_host: https://rubygems.org/
|
134
|
+
homepage_uri: https://github.com/goldbely/trustpilot-ruby
|
135
|
+
source_code_uri: https://github.com/goldbely/trustpilot-ruby
|
136
|
+
changelog_uri: https://github.com/goldbely/trustpilot-ruby/blob/master/CHANGELOG.md
|
137
|
+
rubygems_mfa_required: 'true'
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: 2.7.0
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubygems_version: 3.1.6
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: TrustPilot API integration.
|
157
|
+
test_files: []
|