Class: AliexpressAPI::Base

Inherits:
Object
  • Object
show all
Extended by:
ThreadsafeAttributes
Defined in:
lib/aliexpress_api/resources/base.rb

Direct Known Subclasses

AccessToken, DsLogistic, DsOrder, DsProduct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreadsafeAttributes

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



109
110
111
# File 'lib/aliexpress_api/resources/base.rb', line 109

def initialize(attributes = {})
  @attributes = attributes.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *_arguments) ⇒ Object (private)

:nodoc:



146
147
148
149
# File 'lib/aliexpress_api/resources/base.rb', line 146

def method_missing(method_symbol, *_arguments) #:nodoc:
  method_name = method_symbol.to_s
  attributes[method_name] if attributes.include?(method_name)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



107
108
109
# File 'lib/aliexpress_api/resources/base.rb', line 107

def attributes
  @attributes
end

Class Method Details

.activate_session(session) ⇒ Object



30
31
32
33
# File 'lib/aliexpress_api/resources/base.rb', line 30

def activate_session(session)
  self.session = session
  self
end

.app_keyObject

Raises:



39
40
41
42
43
# 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_secretObject



45
46
47
48
49
# 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

.connectionObject



35
36
37
# File 'lib/aliexpress_api/resources/base.rb', line 35

def connection
  @connection ||= Connection.new
end

.service_endpointObject



51
52
53
54
55
# 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

.sessionObject



61
62
63
64
65
66
67
# File 'lib/aliexpress_api/resources/base.rb', line 61

def session
  if _session_defined?
    _session
  elsif superclass != Object
    superclass.session
  end
end

.session=(session) ⇒ Object



57
58
59
# File 'lib/aliexpress_api/resources/base.rb', line 57

def session=(session)
  self._session = session
end

Instance Method Details

#[](key) ⇒ Object



113
114
115
# File 'lib/aliexpress_api/resources/base.rb', line 113

def [](key)
  attributes[key.to_s]
end

#[]=(key, value) ⇒ Object



117
118
119
# File 'lib/aliexpress_api/resources/base.rb', line 117

def []=(key, value)
  attributes[key.to_s] = value
end

#as_json(*_args) ⇒ Object



126
127
128
# File 'lib/aliexpress_api/resources/base.rb', line 126

def as_json(*_args)
  attributes.as_json
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
# 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_hashObject Also known as: to_h



121
122
123
# File 'lib/aliexpress_api/resources/base.rb', line 121

def to_hash
  attributes.to_h
end

#to_json(*_args) ⇒ Object



130
131
132
# File 'lib/aliexpress_api/resources/base.rb', line 130

def to_json(*_args)
  attributes.to_json
end