class AliexpressAPI::Base

Attributes

attributes[R]

Public Class Methods

activate_session(session) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 30
def activate_session(session)
  self.session = session
  self
end
app_key() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 39
def app_key
  return AliexpressAPI.app_key if AliexpressAPI.app_key

  raise AppKeyNotSetError, 'You must set AliexpressAPI::Base.app_key before making a request.'
end
app_secret() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 45
def app_secret
  return AliexpressAPI.app_secret if AliexpressAPI.app_secret

  raise AppSecretNotSetError, 'You must set AliexpressAPI::Base.app_secret before making a request.'
end
connection() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 35
def connection
  @connection ||= Connection.new
end
new(attributes = {}) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 109
def initialize(attributes = {})
  @attributes = attributes.with_indifferent_access
end
service_endpoint() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 51
def service_endpoint
  return AliexpressAPI.service_endpoint if AliexpressAPI.service_endpoint

  raise AppSecretNotSetError, 'You must set AliexpressAPI::Base.service_endpoint before making a request.'
end
session() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 61
def session
  if _session_defined?
    _session
  elsif superclass != Object
    superclass.session
  end
end
session=(session) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 57
def session=(session)
  self._session = session
end

Private Class Methods

calculated_signature(params) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 97
def calculated_signature(params)
  encoded_params = app_secret.dup.concat(hash_to_string(params), app_secret)
  Digest::MD5.hexdigest(encoded_params).upcase
end
common_params() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 80
def common_params
  {
    app_key: app_key,
    sign_method: 'md5',
    timestamp: Time.now.in_time_zone("Asia/Shanghai").strftime('%Y-%m-%d %H:%M:%S'),
    format: 'json',
    v: '2.0',
    session: session
  }
end
flatten_params(params) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 71
def flatten_params(params)
  params.each_pair do |key, value|
    if value.present? && (value.is_a?(Hash) || value.is_a?(Array))
      params[key] = value.to_json
      end
  end
  params
end
generate_request_params(params) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 91
def generate_request_params(params)
  request_params = flatten_params(common_params.merge(params))
  request_params[:sign] = calculated_signature(request_params)
  request_params
end
hash_to_string(hash) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 102
def hash_to_string(hash)
  hash.keys.sort.map { |key| "#{key}#{hash[key]}" }.join
end

Public Instance Methods

[](key) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 113
def [](key)
  attributes[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 117
def []=(key, value)
  attributes[key.to_s] = value
end
as_json(*_args) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 126
def as_json(*_args)
  attributes.as_json
end
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/aliexpress_api/resources/base.rb, line 134
def respond_to_missing?(method, *args)
  if attributes.nil?
    super
  elsif attributes.include?(method.to_s)
    true
  else
    super
  end
end
to_h()
Alias for: to_hash
to_hash() click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 121
def to_hash
  attributes.to_h
end
Also aliased as: to_h
to_json(*_args) click to toggle source
# File lib/aliexpress_api/resources/base.rb, line 130
def to_json(*_args)
  attributes.to_json
end