Class: PQSDK::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/pqsdk/store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Store

Returns a new instance of Store.



5
6
7
8
9
10
11
12
# File 'lib/pqsdk/store.rb', line 5

def initialize(params = {})
  params.each do |key, val|
    send("#{key}=", val)
  end

  self.leaflet_ids ||= []
  self.opening_hours ||= []
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def address
  @address
end

#cityObject

Returns the value of attribute city.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def city
  @city
end

#city_idObject

Returns the value of attribute city_id.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def city_id
  @city_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def id
  @id
end

#latitudeObject

Returns the value of attribute latitude.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def latitude
  @latitude
end

#leaflet_idsObject

Returns the value of attribute leaflet_ids.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def leaflet_ids
  @leaflet_ids
end

#longitudeObject

Returns the value of attribute longitude.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def name
  @name
end

#opening_hoursObject

Returns the value of attribute opening_hours.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def opening_hours
  @opening_hours
end

#opening_hours_textObject

Returns the value of attribute opening_hours_text.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def opening_hours_text
  @opening_hours_text
end

#originObject

Returns the value of attribute origin.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def origin
  @origin
end

#phoneObject

Returns the value of attribute phone.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def phone
  @phone
end

#zipcodeObject

Returns the value of attribute zipcode.



3
4
5
# File 'lib/pqsdk/store.rb', line 3

def zipcode
  @zipcode
end

Class Method Details

.find(address, zipcode, retailer = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/pqsdk/store.rb', line 14

def self.find(address, zipcode, retailer = nil)
  res = RestLayer.get('v1/stores', { address: address, zipcode: zipcode, retailer: retailer }, { 'Authorization' => "Bearer #{Token.access_token}" })
  if res[0] == 200
    Store.from_json res[1]
  elsif res[0] == 404
    nil
  else
    raise Exception.new("Unexpected HTTP status code #{res[0]}, #{res[1]}")
  end
end

.get(id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/pqsdk/store.rb', line 25

def self.get(id)
  res = RestLayer.get("v1/stores/#{id}", { }, { 'Authorization' => "Bearer #{Token.access_token}" })
  if res[0] == 200
    Store.from_json res[1]
  elsif res[0] == 404
    nil
  else
    raise Exception.new("Unexpected HTTP status code #{res[0]}, #{res[1]}")
  end
end

Instance Method Details

#saveObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pqsdk/store.rb', line 36

def save
  if self.id != nil
    method = :put
    url = "v1/stores/#{self.id}"
    expected_status = 200
  else
    method = :post
    url = "v1/stores"
    expected_status = 201
  end

  if city.nil? and city_id.nil?
    raise "city or city_id must be set"
  end

  fields = {}
  if method != :put
    [ :name, :address, :zipcode, :latitude, :longitude, :origin ].each do |field|
      raise "Missing required #{field} field" if send(field).to_s == ''
      fields[field.to_s] = send(field)
    end

    fields['city'] = city if city
    fields['city_id'] = city_id if city_id
    fields['phone'] = phone if phone
  end

  fields['opening_hours'] = opening_hours if opening_hours.try(:any?)
  fields['opening_hours_text'] = opening_hours_text if opening_hours_text.present?

  res = RestLayer.send(method, url, fields, { 'Authorization' => "Bearer #{Token.access_token}", 'Content-Type' => 'application/json' })

  if res[0] != expected_status
    raise Exception.new("Unexpected HTTP status code #{res[0]}, #{res[1]}")
  else
    if method == :post
      self.id = res[1]['id']
    end
  end
end