solidus_client 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 656255324a849b6b35ce7a3b738a7d07e19a8886548d3f001e53a747a9c6d4de
4
+ data.tar.gz: c8929ce73909ecbcf9749bb9ddeb18d4de06033d452c979fb85aeac0b196d11e
5
+ SHA512:
6
+ metadata.gz: 3d95074fc2ae8bd9a076c5721a8c5bd7d52ab7c3166dbf9dc057b010006f5b713441166765f043b64dd064d347c2ff85d26d9e1a52010af1010a363393e626c1
7
+ data.tar.gz: 2588ccf5a07b2858a36ad16b61681a16edfb420d6ba203c23545cfb1665975dd936d832847a197c81eb4dac1d4b4f4572e9ec5fcaba2e1922ac3a82d062c0a93
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Nebulab S.r.l. (https://nebulab.it)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Ruby Solidus API client
2
+
3
+ ## Requirements
4
+ * [MRI 2.3+](https://www.ruby-lang.org/en/downloads)
5
+ * [Solidus 2.9+](https://solidus.io) with [Solidus Auth (Devise)](https://github.com/solidusio/solidus_auth_devise)
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ gem install solidus_client
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Client class
16
+
17
+ Use `SolidusClient:Client` class
18
+ ```ruby
19
+ require 'solidus_client/client'
20
+
21
+ client = SolidusClient::Client.new(url: 'app url', api_key: 'user api key')
22
+
23
+ item_data_hash = { ... }
24
+ response_hash = client.add_item(order_number, item_data_hash)
25
+ ```
26
+
27
+ ### Command line
28
+ Run `solidus` command
29
+
30
+ ```
31
+ $ solidus -h
32
+
33
+ Usage: solidus [options] command [target] [JSON data]
34
+
35
+ Commands:
36
+ - add_item <order_number> <data>
37
+ - ..
38
+
39
+ Options:
40
+ -u, --url Solidus URL
41
+ -k, --key Solidus API key
42
+ ```
43
+
44
+ ## Development
45
+ Install dependencies
46
+ ```
47
+ bundle install
48
+ ```
49
+
50
+ Export required environment Variables
51
+ ```
52
+ export SOLIDUS_API_KEY=your API key
53
+ export SOLIDUS_URL=Solidus endpoint
54
+ ```
55
+
56
+ Run specs
57
+ **WARNING:** `checkout_spec.rb` requires a running Solidus instance and writes Product and Order data to it
58
+ ```
59
+ bundle exec rake spec
60
+ ```
61
+
62
+ ## License
63
+
64
+ Copyright © 2019 [Nebulab](https://nebulab.it/).
65
+ It is free software, and may be redistributed under the terms specified in the [license](LICENSE.txt).
66
+
67
+ ## About
68
+
69
+ ![Nebulab](http://nebulab.it/assets/images/public/logo.svg)
70
+
71
+ Solidus API Client is funded and maintained by the [Nebulab](http://nebulab.it/) team.
72
+
73
+ We firmly believe in the power of open-source. [Contact us](https://nebulab.it/contact-us/) if you like our work and you need help with your project design or development.
data/exe/solidus ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
6
+
7
+ require 'solidus_client/cli'
8
+
9
+ STDOUT.sync = true
10
+
11
+ SolidusClient::CLI.new.run
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'solidus_client/client'
4
+ require_relative 'solidus_client/version'
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_client/client'
4
+ require 'solidus_client/version'
5
+ require 'json'
6
+ require 'optparse'
7
+
8
+ module SolidusClient
9
+ # The class responsible of handling command line logic
10
+ class CLI
11
+ # Entry point to start the application
12
+ def run
13
+ client = Client.new(parse_options)
14
+ command = ARGV.shift.to_sym
15
+ args = convert_data_input
16
+
17
+ result = client.send(command, *args)
18
+ pp result
19
+ rescue OptionParser::InvalidOption => e
20
+ abort("#{e.message}, see 'solidus --help'")
21
+ end
22
+
23
+ private
24
+
25
+ def parse_options
26
+ options = {}
27
+ OptionParser.new do |opts|
28
+ opts.version = SolidusClient::Version::STRING
29
+ opts_banner(opts)
30
+ opts_url(opts, options)
31
+ opts_key(opts, options)
32
+ end.parse!
33
+
34
+ options
35
+ end
36
+
37
+ def opts_banner(opts)
38
+ opts.banner = 'Usage: solidus [options] command [target] [JSON data]'
39
+ opts.separator('')
40
+ opts.separator("Commands:\n - #{commands_descriptions.join("\n - ")}")
41
+ opts.separator('')
42
+ opts.separator('Options:')
43
+ end
44
+
45
+ def opts_url(opts, options)
46
+ opts.on('-u', '--url [STRING]', 'Solidus URL') do |value|
47
+ options[:url] = value
48
+ end
49
+ end
50
+
51
+ def opts_key(opts, options)
52
+ opts.on('-k', '--key [STRING]', 'Solidus API key') do |value|
53
+ options[:api_key] = value
54
+ end
55
+ end
56
+
57
+ def commands_descriptions
58
+ Client.public_instance_methods(false).sort.map do |method|
59
+ params = Client.instance_method(method).parameters
60
+ "#{method} #{params_description(params)}"
61
+ end
62
+ end
63
+
64
+ def params_description(params)
65
+ params.map { |param| "<#{param[1]}>" }.join(' ')
66
+ end
67
+
68
+ def convert_data_input
69
+ ARGV.map do |arg|
70
+ begin
71
+ JSON.parse(arg, symbolize_names: true)
72
+ rescue JSON::ParserError
73
+ arg
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'pp'
6
+
7
+ module SolidusClient
8
+ # Solidus API Client entrypoint
9
+ class Client
10
+ def initialize(url: nil, api_key: nil)
11
+ url ||= ENV['SOLIDUS_URL']
12
+ api_key ||= ENV['SOLIDUS_API_KEY']
13
+
14
+ @connection = Faraday.new(url: url + '/api') do |connection|
15
+ connection.authorization(:Bearer, api_key)
16
+ connection.use Faraday::Response::RaiseError
17
+ connection.headers['Content-Type'] = 'application/json'
18
+ connection.request(:json)
19
+ connection.response(:json, parser_options: { symbolize_names: true })
20
+ connection.adapter Faraday.default_adapter
21
+ end
22
+ end
23
+
24
+ def states(country_id)
25
+ get("countries/#{country_id}/states")
26
+ end
27
+
28
+ def shipping_zones
29
+ get('zones')
30
+ end
31
+
32
+ def products
33
+ get('products')
34
+ end
35
+
36
+ def create_product(data = {})
37
+ post('products', data)
38
+ end
39
+
40
+ def create_order(data = {})
41
+ post('orders', data)
42
+ end
43
+
44
+ def add_item(order_number, data = {})
45
+ post("orders/#{order_number}/line_items", data)
46
+ end
47
+
48
+ def remove_item(order_number, item_id)
49
+ delete("orders/#{order_number}/line_items/#{item_id}")
50
+ end
51
+
52
+ def advance_checkout(order_number)
53
+ put("checkouts/#{order_number}/next")
54
+ end
55
+
56
+ def complete_checkout(order_number)
57
+ put("checkouts/#{order_number}/complete")
58
+ end
59
+
60
+ def update_order(order_number, data)
61
+ patch("orders/#{order_number}", data)
62
+ end
63
+
64
+ def shipping_rates(shipment_number)
65
+ get("shipments/#{shipment_number}/estimated_rates")
66
+ end
67
+
68
+ def payments
69
+ get('products')
70
+ end
71
+
72
+ def select_shipping_method(shipment_number, data)
73
+ put("shipments/#{shipment_number}/select_shipping_method", data)
74
+ end
75
+
76
+ def enter_payment_details(order_number, data)
77
+ post("orders/#{order_number}/payments", data)
78
+ end
79
+
80
+ private
81
+
82
+ def get(path)
83
+ request(:get, path)
84
+ end
85
+
86
+ def post(path, data = {})
87
+ request(:post, path, data)
88
+ end
89
+
90
+ def put(path, data = {})
91
+ request(:put, path, data)
92
+ end
93
+
94
+ def patch(path, data = {})
95
+ request(:patch, path, data)
96
+ end
97
+
98
+ def delete(path)
99
+ request(:delete, path)
100
+ end
101
+
102
+ def request(method, path, data = nil)
103
+ @connection.send(method, path, data).body
104
+ rescue Faraday::ClientError => e
105
+ pp e.response[:body]
106
+ raise
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusClient
4
+ module Version
5
+ STRING = '1.0.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solidus_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Team Nebulab
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-24 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: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
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: '0.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.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.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ description: Solidus eCommerce API Ruby client
70
+ email: hello@nebulab.it
71
+ executables:
72
+ - solidus
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.md
77
+ files:
78
+ - LICENSE.txt
79
+ - README.md
80
+ - exe/solidus
81
+ - lib/solidus_client.rb
82
+ - lib/solidus_client/cli.rb
83
+ - lib/solidus_client/client.rb
84
+ - lib/solidus_client/version.rb
85
+ homepage: https://github.com/nebulab/solidus_client
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.3.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.1.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Solidus API Ruby client
108
+ test_files: []