Class: Oja::Receipt

Inherits:
Object
  • Object
show all
Defined in:
lib/oja/receipt.rb

Direct Known Subclasses

JSONReceipt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Receipt

Returns a new instance of Receipt.



8
9
10
11
12
# File 'lib/oja/receipt.rb', line 8

def initialize(attributes)
  attributes.each do |attribute, value|
    send("#{attribute}=", value)
  end
end

Instance Attribute Details

#dataObject



26
27
28
# File 'lib/oja/receipt.rb', line 26

def data
  @data ||= read
end

#filenameObject

Returns the value of attribute filename.



5
6
7
# File 'lib/oja/receipt.rb', line 5

def filename
  @filename
end

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/oja/receipt.rb', line 5

def format
  @format
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/oja/receipt.rb', line 5

def password
  @password
end

Instance Method Details

#attributesObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/oja/receipt.rb', line 34

def attributes
  if json?
    attributes = JSON.parse(data)
  else
    attributes = { 'receipt-data' => receipt_data }
  end

  attributes['password'] = password if password
  attributes
end

#json?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/oja/receipt.rb', line 45

def json?
  format == :json
end

#readObject



22
23
24
# File 'lib/oja/receipt.rb', line 22

def read
  File.read(filename)
end

#receipt_dataObject



30
31
32
# File 'lib/oja/receipt.rb', line 30

def receipt_data
  Base64.encode64(data)
end

#request(environment = :production) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/oja/receipt.rb', line 14

def request(environment=:production)
  request = Request.new(
    :receipt     => self,
    :environment => environment
  )
  request.run
end

#to_jsonObject



49
50
51
# File 'lib/oja/receipt.rb', line 49

def to_json
  JSON.dump(attributes)
end

#verifyObject



53
54
55
56
57
58
59
60
61
# File 'lib/oja/receipt.rb', line 53

def verify
  if response = request(:production)
    if response.sandbox_receipt_in_production?
      request(:sandbox)
    else
      response
    end
  end
end