globalticket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfca2239def9438df1a128c46bb4f2b33bd99b60
4
+ data.tar.gz: 09fe99bfb9d0adea7e49281b7f27f76aa781566e
5
+ SHA512:
6
+ metadata.gz: 8b3c27a89b4cfc0675d622585e264a392ea4348bb14efd1f8f16652d106b1432b2472aefd5d68020b0302cf2075b888637666cb9c0ee7c9f7d3c860c99d57439
7
+ data.tar.gz: 5b3dd2eebc3150c2b6c70e030b7978c1585f6129f86477c35c311b76d9145921108892b686cd640955136275e6fad04342dc43030135a1daeafaf3f06891cfb5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in globalticket.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Henk Meijer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # Globalticket
2
+ [![Build Status](https://secure.travis-ci.org/henkm/globalticket.png)](http://travis-ci.org/henkm/globalticket)
3
+ [![Gem Version](https://badge.fury.io/rb/globalticket.svg)](http://badge.fury.io/rb/globalticket)
4
+ [![Dependency Status](https://gemnasium.com/henkm/globalticket.svg)](https://gemnasium.com/henkm/globalticket)
5
+ [![Code Climate](https://codeclimate.com/github/henkm/globalticket/badges/gpa.svg)](https://codeclimate.com/github/henkm/globalticket)
6
+
7
+ This gem works as a simple Ruby wrapper for the Global Ticket Resller API. It implements all the API functions are implemented. Please refer to the awesome [Global Ticket Resller documentation](https://globalreseller.nl/documentation/) to see how the API works.
8
+
9
+
10
+ All this gem does, is make it a little bit simpler to use the API:
11
+ - You don't need to sort the attributes
12
+ - You don't need to calculate the HMAC Key
13
+ - You don't need to send your API key or environment with every request
14
+ - You don't need to figure out how to make the POST requests
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'globalticket'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install globalticket
31
+
32
+ ## Configuration
33
+
34
+ First, obtain an API key and shared secret from Global Ticket. Set it up like this:
35
+ ```ruby
36
+ Globalticket::Config.api_key = "MY-API-KEY"
37
+ Globalticket::Config.api_secret = "MY-API-SHARED-SECRET"
38
+ Globalticket::Config.environment = "test" # or 'production'
39
+ ```
40
+
41
+ To use this gem in a Rails project:
42
+ ```ruby
43
+ # config/development.rb
44
+ config.globalticket.api_key = "MY-API-KEY"
45
+ config.globalticket.api_secret = "MY-API-SHARED-SECRET"
46
+ config.globalticket.environment = "test" # or 'production'
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ ### Get available users
52
+ ```ruby
53
+ @global_ticket_users = Globalticket::API.get_available_users
54
+ # => {"success"=>true, "users"=>[{"userId"=>"xx", "userName"=>"Name of the museum", etc..}, {"userId"=>"yy", "userName"=>"Name of the museum", etc..}]}
55
+ ```
56
+
57
+ ### Get ticket types
58
+ ```ruby
59
+ @ticket_types = Globalticket::API.get_ticket_types(userId: xx)
60
+ # => {"success"=>true, "ticketTypes"=>[{"ticketTypeId"=>xx, "ticketTypeName"=>"Dagticket", "ticketTypePrice"=>"10.00"}, ...]}
61
+ ```
62
+
63
+ ### Etcetera
64
+
65
+ All the other functions work exactly in the same fashion. Checkout the globalticket_spec.rb file to see some more examples.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/henkm/globalticket.
76
+
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
+
82
+ This gem is made with love by the smart people at [Eskes Media B.V.](http://www.eskesmedia.nl) and [dagjewegtickets.nl](https://www.dagjewegtickets.nl)
83
+ Global Ticket is not involved with this project and has no affiliation with Eskes Media B.V.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "globalticket"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'globalticket/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "globalticket"
8
+ spec.version = Globalticket::VERSION
9
+ spec.authors = ["Henk Meijer"]
10
+ spec.email = ["hmeijer@eskesmedia.nl"]
11
+
12
+ spec.summary = %q{Ruby implementation of the Global Ticket Reseller API}
13
+ spec.description = %q{Ruby implementation of the Global Ticket Reseller API}
14
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "httparty"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
@@ -0,0 +1,17 @@
1
+ require "globalticket/version"
2
+ require "globalticket/config"
3
+ require "globalticket/engine" if defined?(Rails) && Rails::VERSION::MAJOR.to_i >= 3
4
+ require "globalticket/api"
5
+
6
+ module Globalticket
7
+ # Your code goes here...
8
+
9
+ # For testing purpose only: set the username and password
10
+ # in environment variables to make the tests pass with your test
11
+ # credentials.
12
+ def self.set_credentials_from_environment
13
+ Config.api_key = ENV["GLOBAL_TICKET_API_KEY"]
14
+ Config.api_secret = ENV["GLOBAL_TICKET_API_SECRET"]
15
+
16
+ end
17
+ end
@@ -0,0 +1,115 @@
1
+ module Globalticket
2
+
3
+ # The communication layer implements all the methods available in the Globalticket API
4
+ # https://globalreseller.nl/documentation/
5
+ class API
6
+ require 'httparty'
7
+ require 'date'
8
+
9
+ ENDPOINT_PREFIX = "https://globalreseller.nl/webservices"
10
+
11
+ # returns a list of available users
12
+ def self.get_available_users
13
+ self.make_api_call(endpoint: "getAvailableUsers", data: {})
14
+ end
15
+
16
+ # return a list of ticket types for given userId
17
+ # Original documentation: https://globalreseller.nl/documentation/api/getTicketTypes
18
+ def self.get_ticket_types(userId: nil, language: nil)
19
+ self.make_api_call(endpoint: "getTicketTypes", data: {userId: userId.to_s, language: language})
20
+ end
21
+
22
+ # Fetch available periods and time slots
23
+ # https://globalreseller.nl/documentation/api/getAvailability
24
+ def self.get_availability(userId: nil, start_date: nil, end_date: nil)
25
+ # set default dates if none are given
26
+ start_date = Time.now if start_date.nil?
27
+ end_date = start_date.to_time + ((3600 * 24) * 30) if end_date.nil? # add 31 days, max allowed
28
+
29
+ # convert dates to required string
30
+ start_date = self.convert_date_object_to_format(date: start_date, format: "%Y-%m-%d")
31
+ end_date = self.convert_date_object_to_format(date: end_date, format: "%Y-%m-%d")
32
+
33
+ self.make_api_call(endpoint: "getAvailability", data: {userId: userId.to_s, startDate: start_date, endDate: end_date})
34
+ end
35
+
36
+ # Create a reservation
37
+ # https://globalreseller.nl/documentation/api/createReservation
38
+ def self.create_reservation(userId: nil, tickets: [], ticketDate: nil, ticketTime: nil)
39
+
40
+ # convert dates to required string
41
+ ticketDate = self.convert_date_object_to_format(date: ticketDate, format: "%Y-%m-%d") unless ticketDate.nil?
42
+ ticketTime = self.convert_date_object_to_format(date: ticketTime, format: "%H:%M") unless ticketTime.nil?
43
+
44
+ self.make_api_call(endpoint: "createReservation", data: {userId: userId.to_s, tickets: tickets, ticketDate: ticketDate, ticketTime: ticketTime})
45
+ end
46
+
47
+ # Add a contact to a reservation
48
+ # https://globalreseller.nl/documentation/api/addContact
49
+ def self.add_contact(data = {})
50
+ self.make_api_call(endpoint: "addContact", data: data)
51
+ end
52
+
53
+ # Complete a reservation
54
+ # https://globalreseller.nl/documentation/api/completeReservation
55
+ def self.complete_reservation(userId: nil, reservationId: nil)
56
+ self.make_api_call(endpoint: "completeReservation", data: {userId: userId.to_s, reservationId: reservationId.to_s})
57
+ end
58
+
59
+ # Complete a reservation
60
+ # https://globalreseller.nl/documentation/api/cancelReservation
61
+ def self.cancel_reservation(userId: nil, reservationId: nil)
62
+ self.make_api_call(endpoint: "cancelReservation", data: {userId: userId.to_s, reservationId: reservationId.to_s})
63
+ end
64
+
65
+ def self.convert_date_object_to_format(date: nil, format: "%Y-%m-%d")
66
+ if date.is_a?(String)
67
+ d = Date.parse(date)
68
+ elsif date.is_a?(Date)
69
+ d = date
70
+ elsif date.is_a?(Time)
71
+ d = date
72
+ else
73
+ raise "Invalid date. Should be of type Date, Time or String."
74
+ end
75
+ return d.strftime(format)
76
+ end
77
+
78
+ # return the request data as needed for the API
79
+ # - sorted alphabetically
80
+ # - ending with HMAC calculation
81
+ def self.request_data(data)
82
+ # delet data wit nil values
83
+ data = data.select {|k, v| !v.nil? }
84
+
85
+ # merge data with api-key and env
86
+ unsorted_data = { apiKey: Config.api_key, environment: Config.environment }.merge(data)
87
+
88
+ # sort alphabetically
89
+ sorted_data = Hash[unsorted_data.sort_by{|k,v| k}]
90
+
91
+ # calculate HMAC value
92
+ calculated_hmac = Base64.encode64(OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), Config.api_secret, sorted_data.to_json.strip)).strip.gsub("\n", "")
93
+
94
+ request_data = sorted_data.merge({"HMACKey" => calculated_hmac})
95
+ # puts request_data.to_json
96
+ return request_data
97
+ end
98
+
99
+ # communicate with the API via POST requests and return the return
100
+ # message in a ruby-esque manor.
101
+ def self.make_api_call(endpoint: '', data: {})
102
+ url = "#{ENDPOINT_PREFIX}/#{endpoint}"
103
+ # puts "posting to URL: #{url}:\n#{self.request_data(data)}"
104
+ result = JSON.parse(HTTParty.post(url,
105
+ body: self.request_data(data).to_json,
106
+ headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }).body)
107
+ if result["success"] == false
108
+ raise result["errorMessage"]
109
+ else
110
+ # puts result
111
+ return result
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Configuration object for storing some parameters required for making transactions
3
+ #
4
+ module Globalticket::Config
5
+ class << self
6
+ attr_accessor :api_key
7
+ attr_accessor :api_secret
8
+ attr_accessor :environment
9
+
10
+
11
+ # Set's the default value's to nil and false
12
+ # @return [Hash] configuration options
13
+ def init!
14
+ @defaults = {
15
+ :@api_key => nil,
16
+ :@api_secret => nil,
17
+ :@environment => 'test'
18
+ }
19
+ end
20
+
21
+ # Resets the value's to there previous value (instance_variable)
22
+ # @return [Hash] configuration options
23
+ def reset!
24
+ @defaults.each { |key, value| instance_variable_set(key, value) }
25
+ end
26
+
27
+ # Set's the new value's as instance variables
28
+ # @return [Hash] configuration options
29
+ def update!
30
+ @defaults.each do |key, value|
31
+ instance_variable_set(key, value) unless instance_variable_defined?(key)
32
+ end
33
+ end
34
+ end
35
+ init!
36
+ reset!
37
+ end
@@ -0,0 +1,13 @@
1
+ module Globalticket
2
+ #
3
+ # Simpel extend on the +Rails::Engine+ to add support for a new config section within
4
+ # the environment configs
5
+ #
6
+ # @example default
7
+ # # /config/environments/development.rb
8
+ # config.globalticket.api_key = "12343465sdfgsadr324"
9
+ #
10
+ class Engine < Rails::Engine
11
+ config.globalticket = Globalticket::Config
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Globalticket
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalticket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Henk Meijer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby implementation of the Global Ticket Reseller API
70
+ email:
71
+ - hmeijer@eskesmedia.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - globalticket.gemspec
86
+ - lib/globalticket.rb
87
+ - lib/globalticket/api.rb
88
+ - lib/globalticket/config.rb
89
+ - lib/globalticket/engine.rb
90
+ - lib/globalticket/version.rb
91
+ homepage:
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby implementation of the Global Ticket Reseller API
115
+ test_files: []