paypal-sdk-core 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Paypal::Sdk::Core
1
+ # PayPal SDK Core
2
2
 
3
3
  Core library for PayPal ruby SDKs.
4
4
 
@@ -1,41 +1,41 @@
1
- require "paypal-sdk/core/version"
2
- require "paypal-sdk/core/config"
3
- require "paypal-sdk/core/logging"
4
-
5
- module PayPal
6
- module SDK
7
- module Core
8
-
9
- autoload :Authentication, "paypal-sdk/core/authentication"
10
-
11
- module API
12
- autoload :Base, "paypal-sdk/core/api/base"
13
- autoload :Merchant, "paypal-sdk/core/api/merchant"
14
- autoload :Platform, "paypal-sdk/core/api/platform"
15
- autoload :IPN, "paypal-sdk/core/api/ipn"
16
-
17
- module DataTypes
18
- autoload :Base, "paypal-sdk/core/api/data_types/base"
19
- autoload :Enum, "paypal-sdk/core/api/data_types/enum"
20
- autoload :SimpleTypes, "paypal-sdk/core/api/data_types/simple_types"
21
- end
22
- end
23
-
24
- module Util
25
- autoload :OauthSignature, "paypal-sdk/core/util/oauth_signature"
26
- end
27
-
28
- module Credential
29
- autoload :Base, "paypal-sdk/core/credential/base"
30
- autoload :Certificate, "paypal-sdk/core/credential/certificate"
31
- autoload :Signature, "paypal-sdk/core/credential/signature"
32
-
33
- module ThirdParty
34
- autoload :Token, "paypal-sdk/core/credential/third_party/token"
35
- autoload :Subject, "paypal-sdk/core/credential/third_party/subject"
36
- end
37
- end
38
-
39
- end
40
- end
41
- end
1
+ require "paypal-sdk/core/version"
2
+ require "paypal-sdk/core/config"
3
+ require "paypal-sdk/core/logging"
4
+
5
+ module PayPal
6
+ module SDK
7
+ module Core
8
+
9
+ autoload :Authentication, "paypal-sdk/core/authentication"
10
+
11
+ module API
12
+ autoload :Base, "paypal-sdk/core/api/base"
13
+ autoload :Merchant, "paypal-sdk/core/api/merchant"
14
+ autoload :Platform, "paypal-sdk/core/api/platform"
15
+ autoload :IPN, "paypal-sdk/core/api/ipn"
16
+
17
+ module DataTypes
18
+ autoload :Base, "paypal-sdk/core/api/data_types/base"
19
+ autoload :Enum, "paypal-sdk/core/api/data_types/enum"
20
+ autoload :SimpleTypes, "paypal-sdk/core/api/data_types/simple_types"
21
+ end
22
+ end
23
+
24
+ module Util
25
+ autoload :OauthSignature, "paypal-sdk/core/util/oauth_signature"
26
+ end
27
+
28
+ module Credential
29
+ autoload :Base, "paypal-sdk/core/credential/base"
30
+ autoload :Certificate, "paypal-sdk/core/credential/certificate"
31
+ autoload :Signature, "paypal-sdk/core/credential/signature"
32
+
33
+ module ThirdParty
34
+ autoload :Token, "paypal-sdk/core/credential/third_party/token"
35
+ autoload :Subject, "paypal-sdk/core/credential/third_party/subject"
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -125,13 +125,13 @@ module PayPal::SDK::Core
125
125
  uri, content, header = format_request(action, params)
126
126
  initheader = default_http_header.merge(header).merge(initheader)
127
127
  initheader.delete_if{|key, val| val.nil? }
128
- start_time = Time.now
129
- response = @http.post(uri.path, content, initheader)
128
+ response =
129
+ log_event("Request: #{action}") do
130
+ @http.post(uri.path, content, initheader)
131
+ end
130
132
  format_response(action, response)
131
133
  rescue Net::HTTPBadGateway, Errno::ECONNRESET, Errno::ECONNABORTED, SocketError => error
132
134
  format_error(error, error.message)
133
- ensure
134
- log_event("Request: #{action}", start_time)
135
135
  end
136
136
 
137
137
  # Format Request data. It will be override by child class
