Sha256: a95445f29769df336e452db2513088ca6f6ab4ad3a1accb6a1c29de5f618d2a6

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module InsalesApi
  class App
    class_attribute :api_key, :api_host, :api_secret, :api_autologin_path
    attr_reader :authorized, :auth_token, :shop, :password

    def initialize(shop, password)
      @authorized = false
      @shop       = self.class.prepare_shop shop
      @password   = password
    end

    def authorization_url
      store_auth_token
      "http://#{shop}/admin/applications/#{self.class.api_key}/login?" \
        "token=#{salt}&login=http://#{self.class.api_host}/#{self.class.api_autologin_path}"
    end

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

    def salt
      @salt ||= Digest::MD5.hexdigest("Twulvyeik#{$$}#{Time.now.to_i}thithAwn")
    end

    def authorize token
      @authorized = false
      if self.auth_token and self.auth_token == token
        @authorized = true
      end

      @authorized
    end

    def authorized?
      @authorized
    end

    def configure_api
      self.class.configure_api shop, password
    end

    class << self
      def configure_api shop, password
        InsalesApi::Base.configure api_key, shop, password
      end

      def prepare_shop shop
        shop.to_s.strip.downcase
      end

      def install shop, token, insales_id
        true
      end

      def uninstall shop, password
        true
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
insales_api-0.0.13 lib/insales_api/app.rb