Module: Imobile

Defined in:
lib/imobile/validate_receipt.rb,
lib/imobile/crypto_app_fprint.rb,
lib/imobile/push_notification.rb

Overview

:nodoc: namespace

Defined Under Namespace

Modules: AppStoreReceiptValidation, CryptoSupportAppFprint, PushNotifications

Class Method Summary collapse

Class Method Details

.crypto_app_fprint(device_or_hash, binary_path) ⇒ Object

An iPhone application’s finger-print, as implemented in CryptoSupport.

Args:

device_or_hash:: a Hash or ActiveRecord model representing the result of
                 calling [ZNDeviceFprint deviceAttributes] on the iMobile
                 device
binary_path:: path to the application's binary (executable file)
              corresponding to the application version on the device
              (indicated by :app_version in the device attributes)

Returns a finger-print that should prove the application’s integrity. The finger-print is a string consisting of printable characters.



28
29
30
# File 'lib/imobile/crypto_app_fprint.rb', line 28

def self.crypto_app_fprint(device_or_hash, binary_path)
  CryptoSupportAppFprint.app_fprint device_or_hash, binary_path
end

.pack_hex_push_token(push_token) ⇒ Object

Packs a hexadecimal iMobile token for push notifications into binary form.



80
81
82
# File 'lib/imobile/push_notification.rb', line 80

def self.pack_hex_push_token(push_token)
  [push_token.gsub(/\s/, '')].pack('H*')
end

.push_feedback(path_or_certificate, &block) ⇒ Object

Reads the available feedback from Apple’s Push Notification service.

Args:

certificate_or_path:: see Imobile.push_notification

The currently provided feedback is the tokens for the devices which rejected notifications. Each piece of feedback is a hash with the following keys:

:push_token:: the device's token, in binary (not hexadecimal) format
:time:: the last time when the device rejected notifications; according to
        Apple, the rejection can be discarded if the device sent a
        token after this time

The method reads all the feedback available from the Push Notification service. If a block is given, each piece of feedback is yielded to the method’s block, and the method returns nil. If no block is given, the method returns an array containing all pieces of feedback.



67
68
69
# File 'lib/imobile/push_notification.rb', line 67

def self.push_feedback(path_or_certificate, &block)
  PushNotifications.push_feedback path_or_certificate, &block
end

.push_notification(notification, path_or_certificate) ⇒ Object

Sends a push notification to an iMobile device.

Args:

notification:: ruby Hash indicating the desired notification; the hash
               should have an extra key named :push_token, containing
               the binary-encoded (not hexadecimally-encoded) iMobile device
               token, as provided by the UIApplicationDelegate method
               application:didRegisterForRemoteNotificationsWithDeviceToken: 
path_or_certificate:: the certificate required to talk to APNs; this can be
                      a path to a .p12 file, a string with the contens of
                      the .p12 file, or a previously-read certificate

Raises a RuntimeException if Apple’s Push Notification service doesn’t behave.



33
34
35
# File 'lib/imobile/push_notification.rb', line 33

def self.push_notification(notification, path_or_certificate)
  PushNotifications.push_notification notification, path_or_certificate
end

.push_notifications(path_or_certificate, notifications = [], &block) ⇒ Object

Bulk-transmission of push notifications to Apple’s service.

Args:

path_or_certificate:: see push_notification
notifications:: an array of notification hashes; see push_notification

If the method receives a block, it yields to its block indefinitely. Each time, the block should next a notification or array of notifications to be pushed. The block should break when it’s done.



46
47
48
49
# File 'lib/imobile/push_notification.rb', line 46

def self.push_notifications(path_or_certificate, notifications = [], &block)
  PushNotifications.push_notifications path_or_certificate, notifications,
                                       &block
end

.valid_notification?(notification) ⇒ Boolean

Checks if a notification is valid for Apple’s Push Notification service.

Currently, notifications are valid if their JSON encodings don’t exceed 256 bytes.

Returns:

  • (Boolean)


75
76
77
# File 'lib/imobile/push_notification.rb', line 75

def self.valid_notification?(notification)
  PushNotifications.encode_notification(notification) ? true : false
end

.validate_receipt(receipt_blob, server_type = :sandbox) ⇒ Object

Decodes and validates an In-App Purchase receipt from the App Store.

Args:

receipt_blob:: raw receipt in SKPaymentTransaction.transactionReceipt
server_type:: production or sandbox (the API accepts symbols and strings)

The decoded receipt is returned as a Ruby-friendly hash. Keys are converted to snake_case symbols (e.g. ‘purchase-date’ becomes :purchase_date). Dates and relevant numbers are parsed out of the JSON strings.

Returns false if validation fails (the receipt was tampered with). Raises a RuntimeException if Apple’s Web service returns a HTTP error code.



33
34
35
# File 'lib/imobile/validate_receipt.rb', line 33

def self.validate_receipt(receipt_blob, server_type = :sandbox)
  AppStoreReceiptValidation.validate_receipt receipt_blob
end