Class: OmnivoreIO::API

Inherits:
Object
  • Object
show all
Defined in:
lib/omnivore-io/api.rb,
lib/omnivore-io/api/errors.rb,
lib/omnivore-io/api/ticket.rb,
lib/omnivore-io/api/version.rb,
lib/omnivore-io/api/location.rb,
lib/omnivore-io/api/modifier.rb,
lib/omnivore-io/api/menu_item.rb,
lib/omnivore-io/api/order_type.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

HEADERS =
{
  :content_type => :json,
  :accept => :json,
  :'Api-Key' => 'API_KEY_HERE'
}
OPTIONS =
{
  :host => 'https://api.omnivore.io/',
  :api_version => '0.1'
}
VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



69
70
71
72
73
# File 'lib/omnivore-io/api.rb', line 69

def initialize(options={})
  options = OPTIONS.merge(options)

  @api_key = options.delete(:api_key) || ENV['OMNIVORE_IO_API_KEY']
end

Instance Method Details

#add_menu_item_to_ticket(location_id, ticket_id, payload_json) ⇒ Object



57
58
59
60
# File 'lib/omnivore-io/api/ticket.rb', line 57

def add_menu_item_to_ticket(location_id, ticket_id, payload_json)
  response = request(:post, "/locations/#{location_id}/tickets/#{ticket_id}/items", payload_json)
  OmnivoreIO::Ticket.new self, response.merge(location_id: location_id)
end

#add_payment_to_ticket(location_id, ticket_id, payload_json) ⇒ Object



62
63
64
65
# File 'lib/omnivore-io/api/ticket.rb', line 62

def add_payment_to_ticket(location_id, ticket_id, payload_json)
  response = request(:post, "/locations/#{location_id}/tickets/#{ticket_id}/payments", payload_json)
  response
end

#api_keyObject



75
76
77
# File 'lib/omnivore-io/api.rb', line 75

def api_key
  @api_key
end

#get_location(location_id) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/omnivore-io/api/location.rb', line 48

def get_location(location_id)
  response = request(
    :get,
    "/locations/#{location_id}"
  )
  OmnivoreIO::Location.new self, response
end

#get_locations(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/omnivore-io/api/location.rb', line 37

def get_locations(options={})
  response = request(
    :get,
    '/locations',
    options
  )
  (response['_embedded']['locations'] || []).map do |location_hash|
    OmnivoreIO::Location.new self, location_hash
  end
end

#get_menu_item(location_id, menu_item_id) ⇒ Object



29
30
31
32
# File 'lib/omnivore-io/api/menu_item.rb', line 29

def get_menu_item(location_id, menu_item_id)
  response = request(:get, "/locations/#{location_id}/menu/items/#{menu_item_id}")
  OmnivoreIO::MenuItem.new self, response.merge(location_id: location_id)
end

#get_menu_items(location_id, options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/omnivore-io/api/menu_item.rb', line 22

def get_menu_items(location_id, options={})
  response = request(:get, "/locations/#{location_id}/menu/items", options)
  (response['_embedded']['menu_items'] || []).map do |menu_item_hash|
    OmnivoreIO::MenuItem.new self, menu_item_hash.merge(location_id: location_id)
  end
end

#get_modifier(location_id, modifier_id) ⇒ Object



29
30
31
32
# File 'lib/omnivore-io/api/modifier.rb', line 29

def get_modifier(location_id, modifier_id)
  response = request(:get, "/locations/#{location_id}/menu/modifiers/#{modifier_id}")
  OmnivoreIO::Modifier.new self, response.merge(location_id: location_id)
end

#get_modifiers(location_id, options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/omnivore-io/api/modifier.rb', line 22

def get_modifiers(location_id, options={})
  response = request(:get, "/locations/#{location_id}/menu/modifiers", options)
  (response['_embedded']['modifiers'] || []).map do |modifier_hash|
    OmnivoreIO::Modifier.new self, modifier_hash.merge(location_id: location_id)
  end
end

#get_order_types(location_id, options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/omnivore-io/api/order_type.rb', line 21

def get_order_types(location_id, options={})
  response = request(:get, "/locations/#{location_id}/order_types", options)
  (response['_embedded']['order_types'] || []).map do |order_type_hash|
    OmnivoreIO::OrderType.new self, order_type_hash.merge(location_id: location_id)
  end
end

#get_ticket(location_id, ticket_id) ⇒ Object



42
43
44
45
# File 'lib/omnivore-io/api/ticket.rb', line 42

def get_ticket(location_id, ticket_id)
  response = request(:get, "/locations/#{location_id}/tickets/#{ticket_id}")
  OmnivoreIO::Ticket.new self, response.merge(location_id: location_id)
end

#get_tickets(location_id, options = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/omnivore-io/api/ticket.rb', line 35

def get_tickets(location_id, options={})
  response = request(:get, "/locations/#{location_id}/tickets", options)
  (response['_embedded']['tickets'] || []).map do |ticket_hash|
    OmnivoreIO::Ticket.new self, ticket_hash.merge(location_id: location_id)
  end
end

#open_ticket(location_id, ticket_json) ⇒ Object



47
48
49
50
# File 'lib/omnivore-io/api/ticket.rb', line 47

def open_ticket(location_id, ticket_json)
  response = request(:post, "/locations/#{location_id}/tickets", ticket_json)
  OmnivoreIO::Ticket.new self, response.merge(location_id: location_id)
end

#request(method, endpoint, query = {}, headers = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omnivore-io/api.rb', line 79

def request(method, endpoint, query={}, headers={})
  headers.merge!({ 'content_type' => :json, 'Api-Key' => self.api_key })
  response = case method
  when :get
    url = "#{OPTIONS[:host]}#{OPTIONS[:api_version]}#{endpoint}"
    if query.keys.length
      url << "?"
      url << URI.encode_www_form(query)
    end
    RestClient.get url, headers
  when :post
    url = "#{OPTIONS[:host]}#{OPTIONS[:api_version]}#{endpoint}"
    payload = query.as_json.to_json
    RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers)
  end
  # TODO: handle errors
  JSON.parse(response.to_str)
end

#void_ticket(location_id, ticket_id) ⇒ Object



52
53
54
55
# File 'lib/omnivore-io/api/ticket.rb', line 52

def void_ticket(location_id, ticket_id)
  response = request(:post, "/locations/#{location_id}/tickets/#{ticket_id}", {"void": true})
  OmnivoreIO::Ticket.new self, response.merge(location_id: location_id)
end