hungry 0.1.0 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,24 @@
1
1
  module Hungry
2
2
  class Location < Resource
3
-
3
+
4
4
  self.endpoint = '/locations'
5
-
5
+
6
6
  ### RESOURCES:
7
-
7
+
8
8
  has_many :venues, 'Hungry::Venue'
9
-
9
+
10
10
  has_many :nearby_venues, 'Hungry::Venue'
11
-
11
+
12
12
  has_many :tags, 'Hungry::Tag'
13
-
13
+
14
14
  ### ATTRIBUTES:
15
-
15
+
16
16
  ### Location:
17
17
  attr_accessor :id, :name, :type, :url, :geolocation,
18
-
18
+
19
19
  ### Utility:
20
20
  :resources, :counters
21
-
21
+
22
22
  def geolocation=(new_coordinates)
23
23
  @geolocation = Geolocation.parse(new_coordinates).tap do |geo|
24
24
  attributes[:geolocation] = geo
@@ -3,43 +3,43 @@ module Hungry
3
3
  autoload :Category, 'hungry/menu/category'
4
4
  autoload :Dish, 'hungry/menu/dish'
5
5
  autoload :Option, 'hungry/menu/option'
6
-
6
+
7
7
  self.endpoint = '/menus'
8
-
8
+
9
9
  ### RESOURCES:
10
-
10
+
11
11
  belongs_to :venue, 'Hungry::Venue'
12
-
12
+
13
13
  ### ATTRIBUTES:
14
-
14
+
15
15
  ### Menu:
16
- attr_accessor :id, :name, :type, :attachment,
17
-
16
+ attr_accessor :id, :name, :type, :attachment, :pages,
17
+
18
18
  ### Associations:
19
19
  :categories, :venue,
20
-
20
+
21
21
  ### Utility:
22
22
  :created_at, :updated_at
23
-
23
+
24
24
  lazy_load :venue
25
-
25
+
26
26
  def managed?
27
27
  type == 'managed' || categories.present?
28
28
  end
29
-
29
+
30
30
  def download?
31
31
  type == 'download'
32
32
  end
33
-
33
+
34
34
  def venue=(venue_attributes)
35
35
  @venue = Hungry::Venue.new(venue_attributes)
36
36
  end
37
-
37
+
38
38
  def categories
39
39
  @categories ||= []
40
40
  end
41
41
  lazy_load :categories
42
-
42
+
43
43
  def categories=(new_categories)
44
44
  @categories = new_categories.map do |attributes|
45
45
  category = Menu::Category.new(attributes)
@@ -48,6 +48,6 @@ module Hungry
48
48
  category
49
49
  end
50
50
  end
51
-
51
+
52
52
  end
53
53
  end
@@ -1,13 +1,13 @@
1
1
  module Hungry
2
2
  class Menu
3
3
  class Category < Resource
4
-
4
+
5
5
  attr_accessor :id, :name, :dishes, :menu
6
-
6
+
7
7
  def dishes
8
8
  @dishes ||= []
9
9
  end
10
-
10
+
11
11
  def dishes=(new_dishes)
12
12
  @dishes = new_dishes.map do |attributes|
13
13
  dish = Menu::Dish.new(attributes)
@@ -16,7 +16,7 @@ module Hungry
16
16
  dish
17
17
  end
18
18
  end
19
-
19
+
20
20
  end
21
21
  end
22
22
  end
@@ -1,13 +1,13 @@
1
1
  module Hungry
2
2
  class Menu
3
3
  class Dish < Resource
4
-
4
+
5
5
  attr_accessor :name, :price, :description, :options, :photos, :category
6
-
6
+
7
7
  def options
8
8
  @options ||= []
9
9
  end
10
-
10
+
11
11
  def options=(new_options)
12
12
  @options = new_options.map do |attributes|
13
13
  option = Menu::Option.new(attributes)
@@ -16,7 +16,7 @@ module Hungry
16
16
  option
17
17
  end
18
18
  end
19
-
19
+
20
20
  end
21
21
  end
22
22
  end
@@ -1,9 +1,9 @@
1
1
  module Hungry
2
2
  class Menu
3
3
  class Option < Resource
4
-
4
+
5
5
  attr_accessor :description, :price, :dish
6
-
6
+
7
7
  end
8
8
  end
9
9
  end
@@ -1,7 +1,7 @@
1
1
  module Hungry
2
2
  class Region < Location
3
-
3
+
4
4
  self.default_criteria = { type: 'Region' }
