Class: SpiffyStoresAPI::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/spiffy_stores_api/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token = nil, shop = nil, extra = {}) ⇒ Session

Returns a new instance of Session.



70
71
72
73
74
75
# File 'lib/spiffy_stores_api/session.rb', line 70

def initialize(url, token = nil, shop = nil, extra = {})
  self.url = self.class.prepare_url(url)
  self.token = token
  self.shop = shop
  self.extra = extra
end

Instance Attribute Details

#extraObject

Returns the value of attribute extra.



14
15
16
# File 'lib/spiffy_stores_api/session.rb', line 14

def extra
  @extra
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/spiffy_stores_api/session.rb', line 14

def name
  @name
end

#shopObject

Returns the value of attribute shop.



14
15
16
# File 'lib/spiffy_stores_api/session.rb', line 14

def shop
  @shop
end

#tokenObject

Returns the value of attribute token.



14
15
16
# File 'lib/spiffy_stores_api/session.rb', line 14

def token
  @token
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/spiffy_stores_api/session.rb', line 14

def url
  @url
end

Class Method Details

.prepare_url(url) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spiffy_stores_api/session.rb', line 36

def prepare_url(url)
  return nil if url.blank?
  # remove http:// or https://
  url = url.strip.gsub(/\Ahttps?:\/\//, '')
  # extract host, removing any username, password or path
  store = URI.parse("https://#{url}").host
  # extract subdomain of .spiffystores.com
  if idx = store.index(".")
    store = store.slice(0, idx)
  end
  return nil if store.empty?
  store = "#{store}.#{spiffy_stores_domain}"
  port ? "#{store}:#{port}" : store
rescue URI::InvalidURIError
  nil
end

.setup(params) ⇒ Object



18
19
20
# File 'lib/spiffy_stores_api/session.rb', line 18

def setup(params)
  params.each { |k,value| public_send("#{k}=", value) }
end

.temp(domain, token, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spiffy_stores_api/session.rb', line 22

def temp(domain, token, &block)
  session = new(domain, token)
  original_site = SpiffyStoresAPI::Base.site.to_s
  original_token = SpiffyStoresAPI::Base.headers['Authorization'].try(:gsub, /^Bearer /i, '')
  original_session = new(original_site, original_token)

  begin
    SpiffyStoresAPI::Base.activate_session(session)
    yield
  ensure
    SpiffyStoresAPI::Base.activate_session(original_session)
  end
end

.validate_signature(params) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/spiffy_stores_api/session.rb', line 53

def validate_signature(params)
  params = params.with_indifferent_access
  return false unless signature = params[:hmac]

  calculated_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), secret, encoded_params_for_signature(params))

  Rack::Utils.secure_compare(calculated_signature, signature)
end

Instance Method Details

#create_permission_url(scope, redirect_uri = nil) ⇒ Object



77
78
79
80
81
# File 'lib/spiffy_stores_api/session.rb', line 77

def create_permission_url(scope, redirect_uri = nil)
  params = {:client_id => api_key, :scope => scope.join(',')}
  params[:redirect_uri] = redirect_uri if redirect_uri
  "#{site}/admin/oauth/authorize?#{parameterize(params)}"
end

#expired?Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/spiffy_stores_api/session.rb', line 126

def expired?
  return false if expires_in.nil?
  expires_in <= 0
end

#expires_atObject



121
122
123
124
# File 'lib/spiffy_stores_api/session.rb', line 121

def expires_at
  return unless extra.present?
  @expires_at ||= Time.at(extra['expires_at']).utc
end

#expires_inObject



116
117
118
119
# File 'lib/spiffy_stores_api/session.rb', line 116

def expires_in
  return unless expires_at.present?
  [0, expires_at.to_i - Time.now.utc.to_i].max
end

#request_token(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/spiffy_stores_api/session.rb', line 83

def request_token(params)
  return token if token

  unless self.class.validate_signature(params) && params[:timestamp].to_i > 24.hours.ago.utc.to_i
    raise SpiffyStoresAPI::ValidationException, "Invalid Signature: Possible malicious login"
  end

  response = access_token_request(params['code'])
  if response.code == "200"
    self.extra = JSON.parse(response.body)
    self.token = extra.delete('access_token')

    if expires_in = extra.delete('expires_in')
      extra['expires_at'] = Time.now.utc.to_i + expires_in
    end
    token
  else
    raise RuntimeError, response.msg
  end
end

#siteObject



108
109
110
# File 'lib/spiffy_stores_api/session.rb', line 108

def site
  "#{protocol}://#{url}/api"
end

#storeObject



104
105
106
# File 'lib/spiffy_stores_api/session.rb', line 104

def store
  Store.current
end

#valid?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/spiffy_stores_api/session.rb', line 112

def valid?
  url.present? && token.present?
end