singleplatform 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2cec3922463afab967e8fd8cb232e18b38b03a5b
4
+ data.tar.gz: 4246d0e5f2c24643433faa462f324be89772c144
5
+ SHA512:
6
+ metadata.gz: 28a77401efa0875e85a135be5f7742f90df1a873fe2774028af577a3e658eaae98c017998f358521aaed54351b37f9a4047579a5cb8bb8e08016bf425f557cde
7
+ data.tar.gz: 8a331be44e7e42b30068d08bddac1c004e71dce901a5d2b3f8ce3b878258cd2c13115ad7efb990665f7a094226d79ea102bddc3712fe4042a15d53b95a72be91
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ .env
3
+ singleplatform-0.1.0.gem
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'bundler'
4
+ gem 'httparty'
5
+ gem 'hashie'
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ dotenv (2.1.1)
5
+ hashie (3.4.3)
6
+ httparty (0.14.0)
7
+ multi_xml (>= 0.5.2)
8
+ multi_xml (0.5.5)
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ bundler
15
+ dotenv
16
+ hashie
17
+ httparty
18
+
19
+ RUBY VERSION
20
+ ruby 2.2.2p95
21
+
22
+ BUNDLED WITH
23
+ 1.12.5
@@ -0,0 +1,84 @@
1
+ # SinglePlatform API Ruby Client
2
+ This is a wrapper for accessing SinglePlatform's API. Please see [SinglePlatform's official API documentation](http://docs.singleplatform.com/spv3/) for information on obtaining API credentials.
3
+
4
+ ## Installation
5
+ ```
6
+ gem install singleplatform
7
+ ```
8
+
9
+ Or add this to your applications Gemfile:
10
+ ```
11
+ gem 'singleplatform'
12
+ ```
13
+
14
+ And then run:
15
+
16
+ ```
17
+ bundle install
18
+ ```
19
+
20
+ ## Usage
21
+ ### Initializing an API Client
22
+ The gem uses a client model to query against the API. Create a client with your API credentials and make requests through that.
23
+
24
+ ```ruby
25
+ require 'singleplatform'
26
+
27
+ client = Singleplatform.new(
28
+ client_id: ENV['CLIENT_ID']
29
+ client_secret: ENV['CLIENT_SECRET']
30
+ )
31
+ ```
32
+
33
+ After creating a client you're able to make requests to SinglePlatform's API. The Client ID and Client Secret are required for each request.
34
+
35
+ ### Locations
36
+
37
+ With an initialized client, you can request information on locations in SinglePlatform's database.
38
+
39
+ #### Fetching Locations by ID
40
+
41
+ ```ruby
42
+ client.location('nobu')
43
+ ```
44
+
45
+ This will return a Hashie::Mash pseudo object, which allows you to access attributes on the response using dot notation. Nested attributes continue to return Hashie::Mash objects until there are no children.
46
+
47
+ ```ruby
48
+ location = client.location('nobu') # => #<Hashie::Mash ... >
49
+ location.name # => "Nobu"
50
+ location.attributes # => #<Hashie::Mash ... >
51
+ location.attributes.drive_thru # => false
52
+ ```
53
+
54
+ See SinglePlatform's API documentation for a full list of attributes.
55
+
56
+ #### Fetching Locations Updated Since a Given Date
57
+
58
+ You can retrieve locations en masse by calling:
59
+
60
+ ```ruby
61
+ client.locations_updated_since('2016-08-01', limit: 100)
62
+ ```
63
+
64
+ Results are paginated. The maximum (and default) limit per page is 5000.
65
+
66
+ ### Menus
67
+ SinglePlatform locations have menus or lists of products and services that you can access with a configured API client. The following call returns an Array of Hashie::Mash objects.
68
+
69
+ ```ruby
70
+ menus = client.menus_for('nobu') # => #<Array ... >
71
+ menus.first.name # => "Dinner Menu"
72
+ ```
73
+
74
+ See SinglePlatform's API documentation for the Menu schema with a full list of attributes.
75
+
76
+ ### Photos
77
+ Many SinglePlatform locations have photos, both at the business and menu-item level. Returns an Array of Hashie::Mash objects.
78
+
79
+ ```ruby
80
+ photos = client.photos_for('no') # => #<Array ... >
81
+ photos.first.type # => "Product"
82
+ photos.first.url # => "http://xyz.cloudfront.net/.../39bf7671bc7d006f4cef72d94eee24aeec7615d2.jpg"
83
+ ```
84
+
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << ['test']
5
+ t.pattern = 'test/**/test_*.rb'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ require 'singleplatform/client'
2
+
3
+ module Singleplatform
4
+
5
+ class << self
6
+
7
+ # Alias for Singleplatform::Client.new
8
+ #
9
+ # @return [Singleplatform::Client]
10
+ def new(args = {})
11
+ Singleplatform::Client.new(args)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ require 'singleplatform/client/request'
2
+ require 'singleplatform/client/locations'
3
+ require 'singleplatform/client/menus'
4
+ require 'singleplatform/client/photos'
5
+ require 'cgi'
6
+ require 'uri'
7
+
8
+ module Singleplatform
9
+ class Client
10
+ attr_accessor :base_url, :client_id, :client_secret
11
+
12
+ BASE_URL = 'http://publishing-api.singleplatform.com'
13
+ CLIENT_ID = ENV['CLIENT_ID'].freeze
14
+ CLIENT_SECRET = ENV['CLIENT_SECRET'].freeze
15
+
16
+ include Singleplatform::Client::Locations
17
+ include Singleplatform::Client::Menus
18
+ include Singleplatform::Client::Photos
19
+
20
+ # Initializes a new API Client Object
21
+ #
22
+ # @return [Singleplatform::Client]
23
+ def initialize(args = {})
24
+ @base_url = BASE_URL
25
+ @client_id = args[:client_id]
26
+ @client_secret = args[:client_secret]
27
+ end
28
+
29
+ # Form the complete URL for a given endpoint
30
+ #
31
+ # @note Signature must be the last parameter
32
+ #
33
+ # @param path [String]
34
+ # @param params [Hash]
35
+ # @return [String]
36
+ def generate_url(path, params = {})
37
+ params['client'] = client_id
38
+ signature_base_string = "#{path}?#{URI.encode_www_form(params)}"
39
+ "#{base_url}#{signature_base_string}&signature=#{generate_signature(signature_base_string)}"
40
+ end
41
+
42
+ # Calculate the signature, Base64 and URL encode it
43
+ #
44
+ # @param base_string [String]
45
+ # @return [String]
46
+ def generate_signature(base_string)
47
+ key = OpenSSL::HMAC.digest('sha1', client_secret, base_string)
48
+ CGI::escape(Base64.encode64(key).chomp)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ module Singleplatform
2
+ class Client
3
+ module Locations
4
+ # Fetch information about a specific location
5
+ #
6
+ # @param id [String]
7
+ # @param options [Hash]
8
+ # @option options [String] :format Short menu ('short') available
9
+ # @return [Hashie::Mash]
10
+ def location(id, options = {})
11
+ url = generate_url("/locations/#{id}/", options)
12
+ Request.get(url)
13
+ end
14
+
15
+ # Fetch all locations updated since a given date
16
+ #
17
+ # @param date [String]
18
+ # @param options [Hash]
19
+ # @option options [Fixnum] :limit Maximum (default) 5000 per page
20
+ # @return [Hashie::Mash]
21
+ def locations_updated_since(date, options = {})
22
+ url = generate_url(
23
+ '/locations/updated_since/',
24
+ { date: date }.merge(options)
25
+ )
26
+ Request.get(url)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module Singleplatform
2
+ class Client
3
+ module Menus
4
+ # Fetch all menus for a given location
5
+ #
6
+ # @param id [String]
7
+ # @param options [Hash]
8
+ # @option options [String] :format Short menu available ('short')
9
+ # @return [Hashie::Mash]
10
+ def menus_for(id, options = {})
11
+ url = generate_url("/locations/#{id}/menus", options)
12
+ Request.get(url)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Singleplatform
2
+ class Client
3
+ module Photos
4
+ # Fetch photos for a given location
5
+ #
6
+ # @param date [String]
7
+ # @param options [Hash]
8
+ # @option options [Fixnum] :height
9
+ # @option options [Fixnum] :width
10
+ # @option options [String] :type (interior, exterior, item, logo, uncategorized)
11
+ # @return [Hashie::Mash]
12
+ def photos_for(id, options = {})
13
+ url = generate_url("/location/#{id}/photos", options)
14
+ Request.get(url)
15
+ end
16
+
17
+ # Fetch photos added/updated since a given date
18
+ #
19
+ # @param date [String]
20
+ # @return [Hashie::Mash]
21
+ def photos_updated_since(date)
22
+ url = generate_url('/photos/updated_since/', date: date)
23
+ Request.get(url)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'httparty'
2
+ require 'hashie'
3
+
4
+ module Singleplatform
5
+ class Request
6
+ # Make an HTTP get request to given URL
7
+ #
8
+ # @param url [String]
9
+ # @return [Hashie::Mash]
10
+ def self.get(url)
11
+ tries ||= 3
12
+ response = HTTParty.get(url)
13
+ rescue
14
+ sleep 3
15
+ if tries -= 1 > 0
16
+ retry
17
+ else
18
+ nil
19
+ end
20
+ else
21
+ Hashie::Mash.new(JSON.parse(response.body)).data
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Singleplatform
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'singleplatform/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "singleplatform"
8
+ spec.version = Singleplatform::VERSION
9
+ spec.authors = ["Jeff Gharakhanian"]
10
+ spec.email = ["jg@okayjeff.com"]
11
+
12
+ spec.summary = "A Ruby client library for accessing the SinglePlatform API."
13
+ spec.homepage = "http://github.com/okayjeff/singleplatform"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files`.split("\n")
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'minitest/autorun'
2
+ require 'singleplatform'
3
+
4
+ class Singleplatform::ClientTest < Minitest::Test
5
+ def test_urls_generate_correctly
6
+ client = Singleplatform::Client.new(
7
+ client_id: 'purplespacesuitfrogboots1',
8
+ client_secret: 'yellowsubmarinesresonatewithmeandmybestbros'
9
+ )
10
+ path = '/locations/updated_since/'
11
+ params = { date: '2016-08-01', limit: 100 }
12
+ url = client.generate_url(path, params)
13
+ assert_equal('http://publishing-api.singleplatform.com/locations/updated_since/?date=2016-08-01&limit=100&client=purplespacesuitfrogboots1&signature=D4mf0vL2jwJKU02OawPlXFXQymg%3D', url)
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require 'minitest/autorun'
2
+ require 'singleplatform'
3
+
4
+ class SingleplatformTest < Minitest::Test
5
+ def test_new_creates_new_client_object
6
+ client = Singleplatform.new(
7
+ client_id: 'purplespacesuitfrogboots1',
8
+ client_secret: 'yellowsubmarinesresonatewithmeandmybestbros'
9
+ )
10
+ assert_instance_of(Singleplatform::Client, client)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: singleplatform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Gharakhanian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - jg@okayjeff.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - README.md
52
+ - Rakefile
53
+ - lib/singleplatform.rb
54
+ - lib/singleplatform/client.rb
55
+ - lib/singleplatform/client/locations.rb
56
+ - lib/singleplatform/client/menus.rb
57
+ - lib/singleplatform/client/photos.rb
58
+ - lib/singleplatform/client/request.rb
59
+ - lib/singleplatform/version.rb
60
+ - singleplatform.gemspec
61
+ - test/singleplatform/test_client.rb
62
+ - test/test_singleplatform.rb
63
+ homepage: http://github.com/okayjeff/singleplatform
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A Ruby client library for accessing the SinglePlatform API.
87
+ test_files: []