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.



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

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



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

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

.install(domain, token, insales_id) ⇒ Object



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

def install(domain, token, insales_id)
  true
end

.password_by_token(token) ⇒ Object



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

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

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



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

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

.uninstall(domain, password) ⇒ Object



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

def uninstall(domain, password)
  true
end

Instance Method Details

#auth_tokenObject Also known as: store_auth_token



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

def auth_token
  @auth_token ||= InsalesApi::Password.create(password, salt)
end

#authorization_urlObject



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

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



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

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

#authorized?Boolean

Returns:

  • (Boolean)


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

def authorized?
  @authorized
end

#configure_apiObject



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

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

#saltObject



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

def salt
  @salt ||= SecureRandom.hex
end