Class: Passtools::Pass

Inherits:
Object
  • Object
show all
Extended by:
Request
Defined in:
lib/passtools/pass.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

construct_url, delete_request, download_file, get, headers, post, put

Constructor Details

#initialize(raw_data) ⇒ Pass

Returns a new instance of Pass.



67
68
69
70
71
72
73
74
75
# File 'lib/passtools/pass.rb', line 67

def initialize(raw_data)
  @raw_data = raw_data
  fields = Array(@raw_data['passFields'])
  fields.each do |k,v|
    # self.instance_variable_set("@#{k}", v)  ## create and initialize an instance variable for this key/value pair
    define_singleton_method  k, proc{ @raw_data["passFields"][k.to_s] }
    define_singleton_method  "#{k}=", proc{ |v| @raw_data["passFields"][k.to_s] = v}
  end
end

Instance Attribute Details

#raw_dataObject

Returns the value of attribute raw_data.



4
5
6
# File 'lib/passtools/pass.rb', line 4

def raw_data
  @raw_data
end

Class Method Details

.add_locations(pass_id, location_list) ⇒ Object



28
29
30
31
# File 'lib/passtools/pass.rb', line 28

def self.add_locations(pass_id,location_list)
  json = MultiJson.dump(location_list)
  post("/pass/#{pass_id}/locations", { :json => json } )
end

.build_from_current(pass_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/passtools/pass.rb', line 49

def self.build_from_current(pass_id)
  begin
    response = show(pass_id)
    self.new(response)
  rescue RestClient::Exception => e
    data = MultiJson.load(e.response)
    data['message'] = e.message
    new(data)
  end
end

.build_from_template(template) ⇒ Object



60
61
62
63
64
65
# File 'lib/passtools/pass.rb', line 60

def self.build_from_template(template)
  @raw_data = {}
  @raw_data['templateId'] = template.id
  @raw_data['passFields'] = template.fields
  self.new(@raw_data)
end

.create(template_id, data) ⇒ Object



18
19
20
21
# File 'lib/passtools/pass.rb', line 18

def self.create(template_id,data)
  json = MultiJson.dump(data)
  post("/pass/#{template_id}", {:json => json } )
end

.delete(pass_id) ⇒ Object



45
46
47
# File 'lib/passtools/pass.rb', line 45

def self.delete(pass_id)
  delete_request("/pass/#{pass_id}")
end

.delete_location(pass_id, location_id) ⇒ Object



33
34
35
# File 'lib/passtools/pass.rb', line 33

def self.delete_location(pass_id,location_id)
  delete_request("/pass/#{pass_id}/location/#{location_id}" )
end

.download(pass_id) ⇒ Object



37
38
39
# File 'lib/passtools/pass.rb', line 37

def self.download(pass_id)
  download_file("/pass/#{pass_id}/download", 'PassToolsPass.pkpass')
end

.listObject



6
7
8
# File 'lib/passtools/pass.rb', line 6

def self.list
  get("/pass")
end

.list_by_template(template_id) ⇒ Object



10
11
12
# File 'lib/passtools/pass.rb', line 10

def self.list_by_template(template_id)
  #TODO not implemented in API yet
end

.push(pass_id) ⇒ Object



41
42
43
# File 'lib/passtools/pass.rb', line 41

def self.push(pass_id)
  put("/pass/#{pass_id}/push")
end

.show(pass_id, params = {}) ⇒ Object



14
15
16
# File 'lib/passtools/pass.rb', line 14

def self.show(pass_id, params = {})
  get("/pass/#{pass_id}", params)
end

.update(pass_id, data) ⇒ Object



23
24
25
26
# File 'lib/passtools/pass.rb', line 23

def self.update(pass_id,data)
  json = MultiJson.dump(data)
  put("/pass/#{pass_id}", { :json => json } )
end

Instance Method Details

#createObject



89
90
91
92
93
94
# File 'lib/passtools/pass.rb', line 89

def create
  return false if self.id
  response = self.class.create(template_id, @raw_data["passFields"])
  new_id = response['id']
  self.raw_data = response if new_id
end

#deleteObject



106
107
108
109
110
111
# File 'lib/passtools/pass.rb', line 106

def delete
  return false unless self.id
  response = self.class.delete(id)
  self.raw_data['id'] = nil if response['Status'] == 'Deleted'
  response
end

#downloadObject



113
114
115
116
# File 'lib/passtools/pass.rb', line 113

def download
  return false unless self.id
  self.class.download(self.id)
end

#idObject



77
78
79
# File 'lib/passtools/pass.rb', line 77

def id
  @raw_data["id"]
end

#pushObject



101
102
103
104
# File 'lib/passtools/pass.rb', line 101

def push
  return false unless self.id
  self.class.push(id)
end

#template_idObject



81
82
83
# File 'lib/passtools/pass.rb', line 81

def template_id
  @raw_data["templateId"]
end

#updateObject



96
97
98
99
# File 'lib/passtools/pass.rb', line 96

def update
  return false unless self.id
  self.class.update(id, @raw_data["passFields"])
end

#valid?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/passtools/pass.rb', line 85

def valid?
  @raw_data.has_key?('id')
end