passtools 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
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
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in passtools.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Tello, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,112 @@
1
+ # PassTools
2
+
3
+ Simple ruby wrapper for the PassTools API. API documentation can be
4
+ found at the main [PassTools API wiki](https://github.com/tello/passtools-api/wiki/Methods)
5
+
6
+ ## Installation
7
+
8
+ Via rubygems.org:
9
+
10
+ `$ gem install passtools`
11
+
12
+
13
+ Using Bundler:
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ `gem 'passtools'`
18
+
19
+ And then execute:
20
+
21
+ `$ bundle install`
22
+
23
+ ## Usage
24
+
25
+ The gem must be configured before any calls can be successfully made. At the very least, the api_key is required and needs to be set. You can configure multiple values from a hash:
26
+
27
+ `Passtools.configure(:api_key => 'i_am_an_api_key', :download_dir =
28
+ "/tmp")`
29
+
30
+ or from a yaml file:
31
+
32
+ `Passtools.configure_from_file('config.yml')`
33
+
34
+ You can also configure individual values directly via accessors:
35
+
36
+ `Passtools.api_key = "i_am_an_api_key"`
37
+
38
+ Once configured, there are 7 methods available to interact with the Passtools api. Returned JSON is parsed to provide Ruby data objects.
39
+
40
+ `Passtools::Template.list`
41
+
42
+ Returns all templates associated with user specified by the api_key
43
+
44
+ `Passtools::Template.show(template_id)`
45
+
46
+ Returns detail information for individual template
47
+
48
+ `Passtools::Pass.list`
49
+
50
+ Returns list of all Passes associated with user
51
+
52
+ `Passtools::Pass.show(pass_id)`
53
+
54
+ Returns detail information for individual pass
55
+
56
+ `Passtools::Pass.create(template_id, data)`
57
+
58
+ Creates a new Pass based on specified template. Data is a nested hash containing data for
59
+ fields, see data returned from the Pass.show call for structure.
60
+
61
+ `Passtools::Pass.update(pass_id, data)`
62
+
63
+ Updates pass data
64
+
65
+ `Passtools::Pass.download(pass_id)`
66
+
67
+ Downloads Pass to the directory named by the 'download_dir'
68
+ configuration value. Passes are named 'PassToolsPass.pkpass'
69
+
70
+ ## Example Code
71
+
72
+ Refer to the [PassTools API wiki](https://github.com/tello/passtools-api/wiki/Methods) for more information about the data returned. Generally you are most likely to be interested in data from the fieldsModel key of the Template detail data.
73
+
74
+ Assuming you had a template with id 5 that returned the following from
75
+ Passtools::Template.show(5)["fieldsModel"]
76
+
77
+ ```
78
+ {"first_name"=>
79
+ {"changeMessage"=>nil,
80
+ "fieldType"=>"PRIMARY",
81
+ "value"=>"John",
82
+ "label"=>"First Name",
83
+ "required"=>false},
84
+ "last_name"=>
85
+ {"changeMessage"=>nil,
86
+ "fieldType"=>"PRIMARY",
87
+ "value"=>"Doe",
88
+ "label"=>"Last Name",
89
+ "required"=>false}
90
+ }
91
+ ```
92
+
93
+ You could create new Pass with:
94
+
95
+ ```
96
+ data = {"first_name" => { "value" => "Abraham" },
97
+ "last_name" => { "value" => "Lincoln" } }
98
+ Passtools::Pass.create(5, data)
99
+ ```
100
+
101
+ You could use the same hash with the update call to edit values in an
102
+ existing Pass based on the same template.
103
+
104
+
105
+
106
+ ## Contributing
107
+
108
+ 1. Fork it
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+
5
+ desc 'pry console'
6
+ task :console do
7
+ exec('bundle exec pry -I lib -r passtools.rb')
8
+ end
@@ -0,0 +1,34 @@
1
+ require "passtools/version"
2
+
3
+ require 'rest_client'
4
+ require 'multi_json'
5
+ require 'yaml'
6
+
7
+ require 'passtools/request'
8
+ require 'passtools/template'
9
+ require 'passtools/pass'
10
+
11
+ module Passtools
12
+
13
+ class << self
14
+ attr_accessor :api_key, :url, :download_dir
15
+ end
16
+ # Configure through hash
17
+ def self.configure(opts = {})
18
+ opts.each {|k,v| instance_variable_set("@#{k}",v) }
19
+ end
20
+
21
+ def self.configure_from_file(pathname)
22
+ config = YAML::load(IO.read(pathname))
23
+ configure(config)
24
+ end
25
+
26
+ def self.url
27
+ @url ||= 'https://api.passtools.com/v1'
28
+ end
29
+
30
+ def self.api_key
31
+ @api_key || raise("You must configure api_key before calling")
32
+ end
33
+
34
+ end
@@ -0,0 +1,33 @@
1
+ module Passtools
2
+ class Pass
3
+ extend Request
4
+
5
+ def self.list
6
+ get("/pass")
7
+ end
8
+
9
+ def self.list_by_template(template_id)
10
+ #TODO not implemented in API yet
11
+ end
12
+
13
+ def self.show(pass_id)
14
+ get("/pass/#{pass_id}")
15
+ end
16
+
17
+ def self.create(template_id,data)
18
+ json = MultiJson.dump(data)
19
+ post("/pass/#{template_id}", {:json => json } )
20
+ end
21
+
22
+ def self.update(pass_id,data)
23
+ json = MultiJson.dump(data)
24
+ put("/pass/#{pass_id}", { :json => json } )
25
+ end
26
+
27
+ def self.download(pass_id)
28
+ download_file("/pass/#{pass_id}/download", 'PassToolsPass.pkpass')
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,41 @@
1
+ module Passtools
2
+ module Request
3
+
4
+ def get(path, params = {})
5
+ url = construct_url(path)
6
+ params.merge!(:api_key => Passtools.api_key)
7
+ response = RestClient.get(url, :params => params)
8
+ MultiJson.load(response)
9
+ end
10
+
11
+ def download_file(path, filename)
12
+ filepath = Pathname.new(Passtools.download_dir.to_s)
13
+ raise "Download directory is not defined or does not exist" unless filepath.exist?
14
+ url = construct_url(path)
15
+ params = {:api_key => Passtools.api_key}
16
+ response = RestClient.get(url, :params => params)
17
+ File.open(filepath.join(filename), 'w') {|f| f.write(response) }
18
+ end
19
+
20
+ def post(path, params = {})
21
+ url = construct_url(path)
22
+ params.merge!(:api_key => Passtools.api_key )
23
+ response = RestClient.post(url, params, :multipart => true )
24
+ MultiJson.load(response)
25
+ end
26
+
27
+ def put(path, params = {})
28
+ url = construct_url(path)
29
+ params.merge!(:api_key => Passtools.api_key )
30
+ response = RestClient.put(url, params, :multipart => true )
31
+ MultiJson.load(response)
32
+ end
33
+
34
+ def construct_url(path)
35
+ raise "You must configure API url before calling" if Passtools.url.to_s.empty?
36
+ Passtools.url + path
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -0,0 +1,15 @@
1
+ module Passtools
2
+ class Template
3
+ extend Request
4
+
5
+ def self.list
6
+ get("/template/headers")
7
+ end
8
+
9
+
10
+ def self.show(template_id)
11
+ get("/template/#{template_id}")
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,3 @@
1
+ module Passtools
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/passtools/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Verba"]
6
+ gem.email = ["david@halcyonengineering.com"]
7
+ gem.description = %q{Ruby wrapper to access the Passtools API}
8
+ gem.summary = %q{Passtools API gem}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "passtools"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Passtools::VERSION
17
+
18
+ gem.add_dependency 'rest-client', "~> 1.6.7"
19
+ gem.add_dependency 'multi_json', "~> 1.3.6"
20
+ gem.add_development_dependency 'pry'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'webmock'
23
+ gem.add_development_dependency 'fakefs'
24
+
25
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'fakefs/spec_helpers'
3
+
4
+ describe 'Pass' do
5
+
6
+ include FakeFS::SpecHelpers
7
+ before(:each) do
8
+ Passtools.configure(:url => 'http://foobar.com',
9
+ :api_key => 'i_am_an_api_key',
10
+ :download_dir => '.')
11
+ end
12
+
13
+ it "calls Passtools API for list of Passes" do
14
+ stub = stub_get("http://foobar.com/pass")
15
+
16
+ Passtools::Pass.list
17
+ stub.should have_been_requested
18
+ end
19
+
20
+ it "calls Passtools API for individual Pass detail" do
21
+ stub = stub_get("http://foobar.com/pass/55")
22
+
23
+ Passtools::Pass.show(55)
24
+ stub.should have_been_requested
25
+ end
26
+
27
+ it "calls Passtools API to download Pass" do
28
+ stub = stub_get("http://foobar.com/pass/55/download", 'contents of pass')
29
+
30
+ Passtools::Pass.download(55)
31
+ File.open("PassToolsPass.pkpass", 'r').read.should == "contents of pass"
32
+ end
33
+
34
+ it "calls Passtools API to create new Pass" do
35
+ stub = stub_write(:post, "http://foobar.com/pass/22", "{\"foo\":\"bar\"}" )
36
+
37
+ Passtools::Pass.create(22, {:foo => :bar })
38
+ stub.should have_been_requested
39
+ end
40
+
41
+ it "calls Passtools API to update new Pass" do
42
+ stub = stub_write(:put, "http://foobar.com/pass/55", "{\"foo\":\"bar\"}" )
43
+
44
+ Passtools::Pass.update(55, {:foo => :bar })
45
+ stub.should have_been_requested
46
+ end
47
+
48
+ it "raises descriptive error if directory does not exist when downloading" do
49
+ Passtools.configure(:download_dir => "/asdfasdfasfd")
50
+ expect {Passtools::Pass.download(55)}.to raise_error(RuntimeError, /Download directory is not defined or does not exist/)
51
+ end
52
+
53
+ it "raises descriptive error if directory is not defined when downloading" do
54
+ Passtools.configure(:download_dir => nil)
55
+ expect {Passtools::Pass.download(55)}.to raise_error(RuntimeError, /Download directory is not defined or does not exist/)
56
+ end
57
+
58
+ it "raises error if url is empty" do
59
+ Passtools.configure(:url => '')
60
+ expect {Passtools::Pass.list}.to raise_error(RuntimeError, /You must configure API url before calling/)
61
+ end
62
+
63
+
64
+ it "raises error if api_key is not set" do
65
+ Passtools.configure(:api_key => nil)
66
+ expect {Passtools::Pass.list}.to raise_error(RuntimeError, /You must configure api_key before calling/)
67
+ end
68
+
69
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Template' do
4
+ before(:all) do
5
+ Passtools.configure(:url => 'http://foobar.com',
6
+ :api_key => 'i_am_an_api_key')
7
+
8
+ end
9
+
10
+ it "calls Passtools API for list of template headers" do
11
+ stub = stub_get("http://foobar.com/template/headers")
12
+
13
+ Passtools::Template.list
14
+ stub.should have_been_requested
15
+ end
16
+
17
+ it "calls Passtools API for individual template detail" do
18
+ stub = stub_get("http://foobar.com/template/55")
19
+
20
+ Passtools::Template.show(55)
21
+ stub.should have_been_requested
22
+ end
23
+
24
+ end
@@ -0,0 +1,21 @@
1
+
2
+ #require 'bundler'
3
+ #Bundler.setup
4
+
5
+ require 'passtools'
6
+ require 'webmock/rspec'
7
+ require 'support/passtools_helpers'
8
+
9
+ RSpec.configure do |config|
10
+ config.include PasstoolsHelpers
11
+
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,18 @@
1
+ module PasstoolsHelpers
2
+
3
+ def stub_get(url, body='{}')
4
+ stub_request(:get, url).
5
+ with(:query => {:api_key => 'i_am_an_api_key'}).
6
+ to_return(:body => body)
7
+ end
8
+
9
+ def stub_write(request_type, url, json)
10
+ stub = stub_request(request_type, url).
11
+ with(:body => {:json => json, :api_key => 'i_am_an_api_key'},
12
+ :headers => {'Content-Type'=>'application/x-www-form-urlencoded', 'Multipart'=>'true'}).
13
+ to_return(:body => "{}")
14
+ end
15
+
16
+ end
17
+
18
+
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passtools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Verba
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.6
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.6
46
+ - !ruby/object:Gem::Dependency
47
+ name: pry
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: webmock
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakefs
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Ruby wrapper to access the Passtools API
111
+ email:
112
+ - david@halcyonengineering.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/passtools.rb
124
+ - lib/passtools/pass.rb
125
+ - lib/passtools/request.rb
126
+ - lib/passtools/template.rb
127
+ - lib/passtools/version.rb
128
+ - passtools.gemspec
129
+ - spec/passtools/pass_spec.rb
130
+ - spec/passtools/template_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/support/passtools_helpers.rb
133
+ homepage: ''
134
+ licenses: []
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 1.8.24
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: Passtools API gem
157
+ test_files:
158
+ - spec/passtools/pass_spec.rb
159
+ - spec/passtools/template_spec.rb
160
+ - spec/spec_helper.rb
161
+ - spec/support/passtools_helpers.rb