simple_advice 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +24 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +16 -0
- data/lib/simple_advice/client.rb +41 -0
- data/lib/simple_advice/collection.rb +23 -0
- data/lib/simple_advice/config.rb +11 -0
- data/lib/simple_advice/error.rb +5 -0
- data/lib/simple_advice/object.rb +22 -0
- data/lib/simple_advice/objects/advice.rb +5 -0
- data/lib/simple_advice/version.rb +5 -0
- data/lib/simple_advice.rb +33 -0
- data/simple_advice.gemspec +32 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6cc4699756da14544d1b61e55a8a20c809565df5a21dd075a92c0e0cf6445bc7
|
4
|
+
data.tar.gz: 65f1a69af6637c28a9c5eddc4e3dcceba34762ad207fb9319c587da342b34189
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 12ed08d5e6a436c6147dbae7f19ab0d9bae029f224870b9053290e7d23b0c5f7c232a32aef704945126541b12d8c95bd340efc53cb23b0a2596f83bb49029134
|
7
|
+
data.tar.gz: b0c3ac08d1017e1d9ee7f3baab4cc85e018224bd78d2465a1579d96cbb23feb2b55473eb758650ce702adce389edd90ec89aa40ffe821121b0414f9cc113d35c
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
TargetRubyVersion: 2.6
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/StringLiteralsInInterpolation:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/OpenStructUse:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Layout/LineLength:
|
21
|
+
Max: 120
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 15
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
simple_advice (0.1.0)
|
5
|
+
faraday (~> 1.7)
|
6
|
+
faraday_middleware (~> 1.1)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.2)
|
12
|
+
faraday (1.10.0)
|
13
|
+
faraday-em_http (~> 1.0)
|
14
|
+
faraday-em_synchrony (~> 1.0)
|
15
|
+
faraday-excon (~> 1.1)
|
16
|
+
faraday-httpclient (~> 1.0)
|
17
|
+
faraday-multipart (~> 1.0)
|
18
|
+
faraday-net_http (~> 1.0)
|
19
|
+
faraday-net_http_persistent (~> 1.0)
|
20
|
+
faraday-patron (~> 1.0)
|
21
|
+
faraday-rack (~> 1.0)
|
22
|
+
faraday-retry (~> 1.0)
|
23
|
+
ruby2_keywords (>= 0.0.4)
|
24
|
+
faraday-em_http (1.0.0)
|
25
|
+
faraday-em_synchrony (1.0.0)
|
26
|
+
faraday-excon (1.1.0)
|
27
|
+
faraday-httpclient (1.0.1)
|
28
|
+
faraday-multipart (1.0.3)
|
29
|
+
multipart-post (>= 1.2, < 3)
|
30
|
+
faraday-net_http (1.0.1)
|
31
|
+
faraday-net_http_persistent (1.2.0)
|
32
|
+
faraday-patron (1.0.0)
|
33
|
+
faraday-rack (1.0.0)
|
34
|
+
faraday-retry (1.0.3)
|
35
|
+
faraday_middleware (1.2.0)
|
36
|
+
faraday (~> 1.0)
|
37
|
+
minitest (5.15.0)
|
38
|
+
multipart-post (2.1.1)
|
39
|
+
parallel (1.22.1)
|
40
|
+
parser (3.1.1.0)
|
41
|
+
ast (~> 2.4.1)
|
42
|
+
rainbow (3.1.1)
|
43
|
+
rake (13.0.6)
|
44
|
+
regexp_parser (2.2.1)
|
45
|
+
rexml (3.2.5)
|
46
|
+
rubocop (1.27.0)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 3.1.0.0)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
regexp_parser (>= 1.8, < 3.0)
|
51
|
+
rexml
|
52
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
55
|
+
rubocop-ast (1.16.0)
|
56
|
+
parser (>= 3.1.1.0)
|
57
|
+
ruby-progressbar (1.11.0)
|
58
|
+
ruby2_keywords (0.0.5)
|
59
|
+
unicode-display_width (2.1.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
x86_64-darwin-19
|
63
|
+
x86_64-darwin-21
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
minitest (~> 5.0)
|
67
|
+
rake (~> 13.0)
|
68
|
+
rubocop (~> 1.21)
|
69
|
+
simple_advice!
|
70
|
+
|
71
|
+
BUNDLED WITH
|
72
|
+
2.3.7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Dmytro Havrysh
|
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,63 @@
|
|
1
|
+
# SimpleAdvice [](https://app.travis-ci.com/DmytroHavryshGoTo/simple_advice)
|
2
|
+
|
3
|
+
Implementation of [Advice Slip JSON API](https://api.adviceslip.com/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_advice'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_advice
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Everthing is as simple as it could be.
|
24
|
+
|
25
|
+
### Get random advice
|
26
|
+
```ruby
|
27
|
+
rnd = SimpleAdvice.random # => #<SimpleAdvice::Advice>
|
28
|
+
|
29
|
+
rnd.id # => 77
|
30
|
+
rnd.advice # => "Mercy is the better part of justice."
|
31
|
+
```
|
32
|
+
|
33
|
+
### Find advice by id
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
SimpleAdvice.find(advice_id: 77) # => #<SimpleAdvice::Advice>
|
37
|
+
```
|
38
|
+
|
39
|
+
### Find advice by text
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
result = SimpleAdvice.find_by(query: "nice") # => #<SimpleAdvice::Collection>
|
43
|
+
|
44
|
+
result.data # => [#<SimpleAdvice::Advice>]
|
45
|
+
|
46
|
+
result.total # => 1
|
47
|
+
```
|
48
|
+
|
49
|
+
## Config
|
50
|
+
|
51
|
+
SimpleAdvice uses Faraday to interact via HTTP, you can provide any HTTP adapter that Faraday supports(if you are using Rails put the following code into `initializers/simple_advice.rb`):
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
SimpleAdvice::Config.http_adapter = :test
|
55
|
+
```
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/DmytroHavryshGoTo/simple_advice.
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleAdvice
|
4
|
+
class Client
|
5
|
+
BASE_URL = "https://api.adviceslip.com"
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def get_request(url, params: {}, headers: {})
|
9
|
+
handle_response connection.get(url, params, headers)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def connection
|
15
|
+
Faraday.new(BASE_URL) do |conn|
|
16
|
+
conn.request :json
|
17
|
+
# Can't use because the response is always text/html format and when 500 error happens it crashes
|
18
|
+
# conn.response :json
|
19
|
+
conn.adapter Config.http_adapter, Config.stubs
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def handle_response(response)
|
24
|
+
case response.status
|
25
|
+
when 400
|
26
|
+
raise Error, "Your request was malformed."
|
27
|
+
when 404
|
28
|
+
raise Error, "No results were found for your request."
|
29
|
+
when 429
|
30
|
+
raise Error, "Your request exceeded the API rate limit."
|
31
|
+
when 500
|
32
|
+
raise Error, "We were unable to perform the request due to server-side problems."
|
33
|
+
when 503
|
34
|
+
raise Error, "You have been rate limited for sending more than 20 requests per second."
|
35
|
+
end
|
36
|
+
|
37
|
+
response
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleAdvice
|
4
|
+
class Collection
|
5
|
+
attr_reader :data, :total
|
6
|
+
|
7
|
+
def self.from_response(response, key:, type:)
|
8
|
+
body = JSON.parse(response.body)
|
9
|
+
data = body[key]
|
10
|
+
return new(data: [], total: 0) unless data
|
11
|
+
|
12
|
+
new(
|
13
|
+
data: data.map { |attrs| type.new(attrs) },
|
14
|
+
total: body["total_results"].to_i
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(data:, total:)
|
19
|
+
@data = data
|
20
|
+
@total = total
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
|
5
|
+
module SimpleAdvice
|
6
|
+
class Object < OpenStruct
|
7
|
+
def initialize(attributes)
|
8
|
+
super to_struct(attributes)
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_struct(obj)
|
12
|
+
case obj
|
13
|
+
when is_a?(Hash)
|
14
|
+
OpenStruct.new(obj.transform_values(&:to_ostruct))
|
15
|
+
when is_a?(Array)
|
16
|
+
obj.map { |o| to_ostruct(o) }
|
17
|
+
else
|
18
|
+
obj
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "faraday_middleware"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
require_relative "simple_advice/version"
|
8
|
+
|
9
|
+
module SimpleAdvice
|
10
|
+
autoload :Config, "simple_advice/config"
|
11
|
+
autoload :Client, "simple_advice/client"
|
12
|
+
autoload :Collection, "simple_advice/collection"
|
13
|
+
autoload :Error, "simple_advice/error"
|
14
|
+
autoload :Object, "simple_advice/object"
|
15
|
+
|
16
|
+
# Classes used to return a nicer object wrapping the response data
|
17
|
+
autoload :Advice, "simple_advice/objects/advice"
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def random
|
21
|
+
Advice.new JSON.parse(Client.get_request("advice").body)["slip"]
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(advice_id:)
|
25
|
+
advice_obj = JSON.parse(Client.get_request("advice/#{advice_id}").body)["slip"]
|
26
|
+
advice_obj ? Advice.new(advice_obj) : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_by(query:)
|
30
|
+
Collection.from_response(Client.get_request("advice/search/#{query}"), key: "slips", type: Advice)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/simple_advice/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "simple_advice"
|
7
|
+
spec.version = SimpleAdvice::VERSION
|
8
|
+
spec.authors = ["Dmytro Havrysh"]
|
9
|
+
spec.email = ["dmytro.havrysh1@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Implementation of Advice Slip JSON API"
|
12
|
+
spec.description = "Easily get an advice"
|
13
|
+
spec.homepage = "https://github.com/DmytroHavryshGoTo/simple_advice"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "faraday", "~> 1.7"
|
31
|
+
spec.add_dependency "faraday_middleware", "~> 1.1"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_advice
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmytro Havrysh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-04-08 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: '1.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
description: Easily get an advice
|
42
|
+
email:
|
43
|
+
- dmytro.havrysh1@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rubocop.yml"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/simple_advice.rb
|
55
|
+
- lib/simple_advice/client.rb
|
56
|
+
- lib/simple_advice/collection.rb
|
57
|
+
- lib/simple_advice/config.rb
|
58
|
+
- lib/simple_advice/error.rb
|
59
|
+
- lib/simple_advice/object.rb
|
60
|
+
- lib/simple_advice/objects/advice.rb
|
61
|
+
- lib/simple_advice/version.rb
|
62
|
+
- simple_advice.gemspec
|
63
|
+
homepage: https://github.com/DmytroHavryshGoTo/simple_advice
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
homepage_uri: https://github.com/DmytroHavryshGoTo/simple_advice
|
68
|
+
source_code_uri: https://github.com/DmytroHavryshGoTo/simple_advice
|
69
|
+
rubygems_mfa_required: 'true'
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.6.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Implementation of Advice Slip JSON API
|
89
|
+
test_files: []
|