aliexpress-ruby-sdk 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5b6c9eb03f7a8085d1a74a72feac2af666456ceeeec729ce6cb3241727136a36
4
+ data.tar.gz: c3c50ef02016e0ff04e313ead0aeffa761ed28e6b06b966ffa66238d3f7305e6
5
+ SHA512:
6
+ metadata.gz: fa9975dcc2c7778f3a65198c9b7a22ab175951d0dca5d4d9417eac605993e565485f28a053cbe8fa70d28fe4185e60e69a689f30ce85b6569a622afbc1a40d1c
7
+ data.tar.gz: 0fc8b0fa031cfddc6bf3083d2a44cc2fbbadb1bcc49e2ae4e349c63cae06de36ec6ae7f6e572fd93ccabbd3b0e2371394a28d9d7e042de8ddfbeaacaf370333d
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at huangpuping@shoplazza.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in aliexpress-ruby-sdk.gemspec
8
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 huang puping
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.
@@ -0,0 +1,76 @@
1
+ # AliexpressAPI
2
+
3
+ Aliexpress SDK for Ruby. 了解更多信息请访问 [Aliexpress API 文档](https://developers.aliexpress.com/en/doc.htm?docId=108970&docType=1).
4
+
5
+ ## 安装
6
+ 添加 `aliexpress-ruby-sdk` 到你的 Gemfile:
7
+ ```ruby
8
+ gem 'aliexpress-ruby-sdk'
9
+ ```
10
+ 或通过gem安装 `gem install aliexpress-ruby-sdk`
11
+ ```ruby
12
+ require 'aliexpress_api'
13
+ ```
14
+
15
+ ## Usage
16
+ ### 1) 设置 App key 和 App secret
17
+ 在 Rails initializer 中创建文件:`config/initializers/omniauth.rb`, 并加入以下代码
18
+ ```ruby
19
+ AliexpressAPI.configure do |config|
20
+ config.app_key = "your App key"
21
+ config.app_secret = "your App secret"
22
+
23
+ # eg: config.service_endpoint = https://eco.taobao.com/router/rest
24
+ config.service_endpoint = "SERVICE_ENDPOINT"
25
+ end
26
+ ```
27
+ ### 2) 向 Aliexpress 发送请求
28
+ 调用 API 之前必须先激活 session:
29
+ ```ruby
30
+ # 成功授权后拿到的 token
31
+ AliexpressAPI::Base.activate_session "token"
32
+ ```
33
+ 调用 API
34
+ ```ruby
35
+ # 创建订单
36
+ AliexpressAPI::Order.create!(
37
+ logistics_address: {
38
+ address: "china"
39
+ },
40
+ product_items: {
41
+ product_count: 1,
42
+ product_id: "4000518642376",
43
+ sku_attr: "N",
44
+ logistics_service_name: "EMS"
45
+ }
46
+ )
47
+
48
+ # 获取订单信息
49
+ AliexpressAPI::Order.find(300518642)
50
+
51
+ # 获取物流信息
52
+ AliexpressAPI::Logistic.tracking_info(
53
+ logistics_no: "330211",
54
+ out_ref: "300518642",
55
+ service_name: "EMS",
56
+ to_area: "US"
57
+ )
58
+ ```
59
+
60
+ ### 3) API List
61
+ - [x] 功能实现
62
+ - [ ] 功能未实现
63
+
64
+ #### -> Dropshipping
65
+ - [x] Dropshipping get product info
66
+ - [x] Dropshipping place order
67
+ - [x] Dropshipping obtain shipping info
68
+ - [x] Dropshipping get order info
69
+ - [x] Query simple information of product for dropshipper
70
+ - [x] Dropshipping get tracking info
71
+ - [ ] Dropshipping sync sales data
72
+ #### -> other
73
+ - [ ] all
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'aliexpress_api/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'aliexpress-ruby-sdk'
9
+ spec.version = AliexpressAPI::VERSION
10
+ spec.authors = ['pupi']
11
+ spec.email = ['pupinghuang@foxmail.com']
12
+
13
+ spec.summary = 'Aliexpress SDK for ruby'
14
+ spec.homepage = 'https://github.com/pupi94/aliexpress-ruby-sdk'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ spec.required_ruby_version = '>= 2.5'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.17'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ spec.add_dependency 'activesupport', '>= 5.0'
31
+ spec.add_dependency 'http', '~> 4.0'
32
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'aliexpress/ruby/sdk'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/attribute_accessors'
4
+ require 'aliexpress_api/version'
5
+ require 'aliexpress_api/connection'
6
+ require 'aliexpress_api/resources'
7
+ require 'aliexpress_api/exceptions'
8
+ require 'aliexpress_api/threadsafe_attributes'
9
+
10
+ module AliexpressAPI
11
+ mattr_accessor :app_key
12
+ @@app_key = nil
13
+
14
+ mattr_accessor :app_secret
15
+ @@app_secret = nil
16
+
17
+ mattr_accessor :service_endpoint
18
+ @@service_endpoint = nil
19
+
20
+ def self.configure
21
+ yield self
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+
5
+ module AliexpressAPI
6
+ class Connection
7
+ def get(path, params: {})
8
+ handle_request { HTTP.get(path, params: params) }
9
+ end
10
+
11
+ def post(path, params: {})
12
+ handle_request { HTTP.post(path, form: params) }
13
+ end
14
+
15
+ private
16
+
17
+ def handle_request
18
+ response = yield
19
+ handle_response response
20
+ rescue HTTP::ConnectionError => e
21
+ raise ConnectionError, e.message
22
+ rescue HTTP::RequestError => e
23
+ raise RequestError, e.message
24
+ rescue HTTP::TimeoutError => e
25
+ raise TimeoutError, e.message
26
+ rescue HTTP::HeaderError => e
27
+ raise HeaderError, e.message
28
+ rescue HTTP::Error => e
29
+ raise Error, e.message
30
+ end
31
+
32
+ def handle_response(response)
33
+ case response.code.to_i
34
+ when 301
35
+ raise RedirectionError, response
36
+ when 200
37
+ response.parse(:json)
38
+ when 400
39
+ raise BadRequestError, response
40
+ when 401
41
+ raise UnauthorizedAccessError, response
42
+ when 403
43
+ raise ForbiddenAccessError, response
44
+ when 404
45
+ raise ResourceNotFoundError, response
46
+ when 422
47
+ raise ResourceInvalidError, response
48
+ when 429
49
+ raise TooManyRequestsError, response
50
+ when 500
51
+ raise ServerError, response
52
+ else
53
+ raise ResponseError.new(response, "Unknown response code: #{response.code}")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ class Error < StandardError; end
5
+ class RequestError < Error; end
6
+ class ConnectionError < Error; end
7
+ class StateError < Error; end
8
+ class TimeoutError < Error; end
9
+ class HeaderError < Error; end
10
+
11
+ class ResponseError < Error
12
+ attr_reader :response
13
+
14
+ def initialize(response, message = nil)
15
+ @response = response
16
+ @message = message
17
+ end
18
+
19
+ def to_s
20
+ message = +'Failed.'
21
+ if response.respond_to?(:code)
22
+ message << " Response code = #{response.code}."
23
+ end
24
+ if response.respond_to?(:to_s)
25
+ message << " Response message = #{response}."
26
+ end
27
+ message
28
+ end
29
+ end
30
+ class RedirectionError < ResponseError
31
+ def to_s
32
+ response['Location'] ? "#{super} => #{response['Location']}" : super
33
+ end
34
+ end
35
+ class BadRequestError < ResponseError; end
36
+ class UnauthorizedAccessError < ResponseError; end
37
+ class ResourceNotFoundError < ResponseError; end
38
+ class ForbiddenAccessError < ResponseError; end
39
+ class ResourceInvalidError < ResponseError; end
40
+ class TooManyRequestsError < ResponseError; end
41
+ class ServerError < ResponseError; end
42
+
43
+ class AppKeyNotSetError < StandardError; end
44
+ class AppSecretNotSetError < StandardError; end
45
+
46
+ class ResultError < Error
47
+ attr_reader :result
48
+
49
+ def initialize(result, message: nil)
50
+ @result = result
51
+ @message = message
52
+ end
53
+
54
+ def message
55
+ @message.present? ? @message : to_s
56
+ end
57
+
58
+ def to_s
59
+ return "Failed. #{@result.to_json}." if @result.respond_to?(:to_json)
60
+
61
+ "Failed. #{@result}."
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aliexpress_api/resources/base'
4
+ Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require(file) }
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/time/zones'
4
+ require 'active_support/time_with_zone'
5
+ require 'active_support/core_ext/hash'
6
+ require 'active_support/hash_with_indifferent_access'
7
+ require 'aliexpress_api/threadsafe_attributes'
8
+
9
+ module AliexpressAPI
10
+ class Base
11
+ class << self
12
+ include ThreadsafeAttributes
13
+
14
+ threadsafe_attribute :_session
15
+
16
+ %i[get post].each do |method|
17
+ define_method method do |url, params = {}|
18
+ response = connection.send(method, url, params: generate_request_params(params))
19
+
20
+ error_response = response['error_response']
21
+ if error_response.present?
22
+ message = [error_response['msg'], error_response['sub_msg']].select(&:present?).join(': ')
23
+ raise ResultError.new(error_response, message: message)
24
+ end
25
+
26
+ response
27
+ end
28
+ end
29
+
30
+ def activate_session(session)
31
+ self.session = session
32
+ self
33
+ end
34
+
35
+ def connection
36
+ @connection ||= Connection.new
37
+ end
38
+
39
+ def app_key
40
+ return AliexpressAPI.app_key if AliexpressAPI.app_key
41
+
42
+ raise AppKeyNotSetError, 'You must set AliexpressAPI::Base.app_key before making a request.'
43
+ end
44
+
45
+ def app_secret
46
+ return AliexpressAPI.app_secret if AliexpressAPI.app_secret
47
+
48
+ raise AppSecretNotSetError, 'You must set AliexpressAPI::Base.app_secret before making a request.'
49
+ end
50
+
51
+ def service_endpoint
52
+ return AliexpressAPI.service_endpoint if AliexpressAPI.service_endpoint
53
+
54
+ raise AppSecretNotSetError, 'You must set AliexpressAPI::Base.service_endpoint before making a request.'
55
+ end
56
+
57
+ def session=(session)
58
+ self._session = session
59
+ end
60
+
61
+ def session
62
+ if _session_defined?
63
+ _session
64
+ elsif superclass != Object
65
+ superclass.session
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def flatten_params(params)
72
+ params.each_pair do |key, value|
73
+ if value.present? && (value.is_a?(Hash) || value.is_a?(Array))
74
+ params[key] = value.to_json
75
+ end
76
+ end
77
+ params
78
+ end
79
+
80
+ def common_params
81
+ {
82
+ app_key: app_key,
83
+ sign_method: 'md5',
84
+ timestamp: Time.now.in_time_zone("Asia/Shanghai").strftime('%Y-%m-%d %H:%M:%S'),
85
+ format: 'json',
86
+ v: '2.0',
87
+ session: session
88
+ }
89
+ end
90
+
91
+ def generate_request_params(params)
92
+ request_params = flatten_params(common_params.merge(params))
93
+ request_params[:sign] = calculated_signature(request_params)
94
+ request_params
95
+ end
96
+
97
+ def calculated_signature(params)
98
+ encoded_params = app_secret.dup.concat(hash_to_string(params), app_secret)
99
+ Digest::MD5.hexdigest(encoded_params).upcase
100
+ end
101
+
102
+ def hash_to_string(hash)
103
+ hash.keys.sort.map { |key| "#{key}#{hash[key]}" }.join
104
+ end
105
+ end
106
+
107
+ attr_reader :attributes
108
+
109
+ def initialize(attributes = {})
110
+ @attributes = attributes.with_indifferent_access
111
+ end
112
+
113
+ def [](key)
114
+ attributes[key.to_s]
115
+ end
116
+
117
+ def []=(key, value)
118
+ attributes[key.to_s] = value
119
+ end
120
+
121
+ def to_hash
122
+ attributes.to_h
123
+ end
124
+ alias to_h to_hash
125
+
126
+ def as_json(*_args)
127
+ attributes.as_json
128
+ end
129
+
130
+ def to_json(*_args)
131
+ attributes.to_json
132
+ end
133
+
134
+ def respond_to_missing?(method, *args)
135
+ if attributes.nil?
136
+ super
137
+ elsif attributes.include?(method.to_s)
138
+ true
139
+ else
140
+ super
141
+ end
142
+ end
143
+
144
+ private
145
+
146
+ def method_missing(method_symbol, *_arguments) #:nodoc:
147
+ method_name = method_symbol.to_s
148
+ attributes[method_name] if attributes.include?(method_name)
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ class DsLogistic < Base
5
+ class << self
6
+ def freights(params = {})
7
+ params = {
8
+ method: 'aliexpress.logistics.buyer.freight.calculate',
9
+ param_aeop_freight_calculate_for_buyer_d_t_o: params.dup
10
+ }
11
+
12
+ response = post(service_endpoint, params)
13
+ result = response['aliexpress_logistics_buyer_freight_calculate_response']['result']
14
+
15
+ unless result['success']
16
+ raise ResultError.new(result, message: result['error_desc'])
17
+ end
18
+
19
+ list = result['aeop_freight_calculate_result_for_buyer_d_t_o_list']['aeop_freight_calculate_result_for_buyer_dto']
20
+ list.map(&:with_indifferent_access)
21
+ end
22
+
23
+ def tracking_info(params)
24
+ params = params.merge(
25
+ method: 'aliexpress.logistics.ds.trackinginfo.query',
26
+ origin: 'ESCROW'
27
+ )
28
+ response = post(service_endpoint, params)
29
+ result = response['aliexpress_logistics_ds_trackinginfo_query_response']
30
+
31
+ unless result['result_success']
32
+ raise ResultError.new(result, message: result['error_desc'])
33
+ end
34
+
35
+ {
36
+ official_website: result['official_website'],
37
+ details: result['details']['details']
38
+ }.with_indifferent_access
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ class DsOrder < Base
5
+ class << self
6
+ def create!(attributes = {})
7
+ params = {
8
+ method: 'aliexpress.trade.buy.placeorder',
9
+ param_place_order_request4_open_api_d_t_o: attributes
10
+ }
11
+ response = post(service_endpoint, params)
12
+ result = response['aliexpress_trade_buy_placeorder_response']['result']
13
+
14
+ unless result['is_success']
15
+ raise ResultError.new(result, message: result['error_msg'] || result['error_code'])
16
+ end
17
+
18
+ result['order_list']
19
+ end
20
+
21
+ def find(id)
22
+ params = {
23
+ method: 'aliexpress.trade.ds.order.get',
24
+ single_order_query: { order_id: id }
25
+ }
26
+ response = post(service_endpoint, params)
27
+ result = response['aliexpress_trade_ds_order_get_response']['result']
28
+ new(result)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ class DsProduct < Base
5
+ class << self
6
+ def find(id, options = {})
7
+ params = options.symbolize_keys
8
+ params[:product_id] = id
9
+ params[:method] = 'aliexpress.postproduct.redefining.findaeproductbyidfordropshipper'
10
+
11
+ response = post(service_endpoint, params)
12
+ result = response['aliexpress_postproduct_redefining_findaeproductbyidfordropshipper_response']['result']
13
+
14
+ if result['error_code'].present?
15
+ raise ResultError.new(result, message: result['error_message'])
16
+ end
17
+
18
+ new(result)
19
+ end
20
+
21
+ def simple_find(id, options = {})
22
+ params = options.symbolize_keys
23
+ params[:product_id] = id
24
+ params[:method] = 'aliexpress.offer.ds.product.simplequery'
25
+
26
+ response = post(service_endpoint, params)
27
+ result = response['aliexpress_offer_ds_product_simplequery_response']
28
+ new(result)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ class AccessToken < Base
5
+ class << self
6
+ def refresh(refresh_token)
7
+ params = {
8
+ client_id: app_key,
9
+ client_secret: app_secret,
10
+ refresh_token: refresh_token,
11
+ grant_type: 'refresh_token'
12
+ }
13
+ response = connection.post('https://oauth.aliexpress.com/token', params: params)
14
+ raise ResultError, response if response['error_code'].present?
15
+
16
+ response
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/duplicable'
4
+
5
+ module AliexpressAPI
6
+ module ThreadsafeAttributes
7
+ def self.included(klass)
8
+ klass.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def threadsafe_attribute(*attrs)
13
+ main_thread = Thread.main # remember this, because it could change after forking
14
+
15
+ attrs.each do |attr|
16
+ define_method attr do
17
+ get_threadsafe_attribute(attr, main_thread)
18
+ end
19
+
20
+ define_method "#{attr}=" do |value|
21
+ set_threadsafe_attribute(attr, value, main_thread)
22
+ end
23
+
24
+ define_method "#{attr}_defined?" do
25
+ threadsafe_attribute_defined?(attr, main_thread)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def get_threadsafe_attribute(name, main_thread)
34
+ if threadsafe_attribute_defined_by_thread?(name, Thread.current)
35
+ get_threadsafe_attribute_by_thread(name, Thread.current)
36
+ elsif threadsafe_attribute_defined_by_thread?(name, main_thread)
37
+ value = get_threadsafe_attribute_by_thread(name, main_thread)
38
+ value = value.dup if value.duplicable?
39
+ set_threadsafe_attribute_by_thread(name, value, Thread.current)
40
+ value
41
+ end
42
+ end
43
+
44
+ def set_threadsafe_attribute(name, value, main_thread)
45
+ set_threadsafe_attribute_by_thread(name, value, Thread.current)
46
+ unless threadsafe_attribute_defined_by_thread?(name, main_thread)
47
+ set_threadsafe_attribute_by_thread(name, value, main_thread)
48
+ end
49
+ end
50
+
51
+ def threadsafe_attribute_defined?(name, main_thread)
52
+ threadsafe_attribute_defined_by_thread?(name, Thread.current) || ((Thread.current != main_thread) && threadsafe_attribute_defined_by_thread?(name, main_thread))
53
+ end
54
+
55
+ def get_threadsafe_attribute_by_thread(name, thread)
56
+ thread.thread_variable_get "aliexpress.#{name}.#{object_id}"
57
+ end
58
+
59
+ def set_threadsafe_attribute_by_thread(name, value, thread)
60
+ thread.thread_variable_set "aliexpress.#{name}.#{object_id}.defined", true
61
+ thread.thread_variable_set "aliexpress.#{name}.#{object_id}", value
62
+ end
63
+
64
+ def threadsafe_attribute_defined_by_thread?(name, thread)
65
+ thread.thread_variable_get "aliexpress.#{name}.#{object_id}.defined"
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AliexpressAPI
4
+ VERSION = '1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliexpress-ruby-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - pupi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: http
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description:
84
+ email:
85
+ - pupinghuang@foxmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - aliexpress-ruby-sdk.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/aliexpress_api.rb
102
+ - lib/aliexpress_api/connection.rb
103
+ - lib/aliexpress_api/exceptions.rb
104
+ - lib/aliexpress_api/resources.rb
105
+ - lib/aliexpress_api/resources/base.rb
106
+ - lib/aliexpress_api/resources/ds_logistic.rb
107
+ - lib/aliexpress_api/resources/ds_order.rb
108
+ - lib/aliexpress_api/resources/ds_product.rb
109
+ - lib/aliexpress_api/resources/token.rb
110
+ - lib/aliexpress_api/threadsafe_attributes.rb
111
+ - lib/aliexpress_api/version.rb
112
+ homepage: https://github.com/pupi94/aliexpress-ruby-sdk
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '2.5'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.0.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Aliexpress SDK for ruby
135
+ test_files: []