5
-
5
+
6
6
  end
7
7
  end
@@ -3,27 +3,28 @@ require 'httparty' unless defined? HTTParty
3
3
  module Hungry
4
4
  class Resource
5
5
  include HTTParty
6
-
6
+
7
7
  attr_accessor :attributes, :resources, :data_source
8
-
8
+
9
9
  ### CLASS METHODS:
10
-
10
+
11
11
  def self.get(*args)
12
12
  self.base_uri Hungry.api_url
13
+
13
14
  super
14
15
  end
15
-
16
- def self.belongs_to(resource, klass = 'Resource')
17
- define_method resource do
16
+
17
+ def self.belongs_to(association, klass = 'Resource')
18
+ define_method association do
18
19
  @belongs_to ||= {}
19
- @belongs_to[resource] ||= begin
20
+ @belongs_to[association] ||= begin
20
21
  klass = Kernel.const_get(klass) if klass.is_a?(String)
21
-
22
- if attributes[resource].present?
23
- resource = klass.new attributes[resource]
22
+
23
+ if attributes[association].present?
24
+ resource = klass.new attributes[association]
24
25
  resource.data_source = data_source
25
26
  resource
26
- elsif url = resources[resource]
27
+ elsif url = resources[association]
27
28
  attributes = self.class.get url
28
29
  resource = klass.new attributes
29
30
  resource.data_source = url
@@ -31,33 +32,49 @@ module Hungry
31
32
  end
32
33
  end
33
34
  end
35
+
36
+ define_method "#{association}=" do |object_or_attributes|
37
+ @belongs_to ||= {}
38
+
39
+ klass = Kernel.const_get(klass) if klass.is_a?(String)
40
+
41
+ if object_or_attributes.is_a?(klass)
42
+ @belongs_to[association] = object_or_attributes
43
+ elsif object_or_attributes.present?
44
+ self.attributes.merge!(association => object_or_attributes)
45
+ else
46
+ @belongs_to[association] = nil
47
+ end
48
+
49
+ @belongs_to[association]
50
+ end
34
51
  end
35
-
36
- def self.has_many(resource, klass = 'Resource')
37
- define_method resource do
52
+
53
+ def self.has_many(association, klass = 'Resource')
54
+ define_method association do
38
55
  @has_many ||= {}
39
- @has_many[resource] ||= begin
56
+ @has_many[association] ||= begin
40
57
  klass = Kernel.const_get(klass) if klass.is_a?(String)
41
-
42
- if url = resources[resource]
58
+
59
+ if url = resources[association]
43
60
  collection = klass.collection.from_url(url)
44
-
45
- if attributes[resource].present?
61
+
62
+ if attributes[association].present?
46
63
  collection.results = attributes[resource]
47
64
  end
48
-
65
+
49
66
  collection
50
67
  end
51
68
  end
52
69
  end
53
70
  end
54
-
71
+
55
72
  def self.lazy_load(*attributes)
56
73
  attributes.each do |attribute|
57
74
  alias_method "#{attribute}_without_lazy_load".to_sym, attribute
58
75
  define_method attribute do
59
76
  result = send "#{attribute}_without_lazy_load".to_sym
60
-
77
+
61
78
  if !result.present? && (canonical_data_source != data_source && canonical_data_source.present?)
62
79
  reload
63
80
  send "#{attribute}_without_lazy_load".to_sym
@@ -67,17 +84,17 @@ module Hungry
67
84
  end
68
85
  end
69
86
  end
70
-
87
+
71
88
  class << self
72
89
  include Enumerable
73
-
90
+
74
91
  attr_writer :endpoint
75
92
  attr_writer :default_criteria
76
-
93
+
77
94
  def endpoint
78
95
  @endpoint || (superclass.respond_to?(:endpoint) ? superclass.endpoint : nil)
79
96
  end
80
-
97
+
81
98
  def default_criteria
82
99
  if @default_criteria.respond_to?(:call)
83
100
  @default_criteria.call
@@ -85,19 +102,19 @@ module Hungry
85
102
  @default_criteria || {}
86
103
  end
87
104
  end
88
-
105
+
89
106
  def collection
90
107
  Collection.new(self, endpoint, default_criteria)
91
108
  end
92
-
109
+
93
110
  def find(id)
94
111
  raise NoEndpointSpecified unless endpoint
95
-
112
+
96
113
  uri = "#{endpoint}/#{id}"