@@ -1,139 +1,153 @@
1
- require 'erb'
2
- require 'yaml'
3
-
4
- module PayPal::SDK::Core
5
-
6
- # Include Configuration module to access configuration from any object
7
- # == Examples
8
- # # Include in any class
9
- # include Configuration
10
- #
11
- # # Access config object and attributes
12
- # config
13
- # config.username
14
- #
15
- # # Change configuration
16
- # set_config(:development)
17
- module Configuration
18
-
19
- # To get default Config object.
20
- def config
21
- @config ||= Config.config
22
- end
23
-
24
- # To change the configuration to given environment or configuration
25
- # === Arguments
26
- # * <tt>env</tt> -- Environment
27
- # * <tt>override_configurations</tt> (Optional) -- To override the default configuration.
28
- def set_config(env, override_configurations = {})
29
- @config = env.is_a?(Config) ? env : Config.config(env, override_configurations)
30
- end
31
-
32
- alias_method :config=, :set_config
33
- end
34
-
35
- # Config class is used to hold the configurations.
36
- # == Examples
37
- # # To load configurations from file
38
- # Config.load('config/paypal.yml', 'development')
39
- #
40
- # # Get configuration
41
- # Config.config # load default configuration
42
- # Config.config(:development) # load development configuration
43
- # Config.config(:development, :app_id => "XYZ") # Override configuration
44
- #
45
- # # Read configuration attributes
46
- # config = Config.config
47
- # config.username
48
- # config.end_point
49
- class Config
50
- attr_accessor :username, :password, :signature, :app_id, :cert_path,
51
- :token, :token_secret, :subject,
52
- :http_timeout, :http_retry, :http_proxy, :ca_file,
53
- :device_ipaddress, :sandbox_email_address,
54
- :mode, :end_point, :merchant_end_point, :platform_end_point, :ipn_end_point,
55
- :redirect_url, :dev_central_url,
56
- :logfile
57
-
58
- # Create Config object
59
- # === Options(Hash)
60
- # * <tt>username</tt> -- Username
61
- # * <tt>password</tt> -- Password
62
- # * <tt>signature</tt> (Optional if certificate present) -- Signature
63
- # * <tt>app_id</tt> -- Application ID
64
- # * <tt>cert_path</tt> (Optional if signature present) -- Certificate file path
65
- def initialize(options)
66
- options.each do |key, value|
67
- send("#{key}=", value)
68
- end
69
- end
70
-
71
- class << self
72
-
73
- @@config_cache = {}
74
-
75
- # Load configurations from file
76
- # === Arguments
77
- # * <tt>file_name</tt> -- Configuration file path
78
- # * <tt>default_environment</tt> (Optional) -- default environment configuration to load
79
- # === Example
80
- # Config.load('config/paypal.yml', 'development')
81
- def load(file_name, default_env = default_environment)
82
- @@configurations = read_configurations(file_name)
83
- @@default_environment = default_env
84
- config
85
- end
86
-
87
- # Get default environment name
88
- def default_environment
89
- @@default_environment ||= ENV['PAYPAL_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['ENV'] || "development"
90
- end
91
-
92
- # Create or Load Config object based on given environment and configurations.
93
- # === Attributes
94
- # * <tt>env</tt> (Optional) -- Environment name
95
- # * <tt>override_configuration</tt> (Optional) -- Override the configuration given in file.
96
- # === Example
97
- # Config.config
98
- # Config.config(:development)
99
- # Config.config(:development, { :app_id => "XYZ" })
100
- def config(env = default_environment, override_configuration = {})
101
- if env.is_a? Hash
102
- override_configuration = env
103
- env = default_environment
104
- end
105
- env = (env || default_environment).to_s
106
- raise "Configuration[#{env}] NotFound" unless configurations[env]
107
- if override_configuration.nil? or override_configuration.empty?
108
- @@config_cache[env] ||= new configurations[env]
109
- else
110
- new configurations[env].merge(override_configuration)
111
- end
112
- end
113
-
114
- def logger=(logger)
115
- Logging.logger = logger
116
- end
117
-
118
- def logger
119
- Logging.logger
120
- end
121
-
122
- private
123
- # Read configurations from the given file name
124
- # === Arguments
125
- # * <tt>file_name</tt> (Optional) -- Configuration file path
126
- def read_configurations(file_name = "config/paypal.yml")
127
- erb = ERB.new(File.read(file_name))
128
- erb.filename = file_name
129
- YAML.load(erb.result)
130
- end
131
-
132
- # Get raw configurations in Hash format.
133
- def configurations
134
- @@configurations ||= read_configurations
135
- end
136
-
137
- end
138
- end
139
- end
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module PayPal::SDK::Core
5
+
6
+ # Include Configuration module to access configuration from any object
7
+ # == Examples
8
+ # # Include in any class
9
+ # include Configuration
10
+ #
11
+ # # Access config object and attributes
12
+ # config
13
+ # config.username
14
+ #
15
+ # # Change configuration
16
+ # set_config(:development)
17
+ module Configuration
18
+
19
+ # To get default Config object.
20
+ def config
21
+ @config ||= Config.config
22
+ end
23
+
24
+ # To change the configuration to given environment or configuration
25
+ # === Arguments
26
+ # * <tt>env</tt> -- Environment
27
+ # * <tt>override_configurations</tt> (Optional) -- To override the default configuration.
28
+ def set_config(env, override_configurations = {})
29
+ @config = env.is_a?(Config) ? env : Config.config(env, override_configurations)
30
+ end
31
+
32
+ alias_method :config=, :set_config
33
+ end
34
+
35
+ # Config class is used to hold the configurations.
36
+ # == Examples
37
+ # # To load configurations from file
38
+ # Config.load('config/paypal.yml', 'development')
39
+ #
40
+ # # Get configuration
41
+ # Config.config # load default configuration
42
+ # Config.config(:development) # load development configuration
43
+ # Config.config(:development, :app_id => "XYZ") # Override configuration
44
+ #
45
+ # # Read configuration attributes
46
+ # config = Config.config
47
+ # config.username
48
+ # config.end_point
49
+ class Config
50
+ attr_accessor :username, :password, :signature, :app_id, :cert_path,
51
+ :token, :token_secret, :subject,
52
+ :http_timeout, :http_retry, :http_proxy, :ca_file,
53
+ :device_ipaddress, :sandbox_email_address,
54
+ :mode, :end_point, :merchant_end_point, :platform_end_point, :ipn_end_point,
55
+ :redirect_url, :dev_central_url,
56
+ :logfile
57
+
58
+ # Create Config object
59
+ # === Options(Hash)
60
+ # * <tt>username</tt> -- Username
61
+ # * <tt>password</tt> -- Password
62
+ # * <tt>signature</tt> (Optional if certificate present) -- Signature
63
+ # * <tt>app_id</tt> -- Application ID
64
+ # * <tt>cert_path</tt> (Optional if signature present) -- Certificate file path
65
+ def initialize(options)
66
+ options.each do |key, value|
67
+ send("#{key}=", value)
68
+ end
69
+ end
70
+
71
+ class << self
72
+
73
+ @@config_cache = {}
74
+
75
+ # Load configurations from file
76
+ # === Arguments
77
+ # * <tt>file_name</tt> -- Configuration file path
78
+ # * <tt>default_environment</tt> (Optional) -- default environment configuration to load
79
+ # === Example
80
+ # Config.load('config/paypal.yml', 'development')
81
+ def load(file_name, default_env = default_environment)
82
+ @@config_cache = {}
83
+ @@configurations = read_configurations(file_name)
84
+ @@default_environment = default_env
85
+ config
86
+ end
87
+
88
+ # Get default environment name
89
+ def default_environment
90
+ @@default_environment ||= ENV['PAYPAL_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['ENV'] || "development"
91
+ end
92
+
93
+ # Set default environment
94
+ def default_environment=(env)
95
+ @@default_environment = env.to_s
96
+ end
97
+
98
+ # Create or Load Config object based on given environment and configurations.
99
+ # === Attributes
100
+ # * <tt>env</tt> (Optional) -- Environment name
101
+ # * <tt>override_configuration</tt> (Optional) -- Override the configuration given in file.
102
+ # === Example
103
+ # Config.config
104
+ # Config.config(:development)
105
+ # Config.config(:development, { :app_id => "XYZ" })
106
+ def config(env = default_environment, override_configuration = {})
107
+ if env.is_a? Hash
108
+ override_configuration = env
109
+ env = default_environment
110
+ end
111
+ env = (env || default_environment).to_s
112
+ raise "Configuration[#{env}] NotFound" unless configurations[env]
113
+ if override_configuration.nil? or override_configuration.empty?
114
+ @@config_cache[env] ||= new configurations[env]
115
+ else
116
+ new configurations[env].merge(override_configuration)
117
+ end
118
+ end
119
+
120
+ # Set logger
121
+ def logger=(logger)
122
+ Logging.logger = logger
123
+ end
124
+
125
+ # Get logger
126
+ def logger
127
+ Logging.logger
128
+ end
129
+
130
+ # Get raw configurations in Hash format.
131
+ def configurations
132
+ @@configurations ||= read_configurations
133
+ end
134
+
135
+ # Set configuration
136
+ def configurations=(configs)
137
+ @@config_cache = {}
138
+ @@configurations = Hash[configs.map{|k,v| [k.to_s, v] }]
139
+ end
140
+
141
+ private
142
+ # Read configurations from the given file name
143
+ # === Arguments
144
+ # * <tt>file_name</tt> (Optional) -- Configuration file path
145
+ def read_configurations(file_name = "config/paypal.yml")
146
+ erb = ERB.new(File.read(file_name))
147
+ erb.filename = file_name
148
+ YAML.load(erb.result)
149
+ end
150
+
151
+ end
152
+ end
153
+ end
@@ -21,9 +21,9 @@ module PayPal::SDK::Core
21
21
 
22
22
  # Return credential properties for authentication.
23
23
  def properties
24
- certificate_properties = {}
25
- certificate_properties.delete_if{|k,v| RemoveProperties.include? k }
26
- certificate_properties.merge( :authorization => oauth_authentication )
24
+ credential_properties = credential.properties
25
+ credential_properties.delete_if{|k,v| RemoveProperties.include? k }
26
+ credential_properties.merge( :authorization => oauth_authentication )
27
27
  end
28
28
 
29
29
  private
@@ -1,43 +1,45 @@
1
- require 'logger'
2
-
3
- module PayPal::SDK::Core
4
- # Include Logging module to provide logger functionality.
5
- # == Configure logger
6
- # Logging.logger = Logger.new(STDERR)
7
- #
8
- # == Example
9
- # include Logger
10
- # logger.info "Debug message"
11
- module Logging
12
-
13
- # Get logger object
14
- def logger
15
- @logger ||= Logging.logger
16
- end
17
-
18
- def log_event(message, start_time, end_time = Time.now)
19
- duration = sprintf("%.3f", end_time - start_time)
20
- logger.info "[#{duration}] #{message}"
21
- end
22
-
23
- class << self
24
-
25
- # Get or Create configured logger based on the default environment configuration
26
- def logger
27
- @logger ||= Logger.new(Config.config.logfile || STDERR)
28
- end
29
-
30
- # Set logger directly and clear the loggers cache.
31
- # === Attributes
32
- # * <tt>logger</tt> -- Logger object
33
- # === Example
34
- # Logging.logger = Logger.new(STDERR)
35
- def logger=(logger)
36
- @logger = logger
37
- end
38
-
39
- end
40
- end
41
-
42
- end
43
-
1
+ require 'logger'
2
+
3
+ module PayPal::SDK::Core
4
+ # Include Logging module to provide logger functionality.
5
+ # == Configure logger
6
+ # Logging.logger = Logger.new(STDERR)
7
+ #
8
+ # == Example
9
+ # include Logger
10
+ # logger.info "Debug message"
11
+ module Logging
12
+
13
+ # Get logger object
14
+ def logger
15
+ @logger ||= Logging.logger
16
+ end
17
+
18
+ def log_event(message, &block)
19
+ start_time = Time.now
20
+ block.call
21
+ ensure
22
+ logger.info sprintf("[%.3fs] %s", Time.now - start_time, message)
23
+ end
24
+
25
+ class << self
26
+
27
+ # Get or Create configured logger based on the default environment configuration
28
+ def logger
29
+ @logger ||= Logger.new(Config.config.logfile || STDERR)
30
+ end
31
+
32
+ # Set logger directly and clear the loggers cache.
33
+ # === Attributes
34
+ # * <tt>logger</tt> -- Logger object
35
+ # === Example
36
+ # Logging.logger = Logger.new(STDERR)
37
+ def logger=(logger)
38
+ @logger = logger
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ end
45
+