refreshingmenus_api 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,10 +19,14 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  api = RefreshingmenusApi::Client.new(:auth_token => 'your_auth_token_here')
22
- result = api.places(:q => "o'pazzo") # Returns Array or results
23
- result.first[:name] # => "O'Pazzo Pizzeria"
24
- result.first[:id] # => "194ebdcc-fcd9-11e1-b4ac-5254006b3bb5"
25
- result.first[:menus].first[:title] # => "Pizzeria Kaart"
22
+
23
+ ### Places
24
+
25
+ result = api.places(:q => "o'pazzo") # Returns Array of RefreshingmenusApi::Place objects
26
+ result.first.name # => "O'Pazzo Pizzeria"
27
+ result.first.guid # => "194ebdcc-fcd9-11e1-b4ac-5254006b3bb5"
28
+ result.first.menus_count # => 2
29
+ result.first.menus.first.title # => "Pizzeria Kaart"
26
30
 
27
31
  A query to match businesses / venues / places based on a phone number:
28
32
 
@@ -30,8 +34,8 @@ A query to match businesses / venues / places based on a phone number:
30
34
  :phone => '+31-(0)-10-2065151',
31
35
  :country_code => 'NL'
32
36
  )
33
- result.first[:normalized_phone] # => "31102065151"
34
- result.first[:id] # => "194e9016-fcd9-11e1-b4ac-5254006b3bb5"
37
+ result.first.normalized_phone # => "31102065151"
38
+ result.first.guid # => "194e9016-fcd9-11e1-b4ac-5254006b3bb5"
35
39
 
36
40
  This phone number is normalized based on the country_code passed. So '010-2065151' and '+31-(0)-10-2065151' with country_code 'NL' will be normalized to '31102065151'.
37
41
 
@@ -46,6 +50,17 @@ Because sometimes multiple places have the same phone number we can do more spec
46
50
  :phone => place.phone_number # Eg. '010-12341234'.
47
51
  )
48
52
 
53
+ ### Menus
54
+
55
+ result = api.places(:q => "o'pazzo")
56
+ result.first.menus.first # => RefreshingmenusApi::Menu
57
+ menu1 = result.first.menus.first.fetch!(api) # Pass api client to fetch all Menu info
58
+ menu1.title # => "Pizzeria Kaart"
59
+ menu1.entries.first.title # => "ANTIPASTI"
60
+
61
+ A Menu is a tree of Entries. An Entry is either a Section (eg. 'Starters') or an Item (eg. 'Hamburger').
62
+ Both Sections and Items can have multiple Prices.
63
+
49
64
  ## Widget usage
50
65
 
51
66
  RefreshingmenusApi::Widget.script_tag(:place_id => 'some_place_id', :widget_token => 'some_token')
@@ -1,9 +1,7 @@
1
1
  require 'refreshingmenus_api/version'
2
2
  require 'refreshingmenus_api/widget'
3
3
 
4
- require 'httparty'
5
- require 'active_support/core_ext'
6
- require 'active_support/core_ext/hash'
4
+ require 'api_smith'
7
5
 
8
6
  module RefreshingmenusApi
9
7
 
@@ -14,6 +12,64 @@ module RefreshingmenusApi
14
12
  class UnprocessableEntityError < ApiError;
15
13
  end
16
14
 
15
+ class Menu < APISmith::Smash
16
+
17
+ class Entry < APISmith::Smash
18
+
19
+ class Price < APISmith::Smash
20
+ property :position
21
+ property :title
22
+ property :price
23
+ property :unit
24
+ property :currency
25
+ end
26
+
27
+ property :type
28
+ property :position
29
+ property :title
30
+ property :desc
31
+
32
+ # Item specific
33
+ property :restrictions
34
+ property :spiciness
35
+
36
+ # Price info
37
+ property :prices, :transformer => Entry::Price
38
+
39
+ # Sub-Entries
40
+ property :entries, :transformer => Entry
41
+ end
42
+
43
+ property :guid, :from => :id
44
+ property :title
45
+ property :desc
46
+ property :position
47
+ property :entries, :transformer => Menu::Entry
48
+
49
+ # FIXME: What is the proper way to do this?
50
+ def fetch!(client)
51
+ client.menu(self.guid)
52
+ end
53
+
54
+ end
55
+
56
+ class Place < APISmith::Smash
57
+ property :guid, :from => :id
58
+ property :name
59
+ property :street
60
+ property :zip
61
+ property :country_code
62
+ property :lat
63
+ property :lng
64
+ property :normalized_phone
65
+ property :menus_count
66
+ property :menus, :transformer => Menu
67
+ end
68
+
69
+ # class PlaceCollection < APISmith::Smash
70
+ # property :dates, :transformer => lambda { |c| c.map { |v| TallyStat.call(v['items']) }.flatten }
71
+ # end
72
+
17
73
  # Usage:
18
74
  # api = RefreshingmenusApi::Client.new(:auth_token => 'your_auth_token_here')
19
75
  # api.places(:q => 'some query')
@@ -25,47 +81,51 @@ module RefreshingmenusApi
25
81
  }
26
82
  ERRORS.default = ApiError
27
83
 
28
- include HTTParty
84
+ include APISmith::Client
29
85
 
30
- attr_reader :auth_token, :version, :format
86
+ attr_reader :auth_token, :version, :locale
31
87
 
32
88
  # Options:
33
89
  # * :auth_token - Your RefreshingMenus Authentication token (API)
34
90
  # * :version - Version (defaults to 1)
91
+ # * :locale - The language (defaults to 'nl')
35
92
  def initialize(options = {})
36
93
  @auth_token = options[:auth_token]
37
94
  @version = options[:version] || '1'
95
+ @locale = options[:locale] || 'nl'
96
+
38
97
  self.class.base_uri(options[:base_uri] || 'www.refreshingmenus.com')
98
+ self.class.endpoint("api/v#{version}")
99
+
100
+ add_query_options!(:auth_token => auth_token)
101
+ add_query_options!(:locale => locale)
39
102
  end
40
103
 
41
104
  def places(options)
42
- get('places.json', :query => options)
105
+ raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}." if not options.is_a?(Hash)
106
+ get('places.json', :extra_query => options, :transform => Place)
43
107
  end
44
108
 
45
- def get(*args)
46
- request :get, *args
109
+ def menu(guid)
110
+ raise ArgumentError, "Expected guid to be a String, got #(guid.inspect}." if not guid.is_a?(String)
111
+ get("menus/#{guid}.json", :transform => Menu)
47
112
  end
48
113
 
49
114
  private
50
-
51
- def request(method, path, options = {})
52
- options[:query] ||= {}
53
- options[:query][:auth_token] ||= auth_token
54
- path = File.join("/api/v#{version}", path)
55
- response = self.class.send(method, path, options)
56
-
115
+
116
+ def check_response_errors(response)
57
117
  # In 2XX range is success otherwise it's probably error (3XX redirects range is handled by HTTParty).
58
118
  # In case of error we lookup error class or default to ApiError.
59
- case response.code
60
- when 200..299
61
- result = response.parsed_response
62
- result = result.collect {|r| r.is_a?(Hash) ? r.with_indifferent_access : r} if result.is_a?(Array)
63
- return result
64
- else
65
- raise ERRORS[response.code], "Got HTTP code #{response.code} (#{response.message}) from API.\nPath: #{path} #{options.inspect}\nAuthToken: #{auth_token}"
119
+ if not (200..299).include?(response.code)
120
+ raise ERRORS[response.code], "Got HTTP code #{response.code} (#{response.message}) from API."
66
121
  end
67
- end
68
122
 
69
- end
123
+ # # TODO: Check JSON for error
124
+ # if response.first.is_a?(Hash) and (error = response.first['error'])
125
+ # raise Error.new(error)
126
+ # end
127
+ end
128
+
129
+ end # Client
70
130
 
71
131
  end
@@ -1,3 +1,3 @@
1
1
  module RefreshingmenusApi
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = RefreshingmenusApi::VERSION
17
17
 
18
- gem.add_dependency('httparty')
19
- gem.add_dependency('activesupport')
18
+ # gem.add_dependency('httparty')
19
+ # gem.add_dependency('activesupport')
20
+ gem.add_dependency('api_smith')
20
21
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joost Hietbrink
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-09-17 00:00:00 +02:00
17
+ date: 2012-09-18 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: httparty
21
+ name: api_smith
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -29,18 +29,6 @@ dependencies:
29
29
  version: "0"
30
30
  type: :runtime
31
31
  version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: activesupport
34
- prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
42
- type: :runtime
43
- version_requirements: *id002
44
32
  description: Ruby API to use the Refreshing Menus REST API.
45
33
  email:
46
34
  - joost@joopp.com