97
-
114
+
98
115
  Util.log "GET: #{uri}"
99
116
  response = get uri
100
-
117
+
101
118
  if response.code == 200
102
119
  attributes = Util.parse_json(response.body)
103
120
  resource = new(attributes)
@@ -105,56 +122,56 @@ module Hungry
105
122
  resource
106
123
  end
107
124
  end
108
-
125
+
109
126
  def first(n = 1)
110
127
  collection.first(n)
111
128
  end
112
-
129
+
113
130
  def all(criteria = {})
114
131
  collection.all(criteria)
115
132
  end
116
-
133
+
117
134
  def each(&block)
118
135
  all.each(&block)
119
136
  end
120
137
  end
121
-
138
+
122
139
  ### INSTANCE METHODS:
123
-
140
+
124
141
  def initialize(new_attributes = {})
125
142
  self.attributes = {}
126
-
143
+
127
144
  new_attributes.each do |key, value|
128
145
  if value.respond_to?(:symbolize_keys)
129
146
  value = value.symbolize_keys
130
147
  end
131
-
148
+
132
149
  attributes[key] = value
133
-
150
+
134
151
  if respond_to?("#{key}=")
135
152
  send("#{key}=", value)
136
153
  end
137
154
  end
138
155
  end
139
-
156
+
140
157
  def canonical_data_source
141
158
  resources && resources[:self]
142
159
  end
143
-
160
+
144
161
  def reload
145
162
  resource = self.class.find id
146
-
163
+
147
164
  if resource
148
165
  self.data_source = resource.data_source
149
166
  initialize resource.attributes
150
167
  else
151
168
  self.data_source = nil
152
- initialize Hash[*attributes.keys.map { |key| [key, nil] }]
169
+ initialize Hash[attributes.keys.map { |key| [key, nil] }]
153
170
  end
154
-
171
+
155
172
  @has_many = {}
156
173
  @belongs_to = {}
157
-
174
+
158
175
  self
159
176
  end
160
177
  end
@@ -1,17 +1,17 @@
1
1
  module Hungry
2
2
  class Response < Resource
3
-
3
+
4
4
  ### RESOURCES:
5
-
5
+
6
6
  belongs_to :review, 'Hungry::Review'
7
-
7
+
8
8
  ### ATTRIBUTES:
9
-
9
+
10
10
  ### Review:
11
11
  attr_accessor :id, :body, :author,
12
-
12
+
13
13
  ### Utility:
14
14
  :created_at, :updated_at
15
-
15
+
16
16
  end
17
17
  end
@@ -1,19 +1,19 @@
1
1
  module Hungry
2
2
  class Review < Resource
3
-
3
+
4
4
  ### RESOURCES:
5
-
5
+
6
6
  belongs_to :venue, 'Hungry::Venue'
7
-
7
+
8
8
  has_many :responses, 'Hungry::Response'
9
-
9
+
10
10
  ### ATTRIBUTES:
11
-
11
+
12
12
  ### Review:
13
13
  attr_accessor :id, :body, :rating, :scores, :author,
14
-
14
+
15
15
  ### Utility:
16
16
  :created_at, :updated_at
17
-
17
+
18
18
  end
19
19
  end
@@ -1,36 +1,37 @@
1
1
  module Hungry
2
2
  class Site < Resource
3
-
3
+
4
4
  self.endpoint = '/sites'
5
-
5
+
6
6
  ### FINDERS:
7
-
7
+
8
8
  def self.with_hostname(hostname)
9
9
  collection.all(hostname: hostname).first
10
10
  end
11
-
11
+
12
12
  def self.for_country(country)
13
13
  collection.all(country: country.id).first
14
14
  end
15
-
15
+
16
16
  def self.default_site
17
17
  collection.all(default: true).first
18
18
  end
19
-
20
-
19
+
20
+
21
21
  ### ATTRIBUTES:
22
-
22
+
23
23
  ### Preview:
24
24
  attr_accessor :id, :name, :title, :subtitle, :identifier, :default, :locale,
25
- :url, :email, :support_email, :timezone, :country, :applications,
26
-
25
+ :url, :email, :support_email, :timezone, :country,
26
+ :newsletter_list, :applications, :emails,
27
+
27
28
  ### Utility:
28
29
  :resources, :counters
29
-
30
+
30
31
  def hostname
31
32
  uri = URI.parse(url) rescue nil
32
33
  uri && uri.hostname
33
34
  end
34
-
35
+
35
36
  end
36
37
  end