yar 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +61 -0
- data/.rspec +5 -0
- data/.rubocop.yml +142 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +91 -0
- data/LICENSE +21 -0
- data/README.md +45 -0
- data/Rakefile +16 -0
- data/lib/Yar/client.rb +52 -0
- data/lib/Yar/collection.rb +22 -0
- data/lib/Yar/error.rb +5 -0
- data/lib/Yar/object.rb +37 -0
- data/lib/Yar/objects/carrier.rb +5 -0
- data/lib/Yar/objects/city.rb +5 -0
- data/lib/Yar/objects/copyright.rb +5 -0
- data/lib/Yar/objects/schedule.rb +5 -0
- data/lib/Yar/objects/station.rb +5 -0
- data/lib/Yar/resource.rb +61 -0
- data/lib/Yar/resources/carrier.rb +9 -0
- data/lib/Yar/resources/copyright.rb +9 -0
- data/lib/Yar/resources/nearest.rb +14 -0
- data/lib/Yar/resources/schedule.rb +15 -0
- data/lib/Yar/resources/stations.rb +23 -0
- data/lib/Yar/version.rb +8 -0
- data/lib/yar.rb +27 -0
- data/spec/client_spec.rb +19 -0
- data/spec/lib/resources/carrier_spec.rb +16 -0
- data/spec/lib/resources/copyright_spec.rb +16 -0
- data/spec/lib/resources/nearest_spec.rb +30 -0
- data/spec/lib/resources/schedule_spec.rb +26 -0
- data/spec/lib/resources/stations_spec.rb +26 -0
- data/spec/spec_helper.rb +60 -0
- data/spec/vcr/carrier/schedule.yml +44 -0
- data/spec/vcr/copyright/info.yml +51 -0
- data/spec/vcr/nearest/city.yml +39 -0
- data/spec/vcr/nearest/stations.yml +139 -0
- data/spec/vcr/schedule/between.yml +130 -0
- data/spec/vcr/schedule/schedule.yml +95 -0
- data/spec/vcr/stations/between.yml +130 -0
- data/spec/vcr/stations/schedule.yml +95 -0
- data/yar.gemspec +36 -0
- metadata +236 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2bccc44a614eb9a950752e62a467766e2c133ea6f1f28e69f38f1145b2fa8bc7
|
4
|
+
data.tar.gz: 2b226cc2d442e9aa6c70c13c63d7a6af0005d9e1ff624f026e9a60e29e5f80a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9af341e0634d5fce627367b5a2ef6b009af7ab52e86daa81ffd474abe7eaefedcdf25da1afc27320dd87ebb55472b12cc4511466e1dda90f3594783eec3bfe9
|
7
|
+
data.tar.gz: 5344ebe87f967c3730e0651d88293df63d8b7ed210c45e4fb0809cca74733572cb9eeeb3eb078b5b610a9e8ff55b460183a69e7aa99e2d40db214f691db3360e
|
data/.gitignore
ADDED
@@ -0,0 +1,61 @@
|
|
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
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
56
|
+
# .rubocop-https?--*
|
57
|
+
|
58
|
+
# macOS
|
59
|
+
.DS_Store
|
60
|
+
dev
|
61
|
+
.rspec_status
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
---
|
2
|
+
AllCops:
|
3
|
+
NewCops: disable # TODO: change to enable after refactoring
|
4
|
+
|
5
|
+
# Commonly used screens these days easily fit more than 80 characters.
|
6
|
+
Layout/LineLength:
|
7
|
+
Max: 120
|
8
|
+
|
9
|
+
# Too short methods lead to extraction of single-use methods, which can make
|
10
|
+
# the code easier to read (by naming things), but can also clutter the class
|
11
|
+
Metrics/MethodLength:
|
12
|
+
Max: 20
|
13
|
+
|
14
|
+
# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
|
15
|
+
Metrics/ClassLength:
|
16
|
+
Max: 1500
|
17
|
+
|
18
|
+
# No space makes the method definition shorter and differentiates
|
19
|
+
# from a regular assignment.
|
20
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
21
|
+
EnforcedStyle: no_space
|
22
|
+
|
23
|
+
# Single quotes being faster is hardly measurable and only affects parse time.
|
24
|
+
# Enforcing double quotes reduces the times where you need to change them
|
25
|
+
# when introducing an interpolation. Use single quotes only if their semantics
|
26
|
+
# are needed.
|
27
|
+
Style/StringLiterals:
|
28
|
+
EnforcedStyle: double_quotes
|
29
|
+
|
30
|
+
# We do not need to support Ruby 1.9, so this is good to use.
|
31
|
+
Style/SymbolArray:
|
32
|
+
Enabled: true
|
33
|
+
|
34
|
+
# Most readable form.
|
35
|
+
Style/OptionHash:
|
36
|
+
# A list of parameter names that will be flagged by this cop.
|
37
|
+
SuspiciousParamNames:
|
38
|
+
- options
|
39
|
+
- opts
|
40
|
+
- args
|
41
|
+
- paramsgit sttus
|
42
|
+
- parameters
|
43
|
+
|
44
|
+
# Mixing the styles looks just silly.
|
45
|
+
Style/HashSyntax:
|
46
|
+
EnforcedStyle: ruby19_no_mixed_keys
|
47
|
+
|
48
|
+
# has_key? and has_value? are far more readable than key? and value?
|
49
|
+
Style/PreferredHashMethods:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
# String#% is by far the least verbose and only object oriented variant.
|
53
|
+
Style/FormatString:
|
54
|
+
EnforcedStyle: percent
|
55
|
+
|
56
|
+
Style/CollectionMethods:
|
57
|
+
Enabled: true
|
58
|
+
PreferredMethods:
|
59
|
+
# inject seems more common in the community.
|
60
|
+
reduce: "inject"
|
61
|
+
|
62
|
+
|
63
|
+
# Either allow this style or don't. Marking it as safe with parenthesis
|
64
|
+
# is silly. Let's try to live without them for now.
|
65
|
+
Style/ParenthesesAroundCondition:
|
66
|
+
AllowSafeAssignment: false
|
67
|
+
Lint/AssignmentInCondition:
|
68
|
+
AllowSafeAssignment: false
|
69
|
+
|
70
|
+
# A specialized exception class will take one or more arguments and construct the message from it.
|
71
|
+
# So both variants make sense.
|
72
|
+
Style/RaiseArgs:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
# Indenting the chained dots beneath each other is not supported by this cop,
|
76
|
+
# see https://github.com/bbatsov/rubocop/issues/1633
|
77
|
+
Layout/MultilineOperationIndentation:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
# Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain.
|
81
|
+
# The argument that fail should be used to abort the program is wrong too,
|
82
|
+
# there's Kernel#abort for that.
|
83
|
+
Style/SignalException:
|
84
|
+
EnforcedStyle: only_raise
|
85
|
+
|
86
|
+
# Suppressing exceptions can be perfectly fine, and be it to avoid to
|
87
|
+
# explicitly type nil into the rescue since that's what you want to return,
|
88
|
+
# or suppressing LoadError for optional dependencies
|
89
|
+
Lint/SuppressedException:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Layout/SpaceInsideBlockBraces:
|
93
|
+
# The space here provides no real gain in readability while consuming
|
94
|
+
# horizontal space that could be used for a better parameter name.
|
95
|
+
# Also {| differentiates better from a hash than { | does.
|
96
|
+
SpaceBeforeBlockParameters: false
|
97
|
+
|
98
|
+
# No trailing space differentiates better from the block:
|
99
|
+
# foo} means hash, foo } means block.
|
100
|
+
Layout/SpaceInsideHashLiteralBraces:
|
101
|
+
EnforcedStyle: no_space
|
102
|
+
|
103
|
+
# { ... } for multi-line blocks is okay, follow Weirichs rule instead:
|
104
|
+
# https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
|
105
|
+
Style/BlockDelimiters:
|
106
|
+
Enabled: false
|
107
|
+
|
108
|
+
# do / end blocks should be used for side effects,
|
109
|
+
# methods that run a block for side effects and have
|
110
|
+
# a useful return value are rare, assign the return
|
111
|
+
# value to a local variable for those cases.
|
112
|
+
Style/MethodCalledOnDoEndBlock:
|
113
|
+
Enabled: true
|
114
|
+
|
115
|
+
# Enforcing the names of variables? To single letter ones? Just no.
|
116
|
+
Style/SingleLineBlockParams:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
# Shadowing outer local variables with block parameters is often useful
|
120
|
+
# to not reinvent a new name for the same thing, it highlights the relation
|
121
|
+
# between the outer variable and the parameter. The cases where it's actually
|
122
|
+
# confusing are rare, and usually bad for other reasons already, for example
|
123
|
+
# because the method is too long.
|
124
|
+
Lint/ShadowingOuterLocalVariable:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
# Check with yard instead.
|
128
|
+
Style/Documentation:
|
129
|
+
Enabled: false
|
130
|
+
|
131
|
+
# This is just silly. Calling the argument `other` in all cases makes no sense.
|
132
|
+
Naming/BinaryOperatorParameterName:
|
133
|
+
Enabled: false
|
134
|
+
|
135
|
+
# There are valid cases, for example debugging Cucumber steps,
|
136
|
+
# also they'll fail CI anyway
|
137
|
+
Lint/Debugger:
|
138
|
+
Enabled: false
|
139
|
+
|
140
|
+
# Style preference
|
141
|
+
Style/MethodDefParentheses:
|
142
|
+
Enabled: false
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yar (1.0.0)
|
5
|
+
addressable (~> 2.3, >= 2.3.7)
|
6
|
+
faraday (~> 1.7)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.8.1)
|
12
|
+
public_suffix (>= 2.0.2, < 6.0)
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
diff-lcs (1.5.0)
|
16
|
+
docile (1.4.0)
|
17
|
+
faraday (1.10.2)
|
18
|
+
faraday-em_http (~> 1.0)
|
19
|
+
faraday-em_synchrony (~> 1.0)
|
20
|
+
faraday-excon (~> 1.1)
|
21
|
+
faraday-httpclient (~> 1.0)
|
22
|
+
faraday-multipart (~> 1.0)
|
23
|
+
faraday-net_http (~> 1.0)
|
24
|
+
faraday-net_http_persistent (~> 1.0)
|
25
|
+
faraday-patron (~> 1.0)
|
26
|
+
faraday-rack (~> 1.0)
|
27
|
+
faraday-retry (~> 1.0)
|
28
|
+
ruby2_keywords (>= 0.0.4)
|
29
|
+
faraday-em_http (1.0.0)
|
30
|
+
faraday-em_synchrony (1.0.0)
|
31
|
+
faraday-excon (1.1.0)
|
32
|
+
faraday-gzip (0.1.0)
|
33
|
+
faraday (>= 1.0)
|
34
|
+
zlib (~> 2.1)
|
35
|
+
faraday-httpclient (1.0.1)
|
36
|
+
faraday-multipart (1.0.4)
|
37
|
+
multipart-post (~> 2)
|
38
|
+
faraday-net_http (1.0.1)
|
39
|
+
faraday-net_http_persistent (1.2.0)
|
40
|
+
faraday-patron (1.0.0)
|
41
|
+
faraday-rack (1.0.0)
|
42
|
+
faraday-retry (1.0.3)
|
43
|
+
hashdiff (1.0.1)
|
44
|
+
multipart-post (2.2.3)
|
45
|
+
public_suffix (5.0.0)
|
46
|
+
rake (12.3.3)
|
47
|
+
rexml (3.2.5)
|
48
|
+
rspec (3.11.0)
|
49
|
+
rspec-core (~> 3.11.0)
|
50
|
+
rspec-expectations (~> 3.11.0)
|
51
|
+
rspec-mocks (~> 3.11.0)
|
52
|
+
rspec-core (3.11.0)
|
53
|
+
rspec-support (~> 3.11.0)
|
54
|
+
rspec-expectations (3.11.1)
|
55
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
56
|
+
rspec-support (~> 3.11.0)
|
57
|
+
rspec-mocks (3.11.1)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.11.0)
|
60
|
+
rspec-support (3.11.1)
|
61
|
+
ruby2_keywords (0.0.5)
|
62
|
+
simplecov (0.21.2)
|
63
|
+
docile (~> 1.1)
|
64
|
+
simplecov-html (~> 0.11)
|
65
|
+
simplecov_json_formatter (~> 0.1)
|
66
|
+
simplecov-html (0.12.3)
|
67
|
+
simplecov-lcov (0.7.0)
|
68
|
+
simplecov_json_formatter (0.1.4)
|
69
|
+
vcr (6.1.0)
|
70
|
+
webmock (3.18.1)
|
71
|
+
addressable (>= 2.8.0)
|
72
|
+
crack (>= 0.3.2)
|
73
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
74
|
+
zlib (2.1.1)
|
75
|
+
|
76
|
+
PLATFORMS
|
77
|
+
x86_64-darwin-17
|
78
|
+
|
79
|
+
DEPENDENCIES
|
80
|
+
bundler
|
81
|
+
faraday-gzip (~> 0.1.0)
|
82
|
+
rake (~> 12.3.3)
|
83
|
+
rspec (~> 3.0)
|
84
|
+
simplecov (~> 0.9)
|
85
|
+
simplecov-lcov (~> 0.7.0)
|
86
|
+
vcr (~> 6.1)
|
87
|
+
webmock (~> 3.18, >= 3.18.1)
|
88
|
+
yar!
|
89
|
+
|
90
|
+
BUNDLED WITH
|
91
|
+
2.3.22
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Ruby API clients
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# yar
|
2
|
+
|
3
|
+
Yandex.Rasp API - API Яндекс.Расписаний
|
4
|
+
docs: <https://yandex.ru/dev/rasp/doc/concepts/about.html>
|
5
|
+
gem: <https://rubygems.org/gems/yar>
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
```sh
|
10
|
+
gem install yar
|
11
|
+
```
|
12
|
+
|
13
|
+
## Example
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
|
17
|
+
require "yar"
|
18
|
+
|
19
|
+
client = Yar::Client.new(token: ENV["YAR_ACCESS_TOKEN"])
|
20
|
+
|
21
|
+
# Копирайт Яндекс.Расписаний
|
22
|
+
client.copyright.info
|
23
|
+
|
24
|
+
# Список всех доступных станций
|
25
|
+
client.stations.list
|
26
|
+
|
27
|
+
# Список станций следования
|
28
|
+
client.stations.route(uid:)
|
29
|
+
|
30
|
+
# Список ближайших станций
|
31
|
+
client.nearest.station(lat:, long:, distance:)
|
32
|
+
|
33
|
+
# Ближайший город
|
34
|
+
client.nearest.city(lat:, long:)
|
35
|
+
|
36
|
+
# Расписание рейсов по станции
|
37
|
+
client.schedule.info(station:)
|
38
|
+
|
39
|
+
# Расписание рейсов между станциями
|
40
|
+
client.schedule.between(from:, to:)
|
41
|
+
|
42
|
+
# Информация о перевозчике
|
43
|
+
client.carrier.info(code:)
|
44
|
+
|
45
|
+
```
|
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
|
+
task default: :test
|
13
|
+
|
14
|
+
task :console do
|
15
|
+
exec "irb -I lib -r yar.rb"
|
16
|
+
end
|
data/lib/Yar/client.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yar
|
4
|
+
class Client
|
5
|
+
BASE_URL = "https://api.rasp.yandex.net/v3.0/"
|
6
|
+
|
7
|
+
attr_reader :token, :adapter, :format
|
8
|
+
|
9
|
+
# new client
|
10
|
+
def initialize(token:, adapter: Faraday.default_adapter, stubs: nil, format: "json")
|
11
|
+
@token = token
|
12
|
+
@adapter = adapter
|
13
|
+
@stubs = stubs
|
14
|
+
@format = format
|
15
|
+
end
|
16
|
+
|
17
|
+
def copyright
|
18
|
+
CopyrightResource.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def stations
|
22
|
+
StationsResource.new(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def carrier
|
26
|
+
CarrierResource.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def nearest
|
30
|
+
NearestResource.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def schedule
|
34
|
+
ScheduleResource.new(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
def connection
|
38
|
+
@connection ||= Faraday.new(BASE_URL) do |conn|
|
39
|
+
conn.request :authorization, nil, token
|
40
|
+
conn.request :json
|
41
|
+
conn.request :url_encoded
|
42
|
+
|
43
|
+
# conn.params[:format] = @format
|
44
|
+
|
45
|
+
conn.response :json, content_type: "application/json"
|
46
|
+
conn.response :xml, content_type: "application/xml" if @format == "xml"
|
47
|
+
|
48
|
+
conn.adapter adapter, @stubs
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yar
|
4
|
+
class Collection
|
5
|
+
attr_reader :data, :items, :total
|
6
|
+
|
7
|
+
def self.from_response(response, key:, type:)
|
8
|
+
body = response.body
|
9
|
+
new(
|
10
|
+
data: body[key].map {|attrs| type.new(attrs) },
|
11
|
+
items: body["items"],
|
12
|
+
total: body["total"]
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(data:, items:, total:)
|
17
|
+
@data = data
|
18
|
+
@items = items
|
19
|
+
@total = total
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/Yar/error.rb
ADDED
data/lib/Yar/object.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
|
5
|
+
module Yar
|
6
|
+
# TODO: change to Struct
|
7
|
+
class Object < OpenStruct
|
8
|
+
def initialize(attributes)
|
9
|
+
super to_ostruct(attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Convert Array or Hash to OpenStruct
|
13
|
+
def to_ostruct(obj)
|
14
|
+
case obj
|
15
|
+
when Hash
|
16
|
+
# obj.to_struct
|
17
|
+
# rubocop:disable Style/HashTransformValues
|
18
|
+
OpenStruct.new(obj.map {|key, val| [key, to_ostruct(val)] }.to_h)
|
19
|
+
# rubocop:enable Style/HashTransformValues
|
20
|
+
when Array
|
21
|
+
obj.map {|o| to_ostruct(o) }
|
22
|
+
else
|
23
|
+
obj
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# class Hash
|
29
|
+
# def to_struct
|
30
|
+
# s = Struct.new(*self.keys.map(&:to_sym))
|
31
|
+
# construct = map do |k,v|
|
32
|
+
# v.is_a?(Hash) ? v.to_struct : v.is_a?(Array) ? v.join(", ") : v
|
33
|
+
# end
|
34
|
+
# s.new(*construct)
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
end
|
data/lib/Yar/resource.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yar
|
4
|
+
class Resource
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def get(url, params: {}, headers: {})
|
14
|
+
handle_response client.connection.get(url, params, headers)
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(url, body:, headers: {})
|
18
|
+
handle_response client.connection.post(url, body, headers)
|
19
|
+
end
|
20
|
+
|
21
|
+
def patch(url, body:, headers: {})
|
22
|
+
handle_response client.connection.patch(url, body, headers)
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(url, body:, headers: {})
|
26
|
+
handle_response client.connection.put(url, body, headers)
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete(url, params: {}, headers: {})
|
30
|
+
handle_response client.connection.delete(url, params, headers)
|
31
|
+
end
|
32
|
+
alias delete_request delete
|
33
|
+
|
34
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
35
|
+
# rubocop:disable Metrics/AbcSize
|
36
|
+
def handle_response(response)
|
37
|
+
case response.status
|
38
|
+
when 302
|
39
|
+
raise Error, "Redirect found. #{response.body['error']}"
|
40
|
+
when 400
|
41
|
+
raise Error, "Your request was malformed. #{response.body['error']}"
|
42
|
+
when 401
|
43
|
+
raise Error, "You did not supply valid authentication credentials. #{response.body['error']}"
|
44
|
+
when 403
|
45
|
+
raise Error, "You are not allowed to perform that action. #{response.body['error']}"
|
46
|
+
when 404
|
47
|
+
raise Error, "No results were found for your request. #{response.body['error']}"
|
48
|
+
when 429
|
49
|
+
raise Error, "Your request exceeded the API rate limit. #{response.body['error']}"
|
50
|
+
when 500
|
51
|
+
raise Error, "We were unable to perform the request due to server-side problems. #{response.body['error']}"
|
52
|
+
when 503
|
53
|
+
raise Error, "You have been rate limited for sending more requests per second. #{response.body['error']}"
|
54
|
+
end
|
55
|
+
|
56
|
+
response
|
57
|
+
end
|
58
|
+
# rubocop:enable Metrics/AbcSize
|
59
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yar
|
4
|
+
class NearestResource < Resource
|
5
|
+
def stations(lat:, long:, distance:)
|
6
|
+
resp = get("nearest_stations/?lat=#{lat}&lng=#{long}&distance=#{distance}")
|
7
|
+
Collection.from_response(resp, key: "stations", type: Station)
|
8
|
+
end
|
9
|
+
|
10
|
+
def city(lat:, long:)
|
11
|
+
City.new get("nearest_settlement/?lat=#{lat}&lng=#{long}").body
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yar
|
4
|
+
class ScheduleResource < Resource
|
5
|
+
# TODO: move to Schedule.info station: -> Schedule
|
6
|
+
def info(station:)
|
7
|
+
Schedule.new get("schedule/?station=#{station}").body
|
8
|
+
end
|
9
|
+
|
10
|
+
# TODO: move to Schedule.between(from: to:) -> Schedule
|
11
|
+
def between(from:, to:)
|
12
|
+
Schedule.new get("search/?from=#{from}&to=#{to}").body
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|