tpaga 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,215 @@
1
+ module Tpaga
2
+ module Swagger
3
+
4
+ class Request
5
+ require 'uri'
6
+ require 'addressable/uri'
7
+ require 'typhoeus'
8
+
9
+ attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
10
+
11
+
12
+ # All requests must have an HTTP method and a path
13
+ # Optionals parameters are :params, :headers, :body, :format, :host
14
+ #
15
+ def initialize(http_method, path, attributes={})
16
+ # initialize Swagger configuraiton if it has not been done so
17
+ Swagger.configure if Swagger.configuration.nil?
18
+
19
+ attributes[:format] ||= Swagger.configuration.format
20
+ attributes[:params] ||= {}
21
+ attributes[:params] = {} if attributes[:params].empty?
22
+
23
+ # set authentication based on configuration setting
24
+ if attributes[:require_auth]
25
+ case Swagger.configuration.api_key_type
26
+ when 'header' # API key in http header
27
+ # only header supports prefix
28
+ if Swagger.configuration.api_key_prefix.present?
29
+ attributes[:headers][Swagger.configuration.api_key_name] = "#{Swagger.configuration.api_key_prefix} #{Swagger.configuration.api_key}"
30
+ else
31
+ attributes[:headers][Swagger.configuration.api_key_name] = Swagger.configuration.api_key
32
+ end
33
+ when 'query' # API key in url query
34
+ attributes[:params][Swagger.configuration.api_key_name] = Swagger.configuration.api_key
35
+ else
36
+ unless Swagger.configuration.api_key.blank?
37
+ # HTTP basic
38
+ http_auth_header = 'Basic ' + ["#{Swagger.configuration.api_key}:"].pack('m').delete("\r\n")
39
+ attributes[:headers]['Authorization'] = http_auth_header
40
+ else
41
+ # throw error before sending the request to server
42
+ end
43
+ end
44
+ end
45
+
46
+ self.http_method = http_method.to_sym
47
+ self.path = path
48
+ attributes.each do |name, value|
49
+ #send("#{name.to_s.underscore.to_sym}=", value)
50
+ attr = name.to_s.underscore.to_sym
51
+ send("#{attr}=", value) if respond_to? attr
52
+ end
53
+ end
54
+
55
+ # Construct a base URL
56
+ #
57
+ def url(options = {})
58
+ u = Addressable::URI.new(
59
+ :scheme => Swagger.configuration.scheme,
60
+ :host => Swagger.configuration.host,
61
+ :path => self.interpreted_path,
62
+ :query => self.query_string.sub(/\?/, '')
63
+ ).to_s
64
+
65
+ # Drop trailing question mark, if present
66
+ u.sub! /\?$/, ''
67
+
68
+ # no longer needed as we no longer support obfuscated
69
+ ## Obfuscate API key?
70
+ #u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
71
+
72
+ u
73
+ end
74
+
75
+ # Iterate over the params hash, injecting any path values into the path string
76
+ #
77
+ # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
78
+ def interpreted_path
79
+ p = self.path.dup
80
+
81
+ # Fill in the path params
82
+ self.params.each_pair do |key, value|
83
+ p = p.gsub("{#{key}}", value.to_s)
84
+ end
85
+
86
+ # Stick a .{format} placeholder into the path if there isn't
87
+ # one already or an actual format like json or xml
88
+ # e.g. /words/blah => /words.{format}/blah
89
+ if Swagger.configuration.inject_format
90
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
91
+ p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
92
+ end
93
+ end
94
+
95
+ # Stick a .{format} placeholder on the end of the path if there isn't
96
+ # one already or an actual format like json or xml
97
+ # e.g. /words/blah => /words/blah.{format}
98
+ if Swagger.configuration.force_ending_format
99
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
100
+ p = "#{p}.#{format}"
101
+ end
102
+ end
103
+
104
+ p = p.sub("{format}", self.format.to_s)
105
+
106
+ URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
107
+ end
108
+
109
+ # Massage the request body into a state of readiness
110
+ # If body is a hash, camelize all keys then convert to a json string
111
+ #
112
+ def body=(value)
113
+ #comment: no need to use camelize keys
114
+ #if value.is_a?(Hash)
115
+ # value = value.inject({}) do |memo, (k,v)|
116
+ # memo[k.to_s.camelize(:lower).to_sym] = v
117
+ # memo
118
+ # end
119
+ #end
120
+ @body = value
121
+ end
122
+
123
+ # If body is an object, JSONify it before making the actual request.
124
+ #
125
+ def outgoing_body
126
+ if @body.nil? && @form_params && !@form_params.empty?
127
+ data = form_params.dup
128
+ data.each do |key, value|
129
+ data[key] = value.to_s if value && !value.is_a?(File)
130
+ end
131
+ data
132
+ else
133
+ @body.is_a?(String) ? @body : @body.to_json
134
+ end
135
+ end
136
+
137
+ # Construct a query string from the query-string-type params
138
+ def query_string
139
+
140
+ # Iterate over all params,
141
+ # .. removing the ones that are part of the path itself.
142
+ # .. stringifying values so Addressable doesn't blow up.
143
+ query_values = {}
144
+ self.params.each_pair do |key, value|
145
+ next if self.path.include? "{#{key}}" # skip path params
146
+ next if value.blank? && value.class != FalseClass # skip empties
147
+
148
+ # no longer support Swagger.configuration.camelize_params
149
+ #if Swagger.configuration.camelize_params
150
+ # key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
151
+ #end
152
+
153
+ query_values[key] = value.to_s
154
+ end
155
+
156
+ # We don't want to end up with '?' as our query string
157
+ # if there aren't really any params
158
+ return "" if query_values.blank?
159
+
160
+ # Addressable requires query_values to be set after initialization..
161
+ qs = Addressable::URI.new
162
+ qs.query_values = query_values
163
+ qs.to_s
164
+ end
165
+
166
+ def make
167
+ response = case self.http_method.to_sym
168
+ when :get,:GET
169
+ Typhoeus::Request.get(
170
+ self.url,
171
+ :headers => self.headers.stringify_keys
172
+ )
173
+
174
+ when :post,:POST
175
+ Typhoeus::Request.post(
176
+ self.url,
177
+ :body => self.outgoing_body,
178
+ :headers => self.headers.stringify_keys
179
+ )
180
+
181
+ when :put,:PUT
182
+ Typhoeus::Request.put(
183
+ self.url,
184
+ :body => self.outgoing_body,
185
+ :headers => self.headers.stringify_keys
186
+ )
187
+
188
+ when :delete,:DELETE
189
+ Typhoeus::Request.delete(
190
+ self.url,
191
+ :body => self.outgoing_body,
192
+ :headers => self.headers.stringify_keys
193
+ )
194
+ end
195
+ Response.new(response)
196
+ end
197
+
198
+ def response
199
+ self.make
200
+ end
201
+
202
+ def response_code_pretty
203
+ return unless @response.present?
204
+ @response.code.to_s
205
+ end
206
+
207
+ def response_headers_pretty
208
+ return unless @response.present?
209
+ # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
210
+ @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
211
+ end
212
+
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,71 @@
1
+ module Tpaga
2
+ module Swagger
3
+
4
+ class Response
5
+ require 'json'
6
+
7
+ attr_accessor :raw
8
+
9
+ def initialize(raw)
10
+ self.raw = raw
11
+
12
+ case self.code
13
+ when 500..510 then raise ServerError.new(self.error_message, raw)
14
+ when 299..426 then raise ClientError.new(self.error_message, raw)
15
+ end
16
+ end
17
+
18
+ def code
19
+ raw.code
20
+ end
21
+
22
+ # Account for error messages that take different forms...
23
+ def error_message
24
+ "#{raw.code} - #{raw.status_message}"
25
+ end
26
+
27
+ # If body is JSON, parse it
28
+ # Otherwise return raw string
29
+ def body
30
+ JSON.parse raw.body
31
+ rescue
32
+ raw.body
33
+ end
34
+
35
+ # `headers_hash` is a Typhoeus-specific extension of Hash,
36
+ # so simplify it back into a regular old Hash.
37
+ def headers
38
+ h = {}
39
+ raw.headers_hash.each {|k,v| h[k] = v }
40
+ h
41
+ end
42
+
43
+ # Extract the response format from the header hash
44
+ # e.g. {'Content-Type' => 'application/json'}
45
+ def format
46
+ headers['Content-Type'].split("/").last.downcase
47
+ end
48
+
49
+ def json?
50
+ format == 'json'
51
+ end
52
+
53
+ def xml?
54
+ format == 'xml'
55
+ end
56
+
57
+ def pretty_body
58
+ return unless body.present?
59
+ case format
60
+ when 'json' then JSON.pretty_generate(body).gsub(/\n/, '<br/>')
61
+ end
62
+ end
63
+
64
+ def pretty_headers
65
+ JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,6 @@
1
+ module Tpaga
2
+ module Swagger
3
+ VERSION = "4.06.08"
4
+ end
5
+ end
6
+
@@ -0,0 +1,99 @@
1
+ require 'logger'
2
+
3
+ module Tpaga
4
+ module Swagger
5
+
6
+ class << self
7
+ attr_accessor :logger
8
+
9
+ # A Swagger configuration object. Must act like a hash and return sensible
10
+ # values for all Swagger configuration options. See Swagger::Configuration.
11
+ attr_accessor :configuration
12
+
13
+ attr_accessor :resources
14
+
15
+ # Call this method to modify defaults in your initializers.
16
+ #
17
+ # @example
18
+ # Swagger.configure do |config|
19
+ # config.api_key = '1234567890abcdef' # required
20
+ # config.username = 'wordlover' # optional, but needed for user-related functions
21
+ # config.password = 'i<3words' # optional, but needed for user-related functions
22
+ # config.format = 'json' # optional, defaults to 'json'
23
+ # end
24
+ #
25
+ def configure
26
+ self.configuration ||= Configuration.new
27
+ yield(configuration) if block_given?
28
+
29
+ # Configure logger. Default to use Rails
30
+ self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
31
+
32
+ # remove :// from scheme
33
+ configuration.scheme.sub!(/:\/\//, '')
34
+
35
+ # remove http(s):// and anything after a slash
36
+ configuration.host.sub!(/https?:\/\//, '')
37
+ configuration.host = configuration.host.split('/').first
38
+
39
+ # Add leading and trailing slashes to base_path
40
+ configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
41
+ configuration.base_path = "" if configuration.base_path == "/"
42
+ end
43
+
44
+ def authenticated?
45
+ Swagger.configuration.auth_token.present?
46
+ end
47
+
48
+ def de_authenticate
49
+ Swagger.configuration.auth_token = nil
50
+ end
51
+
52
+ def authenticate
53
+ return if Swagger.authenticated?
54
+
55
+ if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
56
+ raise ClientError, "Username and password are required to authenticate."
57
+ end
58
+
59
+ request = Swagger::Request.new(
60
+ :get,
61
+ "account/authenticate/{username}",
62
+ :params => {
63
+ :username => Swagger.configuration.username,
64
+ :password => Swagger.configuration.password
65
+ }
66
+ )
67
+
68
+ response_body = request.response.body
69
+ Swagger.configuration.auth_token = response_body['token']
70
+ end
71
+
72
+ def to_body(obj)
73
+ return nil if obj.nil?
74
+ return obj.to_body if obj.respond_to? :to_body
75
+ return obj.map{|x| to_body(x) } if obj.is_a? Array
76
+ return obj
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ class HTTPError < StandardError
84
+ attr_accessor :response
85
+
86
+ def initialize(message = nil, response = nil)
87
+ super(message)
88
+ @response = response
89
+ end
90
+ end
91
+
92
+ class ServerError < HTTPError
93
+ end
94
+
95
+ class ClientError < HTTPError
96
+ end
97
+
98
+ end
99
+
data/lib/tpaga.rb ADDED
@@ -0,0 +1,28 @@
1
+ # Create the base module
2
+ module Tpaga
3
+
4
+ # Swagger common files
5
+ require 'tpaga/monkey.rb'
6
+ require 'tpaga/swagger.rb'
7
+ require 'tpaga/swagger/configuration.rb'
8
+ require 'tpaga/swagger/request.rb'
9
+ require 'tpaga/swagger/response.rb'
10
+ require 'tpaga/swagger/version.rb'
11
+
12
+
13
+ # Models
14
+ require 'tpaga/models/customer.rb'
15
+ require 'tpaga/models/address.rb'
16
+ require 'tpaga/models/creditcard.rb'
17
+ require 'tpaga/models/creditcardcreate.rb'
18
+ require 'tpaga/models/billingaddress.rb'
19
+ require 'tpaga/models/charge.rb'
20
+ require 'tpaga/models/city.rb'
21
+
22
+
23
+ # APIs
24
+ require 'tpaga/api/charge_api.rb'
25
+ require 'tpaga/api/customer_api.rb'
26
+
27
+
28
+ end
data/tpaga.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "tpaga"
6
+ s.date = "2015-05-21"
7
+ s.version = "0.0.2"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sebastian Ortiz V."]
10
+ s.email = ["sortiz@tpaga.co"]
11
+ s.homepage = "https://tpaga.co"
12
+ s.summary = %q{TPaga API Ruby Bindings powered by Swagger}
13
+ s.description = %q{TPaga Payment Gateway API
14
+
15
+ [Learn about TPaga](https://tpaga.co)
16
+
17
+ [More information about this library](http://restunited.com/releases/515503086426391602/wrappers)
18
+ }
19
+ s.license = 'Apache License, Version 2.0'
20
+
21
+ s.add_runtime_dependency 'typhoeus', '~> 0.2', '>= 0.2.1'
22
+ s.add_runtime_dependency 'addressable', '2.2.7'
23
+ s.add_runtime_dependency 'json', '~> 1.4', '>= 1.4.6'
24
+
25
+ s.files = `find *`.split("
26
+ ").uniq.sort.select{|f| !f.empty? }
27
+ s.executables = []
28
+ s.require_paths = ["lib", "models"]
29
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpaga
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Ortiz V.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: addressable
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 2.2.7
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 2.2.7
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.4'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 1.4.6
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.4'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.4.6
67
+ description: |
68
+ TPaga Payment Gateway API
69
+
70
+ [Learn about TPaga](https://tpaga.co)
71
+
72
+ [More information about this library](http://restunited.com/releases/515503086426391602/wrappers)
73
+ email:
74
+ - sortiz@tpaga.co
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - lib/tpaga.rb
80
+ - lib/tpaga/api/charge_api.rb
81
+ - lib/tpaga/api/customer_api.rb
82
+ - lib/tpaga/models/address.rb
83
+ - lib/tpaga/models/billingaddress.rb
84
+ - lib/tpaga/models/charge.rb
85
+ - lib/tpaga/models/city.rb
86
+ - lib/tpaga/models/creditcard.rb
87
+ - lib/tpaga/models/creditcardcreate.rb
88
+ - lib/tpaga/models/customer.rb
89
+ - lib/tpaga/monkey.rb
90
+ - lib/tpaga/swagger.rb
91
+ - lib/tpaga/swagger/configuration.rb
92
+ - lib/tpaga/swagger/request.rb
93
+ - lib/tpaga/swagger/response.rb
94
+ - lib/tpaga/swagger/version.rb
95
+ - tpaga.gemspec
96
+ homepage: https://tpaga.co
97
+ licenses:
98
+ - Apache License, Version 2.0
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ - models
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: TPaga API Ruby Bindings powered by Swagger
121
+ test_files: []