yar 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac226a0af6b4c8761daa6f7814417fcf12ff2548604e49ba53d398a9791c62f1
4
- data.tar.gz: 7d3b2293ab13f4abcb10f466083b18265f6cecefad12b8cf77dfa17900827538
3
+ metadata.gz: c1cac2e98c7750bbdcdd306332845964e801cb1f7eff12ff6a8450685921df07
4
+ data.tar.gz: 6a9e04d28248e37357f7c0c251426dcda1b206bb3b366320ffc230addf8f1367
5
5
  SHA512:
6
- metadata.gz: 5c47e561f76733d7304f6bc0d37e612abf69f185e1e78923afed4bfd3e9f69b0afc7945175468aa3fe7140b5864cd316fa3f6f1af756ceef88d408b60ff27edb
7
- data.tar.gz: 49ae082c99afe2e21e1075124d3018b8b1575a9bf0d279c260e577e540e4e825555ef9c3e56414e258e09def824fb1779120cd9f8ee21fe6e3bbb3e343e8aaaf
6
+ metadata.gz: 6626d1a76d10ac0b7ac3d4f94bd2f814706eb568478c892aae3921d717588f00108a0cb1ea2af22c7b06f3c7d190e0a73767e7e6ce06cb8ccb25eb92fc34c94d
7
+ data.tar.gz: ea195e9c03006172bac11c7f664ea7c2c674fe4b2a56d1a2e77a5558b53d6a34fc7154fd82e2fdd024f56c81b87e7c069212645af92e63ccdac36b59b51f8fbc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yar (1.0.1)
4
+ yar (1.0.2)
5
5
  faraday (~> 1.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # yar
1
+ # yar [![Ruby specs](https://github.com/ruby-api-client/yar/actions/workflows/ci.yml/badge.svg)](https://github.com/ruby-api-client/yar/actions/workflows/ci.yml) [![Gem Version](https://badge.fury.io/rb/yar.svg)](https://badge.fury.io/rb/yar) ![GitHub](https://img.shields.io/github/license/ruby-api-client/yar) [![Coverage Status](https://coveralls.io/repos/github/ruby-api-client/yar/badge.svg?branch=main)](https://coveralls.io/github/ruby-api-client/yar?branch=main) ![Gem](https://img.shields.io/gem/dt/yar)
2
2
 
3
3
  Yandex.Rasp API - API Яндекс.Расписаний
4
4
  docs: <https://yandex.ru/dev/rasp/doc/concepts/about.html>
data/lib/Yar/version.rb CHANGED
@@ -3,6 +3,6 @@
3
3
  module Yar
4
4
  major = 1
5
5
  minor = 0
6
- patch = 1
6
+ patch = 2
7
7
  VERSION = "#{major}.#{minor}.#{patch}"
8
8
  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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class Error < StandardError; end
5
+ end
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class Carrier < Object; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class City < Object; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class Copyright < Object; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class Schedule < Object; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class Station < Object; end
5
+ end
@@ -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,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class CarrierResource < Resource
5
+ def info(code:, system: "yandex")
6
+ Carrier.new get("carrier/?code=#{code}&system=#{system}").body
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class CopyrightResource < Resource
5
+ def info
6
+ Copyright.new get("copyright/").body
7
+ end
8
+ end
9
+ 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
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ class StationsResource < Resource
5
+ # def list
6
+ # resp = get("stations_list/")
7
+ # Collection.from_response(resp, key: "countries", type: Region)
8
+ # end
9
+ def list
10
+ Object.new get("stations_list/").body
11
+ end
12
+
13
+ # TODO: move to Schedule.info station: -> Schedule
14
+ def schedule(station:)
15
+ Schedule.new get("schedule/?station=#{station}").body
16
+ end
17
+
18
+ # TODO: move to Schedule.between(from: to:) -> Schedule
19
+ def between(from:, to:)
20
+ Schedule.new get("search/?from=#{from}&to=#{to}").body
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yar
4
+ major = 1
5
+ minor = 0
6
+ patch = 2
7
+ VERSION = "#{major}.#{minor}.#{patch}"
8
+ end
data/spec/client_spec.rb CHANGED
@@ -14,6 +14,6 @@ RSpec.describe Yar::Client do
14
14
  end
15
15
 
16
16
  describe "VERSION" do
17
- it { expect(Yar::VERSION).to eq "1.0.1" }
17
+ it { expect(Yar::VERSION).to eq "1.0.2" }
18
18
  end
19
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Brin
@@ -161,6 +161,22 @@ files:
161
161
  - lib/Yar/resources/stations.rb
162
162
  - lib/Yar/version.rb
163
163
  - lib/yar.rb
164
+ - lib/yar/client.rb
165
+ - lib/yar/collection.rb
166
+ - lib/yar/error.rb
167
+ - lib/yar/object.rb
168
+ - lib/yar/objects/carrier.rb
169
+ - lib/yar/objects/city.rb
170
+ - lib/yar/objects/copyright.rb
171
+ - lib/yar/objects/schedule.rb
172
+ - lib/yar/objects/station.rb
173
+ - lib/yar/resource.rb
174
+ - lib/yar/resources/carrier.rb
175
+ - lib/yar/resources/copyright.rb
176
+ - lib/yar/resources/nearest.rb
177
+ - lib/yar/resources/schedule.rb
178
+ - lib/yar/resources/stations.rb
179
+ - lib/yar/version.rb
164
180
  - spec/client_spec.rb
165
181
  - spec/lib/resources/carrier_spec.rb
166
182
  - spec/lib/resources/copyright_spec.rb