hungry 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,23 +3,23 @@ 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
14
  super
15
15
  end
16
-
16
+
17
17
  def self.belongs_to(resource, klass = 'Resource')
18
18
  define_method resource do
19
19
  @belongs_to ||= {}
20
20
  @belongs_to[resource] ||= begin
21
21
  klass = Kernel.const_get(klass) if klass.is_a?(String)
22
-
22
+
23
23
  if attributes[resource].present?
24
24
  resource = klass.new attributes[resource]
25
25
  resource.data_source = data_source
@@ -32,33 +32,47 @@ module Hungry
32
32
  end
33
33
  end
34
34
  end
35
+
36
+ define_method "#{resource}=" 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[resource] = object_or_attributes
43
+ else
44
+ @belongs_to[resource] = klass.new object_or_attributes
45
+ end
46
+
47
+ @belongs_to[resource]
48
+ end
35
49
  end
36
-
50
+
37
51
  def self.has_many(resource, klass = 'Resource')
38
52
  define_method resource do
39
53
  @has_many ||= {}
40
54
  @has_many[resource] ||= begin
41
55
  klass = Kernel.const_get(klass) if klass.is_a?(String)
42
-
56
+
43
57
  if url = resources[resource]
44
58
  collection = klass.collection.from_url(url)
45
-
59
+
46
60
  if attributes[resource].present?
47
61
  collection.results = attributes[resource]
48
62
  end
49
-
63
+
50
64
  collection
51
65
  end
52
66
  end
53
67
  end
54
68
  end
55
-
69
+
56
70
  def self.lazy_load(*attributes)
57
71
  attributes.each do |attribute|
58
72
  alias_method "#{attribute}_without_lazy_load".to_sym, attribute
59
73
  define_method attribute do
60
74
  result = send "#{attribute}_without_lazy_load".to_sym
61
-
75
+
62
76
  if !result.present? && (canonical_data_source != data_source && canonical_data_source.present?)
63
77
  reload
64
78
  send "#{attribute}_without_lazy_load".to_sym
@@ -68,17 +82,17 @@ module Hungry
68
82
  end
69
83
  end
70
84
  end
71
-
85
+
72
86
  class << self
73
87
  include Enumerable
74
-
88
+
75
89
  attr_writer :endpoint
76
90
  attr_writer :default_criteria
77
-
91
+
78
92
  def endpoint
79
93
  @endpoint || (superclass.respond_to?(:endpoint) ? superclass.endpoint : nil)
80
94
  end
81
-
95
+
82
96
  def default_criteria
83
97
  if @default_criteria.respond_to?(:call)
84
98
  @default_criteria.call
@@ -86,19 +100,19 @@ module Hungry
86
100
  @default_criteria || {}
87
101
  end
88
102
  end
89
-
103
+
90
104
  def collection
91
105
  Collection.new(self, endpoint, default_criteria)
92
106
  end
93
-
107
+
94
108
  def find(id)
95
109
  raise NoEndpointSpecified unless endpoint
96
-
110
+
97
111
  uri = "#{endpoint}/#{id}"
98
-
112
+
99
113
  Util.log "GET: #{uri}"
100
114
  response = get uri
101
-
115
+
102
116
  if response.code == 200
103
117
  attributes = Util.parse_json(response.body)
104
118
  resource = new(attributes)
@@ -106,56 +120,56 @@ module Hungry
106
120
  resource
107
121
  end
108
122
  end
109
-
123
+
110
124
  def first(n = 1)
111
125
  collection.first(n)
112
126
  end
113
-
127
+
114
128
  def all(criteria = {})
115
129
  collection.all(criteria)
116
130
  end
117
-
131
+
118
132
  def each(&block)
119
133
  all.each(&block)
120
134
  end
121
135
  end
122
-
136
+
123
137
  ### INSTANCE METHODS:
124
-
138
+
125
139
  def initialize(new_attributes = {})
126
140
  self.attributes = {}
127
-
141
+
128
142
  new_attributes.each do |key, value|
129
143
  if value.respond_to?(:symbolize_keys)
130
144
  value = value.symbolize_keys
131
145
  end
132
-
146
+
133
147
  attributes[key] = value
134
-
148
+
135
149
  if respond_to?("#{key}=")
