PostmatesRuby 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02f5131e6b17ffa920de136647548cb2f52e5b5e
4
+ data.tar.gz: d486955945ab482d8863a768e4d074d503c5b52b
5
+ SHA512:
6
+ metadata.gz: 7de42deecd0a1fa0d25ed9a6bd9554361c0c98cfd00c288415ae36266582267ab3e1a0c8edcfe6639fbeb674f9bd4fc50df7b87969eabcabe0b07342b1953460
7
+ data.tar.gz: 58f08fcc6f669f8e94c84f5eac04af3fac31549d74fe940bc44799ec8d7a1b04c9d508c0079eb4167e7f1b3db8c98f5f162f6418a3d313e7164bade98eb8ed73
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea/
24
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Postmates/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "PostmatesRuby"
8
+ spec.version = Postmates::VERSION
9
+ spec.authors = ["Matt Dunbar"]
10
+ spec.email = ["matt@buildrx.com"]
11
+ spec.summary = 'Ruby Gem providing a wrapper for the Postmates API.'
12
+ spec.homepage = ""
13
+
14
+ spec.add_runtime_dependency('faraday', ['>= 0.7', '< 0.10'])
15
+ spec.add_runtime_dependency('faraday_middleware', ['>= 0.8', '< 0.10'])
16
+ spec.add_runtime_dependency('multi_json', '>= 1.0.3', '~> 1.0')
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Postmates
2
+
3
+ Overview here...
4
+
5
+ ## Usage
6
+
7
+ Usage example(s) here...
8
+
9
+ ## Legal
10
+
11
+ Portions of the codebase are from Instagram's Ruby Gem.
12
+
13
+ As a result, the following may apply to parts or all of this gem:
14
+ https://github.com/Instagram/instagram-ruby-gem/blob/master/PATENTS.md
15
+ https://github.com/Instagram/instagram-ruby-gem/blob/master/LICENSE.md
16
+
17
+ Consult your own legal counsel for clarification.
18
+
19
+ Instagram Ruby Gem's remnants:
20
+ Copyright (c) 2014, Facebook, Inc. All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ require 'Postmates/connection'
2
+ require 'Postmates/request'
3
+
4
+ module Postmates
5
+ class Client
6
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
7
+
8
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
9
+
10
+ def initialize(options={})
11
+ options = Postmates.options.merge(options)
12
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
13
+ send("#{key}=", options[key])
14
+ end
15
+ end
16
+
17
+ include Postmates::Connection
18
+ include Postmates::Request
19
+
20
+ include Postmates::Client::Delivery
21
+ include Postmates::Client::DeliveryQuote
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module Postmates
2
+ class Client
3
+ module Delivery
4
+ def create_delivery(options={})
5
+ post('deliveries', options)
6
+ end
7
+ def poll_delivery(delivery_id)
8
+ get("deliveries/#{delivery_id}")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Postmates
2
+ class Client
3
+ module DeliveryQuote
4
+ def delivery_quote(options={})
5
+ post('delivery_quotes', options) # @TODO merge options with auth
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ require 'faraday'
2
+ require 'Postmates/version'
3
+
4
+ module Postmates
5
+ module Configuration
6
+ VALID_OPTIONS_KEYS = [
7
+ :customer_id,
8
+ :api_key,
9
+ :adapter,
10
+ :endpoint,
11
+ :api_version
12
+ ].freeze
13
+
14
+ DEFAULT_CUSTOMER_ID = nil
15
+ DEFAULT_API_KEY = nil
16
+ DEFAULT_ADAPTER = Faraday.default_adapter
17
+ DEFAULT_ENDPOINT = 'https://api.postmates.com/v1/'.freeze
18
+ DEFAULT_API_VERSION = '20150117'.freeze
19
+
20
+ attr_accessor *VALID_OPTIONS_KEYS
21
+
22
+ def self.extended(base)
23
+ base.reset
24
+ end
25
+
26
+ def configure
27
+ yield self
28
+ end
29
+
30
+ def options
31
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
32
+ option.merge!(key => send(key))
33
+ end
34
+ end
35
+
36
+ def reset
37
+ self.customer_id = DEFAULT_CUSTOMER_ID
38
+ self.api_key = DEFAULT_API_KEY
39
+ self.adapter = DEFAULT_ADAPTER
40
+ self.endpoint = DEFAULT_ENDPOINT
41
+ self.api_version = DEFAULT_API_VERSION
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module Postmates
5
+ module Connection
6
+ private
7
+ def connection
8
+ conn = Faraday.new(:url => "#{endpoint}customers/#{customer_id}/") do |c|
9
+ c.use Faraday::Request::UrlEncoded
10
+ c.use FaradayMiddleware::Mashify
11
+ c.use Faraday::Response::ParseJson
12
+ c.use FaradayMiddleware::RaiseHttpException
13
+ c.adapter(adapter)
14
+ end
15
+ conn
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Postmates
2
+ # Custom error class for rescuing from all errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Postmates returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Postmates returns the HTTP status code 401
9
+ class Unauthorized < Error; end
10
+
11
+ # Raised when Postmates returns the HTTP status code 404
12
+ class NotFound < Error; end
13
+
14
+ # Raised when Postmates returns the HTTP status code 500
15
+ class InternalServerError < Error; end
16
+
17
+ # Raised when Postmates returns the HTTP status code 503
18
+ class ServiceUnavailable < Error; end
19
+ end
@@ -0,0 +1,16 @@
1
+ module Postmates
2
+ module Objects
3
+ class Base
4
+ @@OBJECT_FIELDS = []
5
+ attr_accessor *@@OBJECT_FIELDS
6
+
7
+ def initialize data
8
+ data.each do |k,v|
9
+ if @@OBJECT_FIELDS.include?(k.to_sym)
10
+ send("#{k}=", v)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Postmates
2
+ module Objects
3
+ class Delivery < Postmates::Objects::Base
4
+ @@OBJECT_FIELDS = [
5
+ :created,
6
+ :updated,
7
+ :status,
8
+ :complete,
9
+ :pickup_eta,
10
+ :dropoff_eta,
11
+ :dropoff_deadline,
12
+ :quote_id,
13
+ :fee,
14
+ :currency,
15
+ :manifest,
16
+ :pickup,
17
+ :dropoff,
18
+ :courier
19
+ ].freeze
20
+ attr_accessor *@@OBJECT_FIELDS
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ module Postmates
2
+ module Objects
3
+ class DeliveryQuote < Postmates::Objects::Base
4
+ @@OBJECT_FIELDS = [
5
+ :id,
6
+ :created,
7
+ :expires,
8
+ :fee,
9
+ :currency,
10
+ :dropoff_eta,
11
+ :duration
12
+ ].freeze
13
+ attr_accessor *@@OBJECT_FIELDS
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ require 'Postmates/response'
2
+
3
+ module Postmates
4
+ # Defines HTTP request methods
5
+ module Request
6
+ # Perform an HTTP GET request
7
+ def get(path, options={})
8
+ request(:get, path, options)
9
+ end
10
+
11
+ # Perform an HTTP POST request
12
+ def post(path, options={})
13
+ request(:post, path, options)
14
+ end
15
+
16
+ # Perform an HTTP PUT request
17
+ def put(path, options={})
18
+ request(:put, path, options)
19
+ end
20
+
21
+ # Perform an HTTP DELETE request
22
+ def delete(path, options={})
23
+ request(:delete, path, options)
24
+ end
25
+
26
+ private
27
+
28
+ # Perform an HTTP request
29
+ def request(method, path, options)
30
+ conn = connection
31
+ conn.basic_auth(api_key, '')
32
+ response = conn.send(method) do |request|
33
+ case method
34
+ when :get, :delete
35
+ request.url(path, options)
36
+ when :post, :put
37
+ request.path = path
38
+ request.body = options unless options.empty?
39
+ end
40
+ end
41
+ Response.create( response.body )
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+ require 'Postmates/error'
3
+ Dir[File.expand_path('../objects/*.rb', __FILE__)].each{|f| require f}
4
+ module Postmates
5
+ module Response
6
+ def self.create( object )
7
+ parse_object object
8
+ end
9
+
10
+ private
11
+ def self.parse_object object
12
+ case object['kind']
13
+ when 'delivery'
14
+ Postmates::Objects::Delivery.new(object)
15
+ when 'delivery_quote'
16
+ Postmates::Objects::DeliveryQuote.new(object)
17
+ when 'list'
18
+ object['list'].map{|obj| parse_object(obj)}
19
+ when 'error'
20
+ raise Postmates::Error, "#{object['code']}: #{object['message']} ---- #{object.inspect}"
21
+ else
22
+ raise Postmates::Error, 'Invalid Response Kind'
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Postmates
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'Postmates/version'
2
+ require 'Postmates/configuration'
3
+ require 'Postmates/client'
4
+
5
+ module Postmates
6
+ extend Configuration
7
+
8
+ def self.client(options={})
9
+ Postmates::Client.new(options)
10
+ end
11
+
12
+ def self.method_missing(method, *args, &block)
13
+ return super unless client.respond_to?(method)
14
+ client.send(method, *args, &block)
15
+ end
16
+
17
+ def self.respond_to?(method, include_all=false)
18
+ return client.respond_to?(method, include_all) || super
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module FaradayMiddleware
5
+ # @private
6
+ class RaiseHttpException < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env).on_complete do |response|
9
+ case response[:status].to_i
10
+ when 400
11
+ raise Postmates::BadRequest, error_message_400(response)
12
+ when 401
13
+ raise Postmates::Unauthorized, error_message_400(response)
14
+ when 404
15
+ raise Postmates::NotFound, error_message_400(response)
16
+ when 500
17
+ raise Postmates::InternalServerError, error_message_500(response, "Something is technically wrong.")
18
+ when 503
19
+ raise Postmates::ServiceUnavailable, error_message_500(response, "Service is currently unavailable.")
20
+ end
21
+ end
22
+ end
23
+
24
+ def initialize(app)
25
+ super app
26
+ @parser = nil
27
+ end
28
+
29
+ private
30
+
31
+ def error_message_400(response)
32
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
33
+ end
34
+
35
+ def error_body(body)
36
+ # body gets passed as a string, not sure if it is passed as something else from other spots?
37
+ if not body.nil? and not body.empty? and body.kind_of?(String)
38
+ # removed multi_json thanks to wesnolte's commit
39
+ body = ::JSON.parse(body)
40
+ end
41
+
42
+ if body.nil?
43
+ nil
44
+ elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
45
+ ": #{body['meta']['error_message']}"
46
+ elsif body['error_message'] and not body['error_message'].empty?
47
+ ": #{body['error_type']}: #{body['error_message']}"
48
+ end
49
+ end
50
+
51
+ def error_message_500(response, body=nil)
52
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: PostmatesRuby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Dunbar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.10'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.10'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday_middleware
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.10'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0.8'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '0.10'
53
+ - !ruby/object:Gem::Dependency
54
+ name: multi_json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.3
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.3
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: bundler
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.6'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '1.6'
87
+ - !ruby/object:Gem::Dependency
88
+ name: rake
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ description:
102
+ email:
103
+ - matt@buildrx.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".gitignore"
109
+ - Gemfile
110
+ - PostmatesRuby.gemspec
111
+ - README.md
112
+ - Rakefile
113
+ - lib/Postmates/client.rb
114
+ - lib/Postmates/client/delivery.rb
115
+ - lib/Postmates/client/delivery_quote.rb
116
+ - lib/Postmates/configuration.rb
117
+ - lib/Postmates/connection.rb
118
+ - lib/Postmates/error.rb
119
+ - lib/Postmates/objects/base.rb
120
+ - lib/Postmates/objects/delivery.rb
121
+ - lib/Postmates/objects/delivery_quote.rb
122
+ - lib/Postmates/request.rb
123
+ - lib/Postmates/response.rb
124
+ - lib/Postmates/version.rb
125
+ - lib/PostmatesRuby.rb
126
+ - lib/faraday/raise_http_exception.rb
127
+ homepage: ''
128
+ licenses: []
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.2.2
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Ruby Gem providing a wrapper for the Postmates API.
150
+ test_files: []