horoshop 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cdf33245189044444a172acf16d7ad51436e9704a5c96753144e034ae750c74b
4
- data.tar.gz: 7cbce9b8132df17979e93d49cad2f8477b1b28a2a3eadc76ed94eb07871d9cfa
3
+ metadata.gz: 04c4a172c11c73f0e5caa19d2314cdc1d3f93b49a812d1255b7ca10a76fdf34d
4
+ data.tar.gz: 1e3f2a461d69956a7e0512eeac5ee669e40df78be7d6d81785c29311fd231550
5
5
  SHA512:
6
- metadata.gz: 8fd9cb5454f4cbec7ee5162ad30a98a8b1ad74a944ba40f60ca83b625ff5e998174f3225c45fd9096e1313c6a90d5b7990d9cdb17deaadb1fce43f98ee206b0b
7
- data.tar.gz: 99616db24c5fc9e74a9eb166c62436c9690e33833ffc2b9d499e958208bde639f426fa0890c7c827011f1a702a5ea675a939b7ad0093347c0a67a97b9ec1cc6d
6
+ metadata.gz: 0d644c98743e064bddc5963192b260a9bea92cc8b75698f8cf617d1c69285348a2d3f871e7ee2d3253c89985a3d0e54c1c8a7907ce617d30500fc64684c6f214
7
+ data.tar.gz: 8b9dcafc84a9a7f5920ff7679008bba22d93aec6586a0b19b1f2efca06c60af23ab08af228ad935532e15cc7ee272efb3be9d04d796e954f13c8b7f9dd18bdf4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Denys Krupenov
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,11 @@
1
+ This is gem used for exchange data with horoshop.ua internet shop constructor
2
+
3
+ Authorization usage, at first you must create instance of class Horoshop with valid params to connect
4
+ After that you just use instance horoshop for all next requests.
5
+
6
+ horoshop = Horoshop::Client.new(url: URI.parse('http://somesite.org'), username: 'jhon', password: 'piterson')
7
+
8
+ Horoshop::Authotization.new(horoshop).authorize
9
+ Horoshop::ImportResidues.new(horoshop).import(hash)
10
+
11
+ *ImportResidues* returns all log data without OK status.
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Horoshop
6
+ # Class for user authorization
7
+ class Authorization < ::Horoshop::Base
8
+ AUTH_URL = '/api/auth/'
9
+ EXPIRATION_TIME = 600
10
+
11
+ def authorize
12
+ response = post(horoshop: horoshop, url: AUTH_URL, body: body)
13
+ return response unless response['status'] == 'OK'
14
+
15
+ horoshop.token = response.dig('response', 'token')
16
+ horoshop.expiration_timestamp = Time.now + EXPIRATION_TIME
17
+ response
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :horoshop
23
+
24
+ def body
25
+ { login: horoshop.login, password: horoshop.password }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Horoshop
6
+ # base class for all methods used in others classes
7
+ class Base
8
+ include Connection
9
+ attr_reader :horoshop
10
+
11
+ # @param {Horoshop::client}
12
+ def initialize(horoshop)
13
+ @horoshop = horoshop
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ module Horoshop
5
+ # Module for check connection
6
+ module Connection
7
+ ERROR = { 'status' => 'HTTP_ERROR', 'message' => 'UNKNOWN SERVER ERROR' }.freeze
8
+
9
+ def post(horoshop:, url:, body:, add_token: false)
10
+ mixin_token!(horoshop, body) if add_token
11
+ connection(horoshop).post(url, body).body
12
+ rescue Faraday::Error
13
+ ERROR
14
+ end
15
+
16
+ def connection(horoshop)
17
+ @connection ||= Faraday.new(url: horoshop.url) do |faraday|
18
+ faraday.request :json
19
+ faraday.response :raise_error
20
+ faraday.response :json
21
+ faraday.adapter Faraday.default_adapter
22
+ end
23
+ end
24
+
25
+ def mixin_token!(horoshop, body)
26
+ horoshop.refresh_token! unless horoshop.token_valid?
27
+ body[:token] = horoshop.token
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Horoshop
4
+ # import_residues to the horoshop
5
+ class ImportResiduences < ::Horoshop::Base
6
+ URL = 'api/catalog/importResidues/'
7
+ STATUS_OK = 'OK'
8
+
9
+ # @param {Hash}
10
+ def call(body)
11
+ body = post(horoshop: horoshop, url: URL, body: body, add_token: true)
12
+ parse_response(body)
13
+ end
14
+
15
+ private
16
+
17
+ def parse_response(body)
18
+ log = body.dig('response', 'log')
19
+ return [] unless log
20
+
21
+ log.filter { |el| el['status'] != STATUS_OK }
22
+ end
23
+ end
24
+ end
data/lib/horoshop.rb CHANGED
@@ -1,26 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'horoshop/base'
4
+ require_relative 'horoshop/connection'
3
5
  require_relative 'horoshop/authorization'
6
+ require_relative 'horoshop/import_residues'
4
7
 
5
- # Base class used to store data about authentication
6
- class Horoshop
7
- attr_accessor :url, :login, :password, :token, :expiration_timestamp
8
+ module Horoshop
9
+ # Base class used to store data about authentication
10
+ class Client
11
+ attr_accessor :url, :login, :password, :token, :expiration_timestamp
8
12
 
9
- def initialize(url:, login:, password:)
10
- @url = url
11
- @login = login
12
- @password = password
13
- @token = nil
14
- @expiration_timestamp = nil
15
- end
13
+ # @param {URI} url
14
+ # @param {String} login
15
+ # @param {String}
16
+ def initialize(url:, login:, password:)
17
+ @url = url
18
+ @login = login
19
+ @password = password
20
+ @token = nil
21
+ @expiration_timestamp = nil
22
+ end
16
23
 
17
- def token_valid?
18
- return false if token.nil?
24
+ def token_valid?
25
+ return false if token.nil?
19
26
 
20
- expiration_timestamp < Time.now
21
- end
27
+ expiration_timestamp < Time.now
28
+ end
22
29
 
23
- def refresh_token!
24
- Horoshop::Authorization.new(self).authorize
30
+ def refresh_token!
31
+ Horoshop::Authorization.new(self).authorize
32
+ end
25
33
  end
26
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: horoshop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denys Krupenov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-02-26 00:00:00.000000000 Z
12
+ date: 2024-02-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gem for exchangedata with the internet shop constructor
15
15
  email: dkru84@gmail.com
@@ -17,7 +17,13 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - LICENSE
21
+ - README.md
20
22
  - lib/horoshop.rb
23
+ - lib/horoshop/authorization.rb
24
+ - lib/horoshop/base.rb
25
+ - lib/horoshop/connection.rb
26
+ - lib/horoshop/import_residues.rb
21
27
  homepage: https://github.com/dkru/horoshop
22
28
  licenses:
23
29
  - MIT
@@ -27,7 +33,6 @@ post_install_message:
27
33
  rdoc_options: []
28
34
  require_paths:
29
35
  - lib
30
- - spec/external_adapters
31
36
  required_ruby_version: !ruby/object:Gem::Requirement
32
37
  requirements:
33
38
  - - ">="