single_platform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in single_platform.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Justin Ricaurte
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SinglePlatform
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'single_platform'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install single_platform
18
+
19
+ ## Usage
20
+
21
+ A simple api that allows you to easily query the single platform api.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ require "single_platform/version"
2
+
3
+ module SinglePlatform
4
+ def self.config
5
+ @config ||= Configuration.new
6
+ end
7
+ end
8
+
9
+ require 'active_support/core_ext/string/inflections'
10
+ require 'single_platform/configuration'
11
+ require 'single_platform/url_signing'
12
+ require 'single_platform/request'
13
+ require 'single_platform/location'
14
+ require 'single_platform/menu'
15
+ require 'single_platform/menu_section'
16
+ require 'single_platform/menu_item'
@@ -0,0 +1,3 @@
1
+ class SinglePlatform::Configuration
2
+ attr_accessor :client_id,:secret,:api_key
3
+ end
@@ -0,0 +1,42 @@
1
+ class SinglePlatform::Location
2
+
3
+ attr_reader :id, :factual_id, :out_of_business, :published_at, :general, :location, :phones, :hours, :business_type
4
+ attr_reader :menus
5
+
6
+ def self.in_zipcode(zipcode, opts={})
7
+ data = in_zipcode_request(zipcode, opts)
8
+ locations = parse_locations(data)
9
+ locations.each(&:fetch_menus) if opts[:include_menus]
10
+ locations
11
+ end
12
+
13
+ def initialize(data)
14
+ data.each do |key, value|
15
+ instance_variable_set :"@#{key.underscore}", value
16
+ end
17
+ end
18
+
19
+ def fetch_menus
20
+ @menus = SinglePlatform::Menu.menus_for_location(self)
21
+ end
22
+
23
+ private
24
+
25
+ def self.in_zipcode_request(zip_code, opts={})
26
+ response = SinglePlatform::Request.get "/locations/search", count: 1000, q: zip_code, updatedSince: "2011-01-01"
27
+ if response.body["total"] < 1000
28
+ response.body["results"]
29
+ else
30
+ data = response.body["results"]
31
+ (response.body["total"].to_i / 1000.0).floor.times do |i|
32
+ data << SinglePlatform::Request.get("/locations/search", count: 1000, q: zip_code, updatedSince: "2011-01-01", page: i+1).body["results"]
33
+ end
34
+ data
35
+ end
36
+ end
37
+
38
+ def self.parse_locations(data)
39
+ data.map{|result| new(result) }
40
+ end
41
+
42
+ end
@@ -0,0 +1,31 @@
1
+ class SinglePlatform::Menu
2
+ attr_reader :location, :menu_sections
3
+
4
+ attr_reader :title, :desc, :footnote, :state, :entries, :id, :name, :disclaimer, :attribution_image,
5
+ :attribution_image_link, :secure_attribution_image, :secure_attribution_image_link
6
+
7
+ def initialize(location, data)
8
+ @location = location
9
+ @menu_sections = []
10
+ data.each do |key, value|
11
+ instance_variable_set :"@#{key.underscore}", value
12
+ end
13
+ build_menu
14
+ end
15
+
16
+ def self.menus_for_location(location)
17
+ data = SinglePlatform::Request.get("/locations/#{location.id}/menu").body["menus"]
18
+ data.collect { |menu_data| new(location, menu_data) }
19
+ end
20
+
21
+ def build_menu
22
+ current_section = nil
23
+ entries.each do |entry|
24
+ if entry["type"] == "section"
25
+ current_section = SinglePlatform::MenuSection.new(self, entry)
26
+ else
27
+ current_section.menu_items << SinglePlatform::MenuItem.new(current_section, entry)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ class SinglePlatform::MenuItem
2
+
3
+ attr_reader :menu_section
4
+ attr_reader :title, :name, :type, :order_num, :desc, :allergens, :allergen_free, :restrictions, :spicy, :prices, :id
5
+
6
+ def initialize(menu_section, data)
7
+ @menu_section = menu_section
8
+ data.each do |key, value|
9
+ instance_variable_set :"@#{key.underscore}", value
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,12 @@
1
+ class SinglePlatform::MenuSection
2
+ attr_reader :menu, :menu_items
3
+ attr_reader :title, :name, :type, :order_num, :desc, :id
4
+
5
+ def initialize(menu, data)
6
+ @menu = menu
7
+ @menu_items = []
8
+ data.each do |key, value|
9
+ instance_variable_set :"@#{key.underscore}", value
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,42 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ class SinglePlatform::Request
5
+
6
+ def self.get(path, params={})
7
+ new.get(path, params)
8
+ end
9
+
10
+ def get(path, params={})
11
+ client.get do |req|
12
+ req.url path
13
+ req.headers['Accept'] = 'application/json'
14
+ params.each do |key, value|
15
+ req.params[key] = value
16
+ end
17
+ req.params['client'] = SinglePlatform.config.client_id
18
+ req.params['sig'] = signature(path, params)
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ def signature(path, params)
25
+ SinglePlatform::UrlSigning.make_signature(path, params, SinglePlatform.config.client_id, SinglePlatform.config.secret)
26
+ end
27
+
28
+ def domain
29
+ "http://api.singleplatform.co"
30
+ end
31
+
32
+ def client
33
+ @client ||= Faraday.new domain do |conn|
34
+ conn.use FaradayMiddleware::EncodeJson
35
+ conn.use FaradayMiddleware::ParseJson
36
+ conn.use FaradayMiddleware::Instrumentation
37
+ conn.use Faraday::Response::Logger
38
+ conn.adapter Faraday.default_adapter
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,23 @@
1
+ class SinglePlatform::UrlSigning
2
+ require 'rubygems'
3
+ require 'openssl'
4
+ require 'base64'
5
+
6
+ # from SinglePlatform sample ruby library
7
+ def self.make_signature(uri_path, params, client_id, secret)
8
+ padding_factor = (4 - secret.length % 4) % 4
9
+ secret += "=" * padding_factor
10
+ secret = secret.gsub(/[-_]/, {"-" => "+", "_" => "/"})
11
+ binary_key = Base64.decode64(secret)
12
+
13
+ params.update({"client" => client_id})
14
+ path = uri_path + "?" + params.collect{|k,v| "#{k}=#{v}"}.inject{|initial,cur| initial + "&" + cur}
15
+
16
+ #digest = HMAC::SHA1.new(binary_key).update(path).digest
17
+ digest = OpenSSL::HMAC.digest('sha1',binary_key, path)
18
+ digest = Base64.encode64(digest).gsub(/[+\/]/, {"+" => "-", "/" => "_"}).delete("=")
19
+ #return "#{path}&sig=#{digest}"
20
+ digest.delete("\n")
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ module SinglePlatform
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'single_platform/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "single_platform"
8
+ spec.version = SinglePlatform::VERSION
9
+ spec.authors = ["Justin Ricaurte"]
10
+ spec.email = ["justin@justinricaurte.com"]
11
+ spec.description = %q{A simple way to get data from the Single Platform api.}
12
+ spec.summary = %q{A simple way to get data from the Single Platform api.}
13
+ spec.homepage = "https://github.com/ricaurte/single_platform"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "faraday", "~> 0.8"
24
+ spec.add_dependency "faraday_middleware", "~> 0.9"
25
+ spec.add_dependency "active_support", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: single_platform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Ricaurte
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.8'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday_middleware
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.9'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ - !ruby/object:Gem::Dependency
79
+ name: active_support
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '3.0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '3.0'
94
+ description: A simple way to get data from the Single Platform api.
95
+ email:
96
+ - justin@justinricaurte.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/single_platform.rb
107
+ - lib/single_platform/configuration.rb
108
+ - lib/single_platform/location.rb
109
+ - lib/single_platform/menu.rb
110
+ - lib/single_platform/menu_item.rb
111
+ - lib/single_platform/menu_section.rb
112
+ - lib/single_platform/request.rb
113
+ - lib/single_platform/url_signing.rb
114
+ - lib/single_platform/version.rb
115
+ - single_platform.gemspec
116
+ homepage: https://github.com/ricaurte/single_platform
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: A simple way to get data from the Single Platform api.
141
+ test_files: []