suntech_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/suntech_rails/application.js +13 -0
  6. data/app/assets/stylesheets/suntech_rails/application.css +15 -0
  7. data/app/controllers/suntech_rails/application_controller.rb +4 -0
  8. data/app/helpers/suntech_rails/suntech_rails_helper.rb +11 -0
  9. data/app/views/layouts/suntech_rails/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/config/suntech.yml +12 -0
  12. data/lib/suntech_rails/core/config.rb +210 -0
  13. data/lib/suntech_rails/core/exceptions.rb +76 -0
  14. data/lib/suntech_rails/core/logging.rb +49 -0
  15. data/lib/suntech_rails/core/version.rb +5 -0
  16. data/lib/suntech_rails/engine.rb +10 -0
  17. data/lib/suntech_rails/payment/buysafe.rb +46 -0
  18. data/lib/suntech_rails_core.rb +28 -0
  19. data/lib/suntech_rails_payment.rb +8 -0
  20. data/lib/tasks/suntech_rails_tasks.rake +4 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/javascripts/buysafes.js +2 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  26. data/test/dummy/app/assets/stylesheets/buysafes.css +4 -0
  27. data/test/dummy/app/controllers/application_controller.rb +5 -0
  28. data/test/dummy/app/controllers/buysafes_controller.rb +18 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/app/helpers/buysafes_helper.rb +2 -0
  31. data/test/dummy/app/views/buysafes/show.html.erb +1 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/test/dummy/bin/bundle +3 -0
  34. data/test/dummy/bin/rails +4 -0
  35. data/test/dummy/bin/rake +4 -0
  36. data/test/dummy/bin/setup +29 -0
  37. data/test/dummy/config.ru +4 -0
  38. data/test/dummy/config/application.rb +26 -0
  39. data/test/dummy/config/boot.rb +5 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +41 -0
  43. data/test/dummy/config/environments/production.rb +79 -0
  44. data/test/dummy/config/environments/test.rb +42 -0
  45. data/test/dummy/config/initializers/assets.rb +11 -0
  46. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  48. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/test/dummy/config/initializers/inflections.rb +16 -0
  50. data/test/dummy/config/initializers/mime_types.rb +4 -0
  51. data/test/dummy/config/initializers/session_store.rb +3 -0
  52. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/test/dummy/config/locales/en.yml +23 -0
  54. data/test/dummy/config/routes.rb +4 -0
  55. data/test/dummy/config/secrets.yml +22 -0
  56. data/test/dummy/config/suntech.yml +12 -0
  57. data/test/dummy/db/development.sqlite3 +0 -0
  58. data/test/dummy/db/test.sqlite3 +0 -0
  59. data/test/dummy/log/development.log +145 -0
  60. data/test/dummy/log/test.log +240 -0
  61. data/test/dummy/public/404.html +67 -0
  62. data/test/dummy/public/422.html +67 -0
  63. data/test/dummy/public/500.html +66 -0
  64. data/test/dummy/public/favicon.ico +0 -0
  65. data/test/dummy/test/controllers/buysafes_controller_test.rb +7 -0
  66. data/test/dummy/tmp/cache/sprockets/v3.0/08rTh6Ah0YZOj7lOmzBwFX6MyENphGlPlg6pnr2YJDw.cache +0 -0
  67. data/test/dummy/tmp/cache/sprockets/v3.0/4LkP2RT7AmBQjCAht7srZtGMmW2VMXJ0i1hp5z26wnM.cache +1 -0
  68. data/test/dummy/tmp/cache/sprockets/v3.0/4kRWKxifNQ-xtwaKOgE7Ued1jxhCuYV8JC8aPsUeoEo.cache +0 -0
  69. data/test/dummy/tmp/cache/sprockets/v3.0/55i7xZGHNL7d58DbdSB-heUfopfF0daewzkISaulbcg.cache +0 -0
  70. data/test/dummy/tmp/cache/sprockets/v3.0/5BJiDT0IO88A45bBo6_2hgtzvVIU4Wm7BWw9wmFWX5o.cache +0 -0
  71. data/test/dummy/tmp/cache/sprockets/v3.0/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +2 -0
  72. data/test/dummy/tmp/cache/sprockets/v3.0/6Hy_Sj7zUMme06k4qW1wEzwz75y6cHgFQs4Q1TaTa4I.cache +2 -0
  73. data/test/dummy/tmp/cache/sprockets/v3.0/996Rcuk-aHcwTh9cd4670ulUHb425lS91LujtTI4lwQ.cache +1 -0
  74. data/test/dummy/tmp/cache/sprockets/v3.0/9SBFNL1qC4LcOtEGqB3bB3x5Mnr9QvIcYsoA3Y2-sRs.cache +0 -0
  75. data/test/dummy/tmp/cache/sprockets/v3.0/Cl1B-FvSn9Ixi2izJ61iZypeU6zEwLwhcRh3smbEl2M.cache +1 -0
  76. data/test/dummy/tmp/cache/sprockets/v3.0/D0eiZrvgBF9ei4qTzd1THNuOb2gqdloxuc4s8JqQmT8.cache +1 -0
  77. data/test/dummy/tmp/cache/sprockets/v3.0/DSOLSc6A5RVSmvM415eEWAWG_AgOvZcLZOXQjsXyWQA.cache +2 -0
  78. data/test/dummy/tmp/cache/sprockets/v3.0/DTRN9tlq1MVURJs-bRxlrn-ERsWQwoxjg0-1668K1s0.cache +1 -0
  79. data/test/dummy/tmp/cache/sprockets/v3.0/FDlRJLbXBq7e45MWXJuRY214dnrhhjIBd7w6EQT-sbM.cache +3 -0
  80. data/test/dummy/tmp/cache/sprockets/v3.0/FgNmVnSnkSCwall2jsOd_KJIPg7CKpR3C0smXYFGiyM.cache +1 -0
  81. data/test/dummy/tmp/cache/sprockets/v3.0/HQAes2K-dOTMSld5Ap-i4NoVrlcVwVmomdX8B7pPTmE.cache +2 -0
  82. data/test/dummy/tmp/cache/sprockets/v3.0/Hxf5ifXWKwavHYD0WajwxBrRvxsEA6rj0ZH3UwT4rsM.cache +1 -0
  83. data/test/dummy/tmp/cache/sprockets/v3.0/J5XL3lXnB3XTZv-G3LOl_XkrUGVxwXB2S3C7pKRH9Lo.cache +0 -0
  84. data/test/dummy/tmp/cache/sprockets/v3.0/KZoTbCpAVs5qWHxjecvcaymolV14u_yPnVdgjWfx71U.cache +2 -0
  85. data/test/dummy/tmp/cache/sprockets/v3.0/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +2 -0
  86. data/test/dummy/tmp/cache/sprockets/v3.0/P881l9RCS4F9NxmnB0UjAQAN2mM3cO0jRuQXFAkgvM8.cache +2 -0
  87. data/test/dummy/tmp/cache/sprockets/v3.0/QravnHvqhyOB1yjRqTtVyZjL0-pnNZcepDbf1h6y39M.cache +2 -0
  88. data/test/dummy/tmp/cache/sprockets/v3.0/RaD_VWgi5iHWQ2U843gfYBOjDeyxKembej_3yVZUdCg.cache +1 -0
  89. data/test/dummy/tmp/cache/sprockets/v3.0/Y3WL9fJvPMIyL_Y5uHY8GjBC0HWEhYmdsi9pXZAEI8M.cache +3 -0
  90. data/test/dummy/tmp/cache/sprockets/v3.0/_DeVhvH73RLFdpLj2gr3NLAdVvXJFJpbjArMByqDaHo.cache +1 -0
  91. data/test/dummy/tmp/cache/sprockets/v3.0/_GK5iXKPC1vJKKWIBje93dP_cuhT0kI4_ygDW4BijQM.cache +0 -0
  92. data/test/dummy/tmp/cache/sprockets/v3.0/amu6YjT5LlNxuJGx2ieR4ZeQwegfW9lOBG_tNnquIMY.cache +1 -0
  93. data/test/dummy/tmp/cache/sprockets/v3.0/cVJsfmABu_AJogfYTMLW7B7OedAvMbu91fXwBqulw60.cache +1 -0
  94. data/test/dummy/tmp/cache/sprockets/v3.0/gZp3uXMHuYQC4hzCr7bQfetKNdJAtbQmg3so2KpW1Dw.cache +2 -0
  95. data/test/dummy/tmp/cache/sprockets/v3.0/h4x4j1tr1EhdtlHNvBAJWeWiIxvdJtKjtaU5sJRHei4.cache +0 -0
  96. data/test/dummy/tmp/cache/sprockets/v3.0/hZi1k6tpxxCGYxRe7zY74ItcOI8gZrREOpGuA8JSpGg.cache +2 -0
  97. data/test/dummy/tmp/cache/sprockets/v3.0/hl0DUISTd7rW0KZQk41i-DBi_5LoGb0L-I40lmvco4w.cache +1 -0
  98. data/test/dummy/tmp/cache/sprockets/v3.0/jRrGVp_ANmh7RSuJVrw0uprGAKeB2Jc-o3mHdNILtEY.cache +0 -0
  99. data/test/dummy/tmp/cache/sprockets/v3.0/k0-57UP5CRChoOnFMoThKPh25QN7RSqsyzVAZBueL9A.cache +0 -0
  100. data/test/dummy/tmp/cache/sprockets/v3.0/o-wlnhbjVzz4W_lRxHzjMyh-PPkCOUplbyJNtLZOnYE.cache +1 -0
  101. data/test/dummy/tmp/cache/sprockets/v3.0/pA3zfD4fxEI8qov5CPSMm8lfctNpKSVx9bqJnhs3PmE.cache +1 -0
  102. data/test/dummy/tmp/cache/sprockets/v3.0/pEhaat2KBd5SrT7szC_8R1_6hK17FTpvoRFkmCRSD3M.cache +2 -0
  103. data/test/dummy/tmp/cache/sprockets/v3.0/uAsMZu9U74VcG9L1TIqV3vrj-t6DUutL2GfpwhHCIJk.cache +0 -0
  104. data/test/dummy/tmp/cache/sprockets/v3.0/uk049MYsQuuLH_ZxCd_oI_PvEA7w0XPSS7WE8zZk8t8.cache +0 -0
  105. data/test/dummy/tmp/cache/sprockets/v3.0/v7XWbBEJF4o69YuXw0Qf_0Y3grpgUQuXiNbOUO8b5cw.cache +1 -0
  106. data/test/dummy/tmp/cache/sprockets/v3.0/vHGE9peXPRPu2Vur2EpD9h9IDC12Kvm_Si_UO9ftLLQ.cache +1 -0
  107. data/test/dummy/tmp/cache/sprockets/v3.0/vgziwzRd1d05lkU8CZ8SXWhJckqG8KGqEqmL1r_V3Yw.cache +0 -0
  108. data/test/dummy/tmp/cache/sprockets/v3.0/w0Pv9LkPIAKrfdkfCL2cM-RCbMcGh1cm-jJPhfJd6mw.cache +2 -0
  109. data/test/dummy/tmp/cache/sprockets/v3.0/whceGEyKf6iFrWAKfa-IqpRxJcPXFH5Q96DMbnYHt7I.cache +1 -0
  110. data/test/dummy/tmp/cache/sprockets/v3.0/wjpTGdf2wwgWGq5Zw1DyLQZgQNFJviYk3odj8YR0ZTc.cache +2 -0
  111. data/test/dummy/tmp/cache/sprockets/v3.0/z26TnlwC02ZdVw9atwqmOaJ1-zdMNbEeLS_IL2KIkgg.cache +1 -0
  112. data/test/helpers/suntech_rails_helper_test.rb +27 -0
  113. data/test/integration/navigation_test.rb +10 -0
  114. data/test/lib/suntech_rails/payment/buysafe_test.rb +25 -0
  115. data/test/suntech_rails_core_test.rb +7 -0
  116. data/test/test_helper.rb +20 -0
  117. metadata +283 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c98ca534f2c7745f05e2a4febd5b6127beaea250
