Class: Honey::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/honey/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Order

Returns a new instance of Order.



19
20
21
# File 'lib/honey/order.rb', line 19

def initialize(args = {})
  update(args)
end

Instance Attribute Details

#address1Object

Returns the value of attribute address1.



17
18
19
# File 'lib/honey/order.rb', line 17

def address1
  @address1
end

#address2Object

Returns the value of attribute address2.



17
18
19
# File 'lib/honey/order.rb', line 17

def address2
  @address2
end

#cityObject

Returns the value of attribute city.



17
18
19
# File 'lib/honey/order.rb', line 17

def city
  @city
end

#countryObject

Returns the value of attribute country.



17
18
19
# File 'lib/honey/order.rb', line 17

def country
  @country
end

#dateObject

Returns the value of attribute date.



17
18
19
# File 'lib/honey/order.rb', line 17

def date
  @date
end

#emailaddressObject

Returns the value of attribute emailaddress.



17
18
19
# File 'lib/honey/order.rb', line 17

def emailaddress
  @emailaddress
end

#firstObject

Returns the value of attribute first.



17
18
19
# File 'lib/honey/order.rb', line 17

def first
  @first
end

#instructionsObject

Returns the value of attribute instructions.



17
18
19
# File 'lib/honey/order.rb', line 17

def instructions
  @instructions
end

#itemsObject

Returns the value of attribute items.



17
18
19
# File 'lib/honey/order.rb', line 17

def items
  @items
end

#lastObject

Returns the value of attribute last.



17
18
19
# File 'lib/honey/order.rb', line 17

def last
  @last
end

#phoneObject

Returns the value of attribute phone.



17
18
19
# File 'lib/honey/order.rb', line 17

def phone
  @phone
end

#referenceObject

Returns the value of attribute reference.



17
18
19
# File 'lib/honey/order.rb', line 17

def reference
  @reference
end

#shipbyObject Also known as: shipping

Returns the value of attribute shipby.



17
18
19
# File 'lib/honey/order.rb', line 17

def shipby
  @shipby
end

#stateObject

Returns the value of attribute state.



17
18
19
# File 'lib/honey/order.rb', line 17

def state
  @state
end

#zipObject

Returns the value of attribute zip.



17
18
19
# File 'lib/honey/order.rb', line 17

def zip
  @zip
end

Class Method Details

.attr_accessor(*attributes) ⇒ Object



3
4
5
6
7
# File 'lib/honey/order.rb', line 3

def self.attr_accessor(*attributes)
  @attributes ||= []
  @attributes.concat attributes
  super(*attributes)
end

.attributesObject



9
10
11
# File 'lib/honey/order.rb', line 9

def self.attributes
  @attributes
end

.shipping_option(carrier: :special, service:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/honey/order.rb', line 71

def self.shipping_option(carrier: :special, service:)
  carrier = carrier.to_s
  service = service.to_s
  unless carrier_options = shipping_options[carrier.downcase]
    raise Honey::Error, "'#{carrier}' isn't a valid carrier. Valid carriers are: #{shipping_options.keys}"
  end
  unless option = carrier_options[service.downcase]
    raise Honey::Error, "'#{service}' isn't a shipping option for #{carrier}. Valid options are: #{carrier_options.keys}"
  end
  option
end

.shipping_option_from_api_code(api_code) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/honey/order.rb', line 87

def self.shipping_option_from_api_code(api_code)
  shipping_options.each_pair do |carrier, options|
    options.each_pair do |service, code|
      return { carrier: carrier, service: service } if api_code == code
    end
  end
  nil
end

Instance Method Details

#attributesObject



13
14
15
# File 'lib/honey/order.rb', line 13

def attributes
  self.class.attributes
end

#each_pair(&block) ⇒ Object



47
48
49
50
51
# File 'lib/honey/order.rb', line 47

def each_pair(&block)
  present_attributes.each do |key|
    yield(key, self.send(key), block)
  end
end

#invalid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/honey/order.rb', line 57

def invalid?
  !valid?
end

#present_attributesObject



65
66
67
68
69
# File 'lib/honey/order.rb', line 65

def present_attributes
  attributes.collect do |attr|
    attr unless self.send(attr).nil?
  end.compact
end

#required_attributesObject



61
62
63
# File 'lib/honey/order.rb', line 61

def required_attributes
  attributes - [:address2, :instructions, :phone]
end

#shipping_option(*args) ⇒ Object



83
84
85
# File 'lib/honey/order.rb', line 83

def shipping_option(*args)
  self.class.shipping_option(*args)
end

#update(args = {}) ⇒ Object



23
24
25
26
27
# File 'lib/honey/order.rb', line 23

def update(args = {})
  args.each_pair do |key, value|
    self.send("#{key}=".to_sym, value)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/honey/order.rb', line 53

def valid?
  (required_attributes - present_attributes).empty?
end