136
150
  send("#{key}=", value)
137
151
  end
138
152
  end
139
153
  end
140
-
154
+
141
155
  def canonical_data_source
142
156
  resources && resources[:self]
143
157
  end
144
-
158
+
145
159
  def reload
146
160
  resource = self.class.find id
147
-
161
+
148
162
  if resource
149
163
  self.data_source = resource.data_source
150
164
  initialize resource.attributes
151
165
  else
152
166
  self.data_source = nil
153
- initialize Hash[*attributes.keys.map { |key| [key, nil] }]
167
+ initialize Hash[attributes.keys.map { |key| [key, nil] }]
154
168
  end
155
-
169
+
156
170
  @has_many = {}
157
171
  @belongs_to = {}
158
-
172
+
159
173
  self
160
174
  end
161
175
  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,
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
@@ -1,19 +1,19 @@
1
1
  module Hungry
2
2
  class Tag < Resource
3
-
3
+
4
4
  self.endpoint = '/tags'
5
-
5
+
6
6
  ### RESOURCES:
7
-
7
+
8
8
  has_many :venues, 'Hungry::Venue'
9
-
9
+
10
10
  has_many :tags, 'Hungry::Tag'
11
-
11
+
12
12
  ### ATTRIBUTES:
13
-
13
+
14
14
  ### Tag:
15
15
  attr_accessor :id, :name, :context,
16
-
16
+
17
17
  ### Utility:
18
18
  :resources, :counters
19
19
  end
@@ -1,77 +1,77 @@
1
1
  module Hungry
2
2
  class User < Resource
3
-
3
+
4
4
  self.endpoint = '/users'
5
-
5
+
6
6
  ### ASSOCIATIONS:
7
-
7
+
8
8
  has_many :reviews, 'Hungry::Review'
9
-
9
+
10
10
  ### FINDERS:
11
-
11
+
12
12
  def self.with_ip(ip_address)
13
13
  collection.all(ip: ip_address)
14
14
  end
15
-
15
+
16
16
  ### CLASS METHODS:
17
-
17
+
18
18
  def self.authenticate_with_persistence_token(persistence_token)
19
19
  result = authenticate_via_api 'Cookie' => "persistence_token=#{persistence_token}"
20
20
  return false unless result.present?
21
-
21
+
22
22
  from_json(result)
23
23
  end
24
-
24
+
25
25
  def self.authenticate_with_perishable_token(perishable_token)
26
26
  result = authenticate_via_api query: "perishable_token=#{perishable_token}"
27
27
  return false unless result.present?
28
-
28
+
29
29
  from_json(result)
30
30
  end
31
-
31
+
32
32
  def self.from_json(json)
33
33
  new Util.parse_json(json)
34
34
  end
35
-
35
+
36
36
  def self.authenticate_via_api(options = {})
37
37
  options = {
38
38
  path: '/account.json',
39
39
  query: nil
40
40
  }.merge(options)
41
-
41
+
42
42
  url = URI.parse(Hungry.api_url)
43
43
  url.path = options.delete(:path)
44
44
  url.query = options.delete(:query)
45
-
45
+
46
46
  http = Net::HTTP.new(url.host, url.port)
47
47
  if url.scheme == 'https'
48
48
  http.use_ssl = true
49
49
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
50
  end
51
-
51
+
52
52
  request = Net::HTTP::Get.new(url.request_uri, options)
53
53
  response = http.request request
54
-
54
+
55
55
  if response.code.to_i == 200
56
56
  response.body
57
57
  else
58
58
  false
59
59
  end
60
60
  end
61
-
61
+
62
62
  ### ATTRIBUTES:
63
-
63
+
64
64
  ### Preview:
65
65
  attr_accessor :id, :name, :avatar, :slug, :profile_url, :score, :bio,
66
66
  :location, :email,
67
-
67
+
68
68
  ### FULL:
69
69
  :gender, :birth_date, :website_url, :persistence_token,
70
70
  :single_access_token, :maintainer_of, :connections,
71
71
  :device_tokens, :admin,
72
-
72
+
73
73
  ### Utility:
74
74
  :resources, :counters, :created_at, :updated_at, :activated_at
75
-
75
+
76
76
  end
77
77
  end