4
+ data.tar.gz: d15a69645a6b458a4eec968193cfed7ce958cffc
5
+ SHA512:
6
+ metadata.gz: 965286017002182e4058231134305bb55159df5a7d599ecaae85f9c6a03359a8a49ce7f3aa19f128a327f64fb4f5fbee1ac95bc694ffbd706d63a12bd06e4aa3
7
+ data.tar.gz: ff66de88f16726d92f0d55832eb0ffb103f3ab590f6b3e3091b259d91c85e8456ae25bcd3823c05cd9ff64efee784b36f004db04206251eac2e275d126dfae10
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 aloha
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = SuntechRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SuntechRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module SuntechRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ module SuntechRails
2
+ module SuntechRailsHelper
3
+ def build_post_form payment
4
+ form_tag SuntechRails::Core::Config.config.endpoint, class: "#{payment.class.name.demodulize}-form" do
5
+ payment.instance_variable_names.map do |param|
6
+ hidden_field_tag param.gsub('@',''), payment.instance_variable_get(param)
7
+ end.join.html_safe << submit_tag('送出', class: 'btn payment-submit-btn')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SuntechRails</title>
5
+ <%= stylesheet_link_tag "suntech_rails/application", media: "all" %>
6
+ <%= javascript_include_tag "suntech_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ SuntechRails::Engine.routes.draw do
2
+ end
@@ -0,0 +1,12 @@
1
+ test: &default
2
+ web_atm_id: S1512089028
3
+ buy_safe_id: S1512089010
4
+ twentyfour_id: S1512089036
5
+ pay_code_id: S1512089000
6
+ ali_pay_id: S1512089001
7
+ transaction_code: Coool111
8
+ mode: sandbox
9
+ endpoint: https://test.esafe.com.tw/Service/Etopm.aspx
10
+
11
+ development:
12
+ <<: *default
@@ -0,0 +1,210 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module SuntechRails::Core
5
+ # Include Configuration module to access configuration from any object
6
+ # == Examples
7
+ # # Include in any class
8
+ # include Configuration
9
+ #
10
+ # # Access config object and attributes
11
+ # config
12
+ # config.username
13
+ #
14
+ # # Change configuration
15
+ # set_config(:development)
16
+ module Configuration
17
+
18
+ # To get default Config object.
19
+ def config
20
+ @config ||= Config.config
21
+ end
22
+
23
+ # To change the configuration to given environment or configuration
24
+ # === Arguments
25
+ # * <tt>env</tt> -- Environment
26
+ # * <tt>override_configurations</tt> (Optional) -- To override the default configuration.
27
+ # === Examples
28
+ # obj.set_config(api.config)
29
+ # obj.set_config(:http_timeout => 30)
30
+ # obj.set_config(:development)
31
+ # obj.set_config(:development, :http_timeout => 30)
32
+ def set_config(env, override_configurations = {})
33
+ @config =
34
+ case env
35
+ when Config
36
+ env
37
+ when Hash
38
+ begin
39
+ config.dup.merge!(env)
40
+ rescue Errno::ENOENT => error
41
+ Config.new(env)
42
+ end
43
+ else
44
+ Config.config(env, override_configurations)
45
+ end
46
+ end
47
+
48
+ alias_method :config=, :set_config
49
+ end
50
+
51
+ # Config class is used to hold the configurations.
52
+ # == Examples
53
+ # # To load configurations from file
54
+ # Config.load('config/suntech.yml', 'development')
55
+ #
56
+ # # Get configuration
57
+ # Config.config # load default configuration
58
+ # Config.config(:development) # load development configuration
59
+ # Config.config(:development, :app_id => "XYZ") # Override configuration
60
+ #
61
+ # # Read configuration attributes
62
+ # config = Config.config
63
+ # config.username
64
+ # config.endpoint
65
+ class Config
66
+
67
+ include Logging
68
+ include Exceptions
69
+
70
+ attr_accessor :web_atm_id, :buy_safe_id, :twentyfour_id, :pay_code_id, :ali_pay_id,
71
+ :mode, :endpoint, :transaction_code
72
+
73
+ # Create Config object
74
+ # === Options(Hash)
75
+ # * <tt>username</tt> -- Username
76
+ # * <tt>password</tt> -- Password
77
+ # * <tt>signature</tt> (Optional if certificate present) -- Signature
78
+ # * <tt>app_id</tt> -- Application ID
79
+ # * <tt>cert_path</tt> (Optional if signature present) -- Certificate file path
80
+ def initialize(options)
81
+ merge!(options)
82
+ end
83
+
84
+ def ssl_options
85
+ @ssl_options ||= {}.freeze
86
+ end
87
+
88
+ def ssl_options=(options)
89
+ options = Hash[options.map{|key, value| [key.to_sym, value] }]
90
+ @ssl_options = ssl_options.merge(options).freeze
91
+ end
92
+
93
+ # Override configurations
94
+ def merge!(options)
95
+ options.each do |key, value|
96
+ send("#{key}=", value)
97
+ end
98
+ self
99
+ end
100
+
101
+ # Validate required configuration
102
+ def required!(*names)
103
+ names = names.select{|name| send(name).nil? }
104
+ raise MissingConfig.new("Required configuration(#{names.join(", ")})") if names.any?
105
+ end
106
+
107
+ class << self
108
+
109
+ @@config_cache = {}
110
+
111
+ # Load configurations from file
112
+ # === Arguments
113
+ # * <tt>file_name</tt> -- Configuration file path
114
+ # * <tt>default_environment</tt> (Optional) -- default environment configuration to load
115
+ # === Example
116
+ # Config.load('config/paypal.yml', 'development')
117
+ def load(file_name, default_env = default_environment)
118
+ @@config_cache = {}
119
+ @@configurations = read_configurations(file_name)
120
+ @@default_environment = default_env
121
+ config
122
+ end
123
+
124
+ # Get default environment name
125
+ def default_environment
126
+ @@default_environment ||= ENV['SUNTECH_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development"
127
+ end
128
+
129
+ # Set default environment
130
+ def default_environment=(env)
131
+ @@default_environment = env.to_s
132
+ end
133
+
134
+ def configure(options = {}, &block)
135
+ begin
136
+ self.config.merge!(options)
137
+ rescue Errno::ENOENT
138
+ self.configurations = { default_environment => options }
139
+ end
140
+ block.call(self.config) if block
141
+ self.config
142
+ end
143
+
144
+ # Create or Load Config object based on given environment and configurations.
145
+ # === Attributes
146
+ # * <tt>env</tt> (Optional) -- Environment name
147
+ # * <tt>override_configuration</tt> (Optional) -- Override the configuration given in file.
148
+ # === Example
149
+ # Config.config
150
+ # Config.config(:development)
151
+ # Config.config(:development, { :app_id => "XYZ" })
152
+ def config(env = default_environment, override_configuration = {})
153
+ if env.is_a? Hash
154
+ override_configuration = env
155
+ env = default_environment
156
+ end
157
+ if override_configuration.nil? or override_configuration.empty?
158
+ default_config(env)
159
+ else
160
+ default_config(env).dup.merge!(override_configuration)
161
+ end
162
+ end
163
+
164
+ def default_config(env = nil)
165
+ env = (env || default_environment).to_s
166
+ if configurations[env]
167
+ @@config_cache[env] ||= new(configurations[env])
168
+ else
169
+ raise Exceptions::MissingConfig.new("Configuration[#{env}] NotFound")
170
+ end
171
+ end
172
+
173
+ # Set logger
174
+ def logger=(logger)
175
+ Logging.logger = logger
176
+ end
177
+
178
+ # Get logger
179
+ def logger
180
+ if @@configurations[:mode] == 'live' and Logging.logger.level == Logger::DEBUG
181
+ Logging.logger.warn "DEBUG log level not allowed in sandbox mode for security of confidential information. Changing log level to INFO..."
182
+ Logging.logger.level = Logger::INFO
183
+ end
184
+ Logging.logger
185
+ end
186
+
187
+ # Get raw configurations in Hash format.
188
+ def configurations
189
+ @@configurations ||= read_configurations
190
+ end
191
+
192
+ # Set configuration
193
+ def configurations=(configs)
194
+ @@config_cache = {}
195
+ @@configurations = configs && Hash[configs.map{|k,v| [k.to_s, v] }]
196
+ end
197
+
198
+ private
199
+ # Read configurations from the given file name
200
+ # === Arguments
201
+ # * <tt>file_name</tt> (Optional) -- Configuration file path
202
+ def read_configurations(file_name = "config/suntech.yml")
203
+ erb = ERB.new(File.read(file_name))
204
+ erb.filename = file_name
205
+ YAML.load(erb.result)
206
+ end
207
+
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+ require 'pp'
3
+
4
+ module SuntechRails::Core
5
+ module Exceptions
6
+ class ConnectionError < StandardError # :nodoc:
7
+ attr_reader :response
8
+
9
+ def initialize(response, message = nil)
10
+ @response = response
11
+ @message = message
12
+ end
13
+
14
+ def to_s
15
+ message = "Failed."
16
+ message << " Response code = #{response.code}." if response.respond_to?(:code)
17
+ message << " Response message = #{response.message}." if response.respond_to?(:message)
18
+ message
19
+ end
20
+ end
21
+
22
+ # Raised when a Timeout::Error occurs.
23
+ class TimeoutError < ConnectionError
24
+ def initialize(message)
25
+ @message = message
26
+ end
27
+ def to_s; @message ;end
28
+ end
29
+
30
+ # 3xx Redirection
31
+ class Redirection < ConnectionError # :nodoc:
32
+ def to_s
33
+ response['Location'] ? "#{super} => #{response['Location']}" : super
34
+ end
35
+ end
36
+
37
+ class MissingParam < ArgumentError # :nodoc:
38
+ end
39
+
40
+ class MissingConfig < StandardError # :nodoc:
41
+ end
42
+
43
+ # 4xx Client Error
44
+ class ClientError < ConnectionError # :nodoc:
45
+ end
46
+
47
+ # 400 Bad Request
48
+ class BadRequest < ClientError # :nodoc:
49
+ end
50
+
51
+ # 401 Unauthorized
52
+ class UnauthorizedAccess < ClientError # :nodoc:
53
+ end
54
+
55
+ # 403 Forbidden
56
+ class ForbiddenAccess < ClientError # :nodoc:
57
+ end
58
+
59
+ # 404 Not Found
60
+ class ResourceNotFound < ClientError # :nodoc:
61
+ end
62
+
63
+ # 409 Conflict
64
+ class ResourceConflict < ClientError # :nodoc:
65
+ end
66
+
67
+ # 410 Gone
68
+ class ResourceGone < ClientError # :nodoc:
69
+ end
70
+
71
+ # 5xx Server Error
72
+ class ServerError < ConnectionError # :nodoc:
73
+ end
74
+
75
+ end
76
+ end