Class: InsalesApi::App

Inherits:
Object
  • Object
show all
Defined in:
lib/insales_api/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, password) ⇒ App

Returns a new instance of App.



39
40
41
42
43
# File 'lib/insales_api/app.rb', line 39

def initialize(domain, password)
  @authorized = false
  @domain     = self.class.prepare_domain(domain)
  @password   = password
end

Instance Attribute Details

#authorizedObject (readonly)

Returns the value of attribute authorized.



5
6
7
# File 'lib/insales_api/app.rb', line 5

def authorized
  @authorized
end

#domainObject (readonly) Also known as: shop

Returns the value of attribute domain.



5
6
7
# File 'lib/insales_api/app.rb', line 5

def domain
  @domain
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/insales_api/app.rb', line 5

def password
  @password
end

Class Method Details

.configure_api(domain, password) ⇒ Object



10
11
12
# File 'lib/insales_api/app.rb', line 10

def configure_api(domain, password)
  base_resource_class.configure(api_key, domain, password)
end

.install(domain, token, insales_id) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



26
27
28
# File 'lib/insales_api/app.rb', line 26

def install(domain, token, insales_id) # rubocop:disable Lint/UnusedMethodArgument
  true
end

.password_by_token(token) ⇒ Object



34
35
36
# File 'lib/insales_api/app.rb', line 34

def password_by_token(token)
  InsalesApi::Password.create(api_secret, token)
end

.prepare_domain(domain) ⇒ Object Also known as: prepare_shop



16
17
18
# File 'lib/insales_api/app.rb', line 16

def prepare_domain(domain)
  domain.to_s.strip.downcase
end

.uninstall(domain, password) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



30
31
32
# File 'lib/insales_api/app.rb', line 30

def uninstall(domain, password) # rubocop:disable Lint/UnusedMethodArgument
  true
end

Instance Method Details

#auth_token(salt = self.salt, user_email = '', user_name = '', user_id = '') ⇒ Object Also known as: store_auth_token



61
62
63
# File 'lib/insales_api/app.rb', line 61

def auth_token(salt = self.salt, user_email = '', user_name = '', user_id = '')
  @auth_token ||= InsalesApi::Password.create(password, salt, user_email, user_name, user_id)
end

#authorization_urlObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/insales_api/app.rb', line 45

def authorization_url
  host, port = domain, nil
  match = /(.+):(\d+)/.match host
  host, port = match[1..2] if match
  URI::Generic.build(
    scheme: 'http',
    host: host,
    port: port && port.to_i,
    path: "/admin/applications/#{api_key}/login",
    query: {
      token: salt,
      login: api_autologin_url || "http://#{api_host}/#{api_autologin_path}",
    }.to_query,
  ).to_s
end

#authorize(token) ⇒ Object



69
70
71
# File 'lib/insales_api/app.rb', line 69

def authorize(token)
  @authorized = auth_token == token
end

#authorized?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/insales_api/app.rb', line 73

def authorized?
  @authorized
end

#configure_apiObject



77
78
79
# File 'lib/insales_api/app.rb', line 77

def configure_api
  self.class.configure_api(domain, password)
end

#saltObject



65
66
67
# File 'lib/insales_api/app.rb', line 65

def salt
  @salt ||= SecureRandom.hex
end