class Vendor::Info

Attributes

block[RW]
params[RW]

Public Class Methods

new(params, &block) click to toggle source
# File lib/project/info.rb, line 5
def initialize(params, &block)
  @params = params
  @block = block

  # Start product request
  productsRequest = SKProductsRequest.alloc.initWithProductIdentifiers(NSSet.setWithObject(@params.id))
  productsRequest.delegate = self
  productsRequest.start

  # Update receipt if bought and subscription
  if bought? && subscription?
    receipt = NSUserDefaults["#{@params.id}.receipt"]
    decoder = CocoaSecurityDecoder.new
    latest_receipt_data = decoder.base64(receipt[:latest_receipt])
    Vendor::Receipt.new(latest_receipt_data, @params)
  end
end

Public Instance Methods

bought?() click to toggle source
# File lib/project/info.rb, line 53
def bought?
  NSUserDefaults["#{@params.id}.receipt"] != nil
end
description() click to toggle source
# File lib/project/info.rb, line 49
def description
  NSUserDefaults["#{@params.id}.localizedDescription"] || @params.desc
end
price() click to toggle source

INFO METHODS

# File lib/project/info.rb, line 26
def price
  local_price = NSUserDefaults["#{@params.id}.price"] || @params.price
  number_formatter = NSNumberFormatter.alloc.init
  number_formatter.setFormatterBehavior(NSNumberFormatterBehavior10_4)
  number_formatter.setNumberStyle(NSNumberFormatterCurrencyStyle)
  number_formatter.setLocale(price_locale)

  currency_string = number_formatter.internationalCurrencySymbol
  format = number_formatter.positiveFormat
  format.gsub("ยค", currency_string)
  number_formatter.setPositiveFormat(format)

  return number_formatter.stringFromNumber(local_price)
end
price_locale() click to toggle source
# File lib/project/info.rb, line 41
def price_locale
  @params[:price_locale]==nil ? NSLocale.alloc.initWithLocaleIdentifier("en_US") : @params[:price_locale]
end
productsRequest(request, didReceiveResponse:response) click to toggle source

DELEGATE METHODS

# File lib/project/info.rb, line 88
def productsRequest(request, didReceiveResponse:response) 
  exists = response.invalidProductIdentifiers.count==0

  # Save needed product info
  if exists
    product = response.products.first
    NSUserDefaults["#{@params.id}.price"] = product.price
    @params[:price_locale] = product.priceLocale
    NSUserDefaults["#{@params.id}.localizedTitle"] = product.localizedTitle
    NSUserDefaults["#{@params.id}.localizedDescription"] = product.localizedDescription
  end

  @block.call({success: exists, response: response, error: nil}.to_object)
end
request(request, didFailWithError:error) click to toggle source
# File lib/project/info.rb, line 103
def request(request, didFailWithError:error)
  @block.call({success: false, response: nil, error: error}.to_object)
end
subscribed?() click to toggle source
# File lib/project/info.rb, line 61
def subscribed?
  # Return false if product is not a subscription or not bought
  return false if !subscription? || !bought?
  receipt = NSUserDefaults["#{@params.id}.receipt"]
  return false if receipt[:status].to_i!=0

  # Get the latest receipt
  receipt = NSUserDefaults["#{@params.id}.receipt"]
  decoder = CocoaSecurityDecoder.new
  latest_receipt_data = decoder.base64(receipt[:latest_receipt])
  latest_receipt_plist = NSPropertyListSerialization.propertyListWithData(latest_receipt_data, options:NSPropertyListImmutable, format:nil, error:nil)

  # Get the time for the latest receipt
  purchase_info_data = decoder.base64(latest_receipt_plist.objectForKey("purchase-info"))
  purchase_info_plist = NSPropertyListSerialization.propertyListWithData(purchase_info_data, options:NSPropertyListImmutable, format:nil, error:nil)

  # Manipulate the time for the latest receipt
  expires_date = purchase_info_plist.objectForKey("expires-date")
  expires_calc = expires_date.to_i/1000

  # Check if the latest receipt is too old
  return expires_calc > NSDate.date.timeIntervalSince1970
end
subscription?() click to toggle source
# File lib/project/info.rb, line 57
def subscription?
  @params.subscription
end
title() click to toggle source
# File lib/project/info.rb, line 45
def title
  NSUserDefaults["#{@params.id}.localizedTitle"] || @params.title
end