paypal-sdk-rest-pmrb 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +15 -0
- data/README.md +265 -0
- data/Rakefile +15 -0
- data/data/DigiCertHighAssuranceEVRootCA.pem +23 -0
- data/data/DigiCertSHA2ExtendedValidationServerCA.pem +28 -0
- data/data/paypal.crt +193 -0
- data/lib/generators/paypal/sdk/USAGE +3 -0
- data/lib/generators/paypal/sdk/install_generator.rb +17 -0
- data/lib/generators/paypal/sdk/templates/paypal.rb +2 -0
- data/lib/generators/paypal/sdk/templates/paypal.yml +29 -0
- data/lib/paypal-sdk-core.rb +38 -0
- data/lib/paypal-sdk-rest.rb +2 -0
- data/lib/paypal-sdk/core/api.rb +20 -0
- data/lib/paypal-sdk/core/api/base.rb +169 -0
- data/lib/paypal-sdk/core/api/data_types/array_with_block.rb +44 -0
- data/lib/paypal-sdk/core/api/data_types/base.rb +225 -0
- data/lib/paypal-sdk/core/api/data_types/enum.rb +26 -0
- data/lib/paypal-sdk/core/api/data_types/simple_types.rb +52 -0
- data/lib/paypal-sdk/core/api/ipn.rb +66 -0
- data/lib/paypal-sdk/core/api/rest.rb +177 -0
- data/lib/paypal-sdk/core/authentication.rb +66 -0
- data/lib/paypal-sdk/core/config.rb +253 -0
- data/lib/paypal-sdk/core/credential.rb +16 -0
- data/lib/paypal-sdk/core/credential/base.rb +27 -0
- data/lib/paypal-sdk/core/credential/certificate.rb +32 -0
- data/lib/paypal-sdk/core/credential/signature.rb +22 -0
- data/lib/paypal-sdk/core/credential/third_party/subject.rb +25 -0
- data/lib/paypal-sdk/core/credential/third_party/token.rb +39 -0
- data/lib/paypal-sdk/core/exceptions.rb +112 -0
- data/lib/paypal-sdk/core/logging.rb +50 -0
- data/lib/paypal-sdk/core/openid_connect.rb +140 -0
- data/lib/paypal-sdk/core/openid_connect/api.rb +50 -0
- data/lib/paypal-sdk/core/openid_connect/data_types.rb +73 -0
- data/lib/paypal-sdk/core/openid_connect/get_api.rb +28 -0
- data/lib/paypal-sdk/core/openid_connect/request_data_type.rb +52 -0
- data/lib/paypal-sdk/core/openid_connect/set_api.rb +36 -0
- data/lib/paypal-sdk/core/util.rb +11 -0
- data/lib/paypal-sdk/core/util/http_helper.rb +171 -0
- data/lib/paypal-sdk/core/util/oauth_signature.rb +64 -0
- data/lib/paypal-sdk/core/util/ordered_hash.rb +165 -0
- data/lib/paypal-sdk/rest.rb +39 -0
- data/lib/paypal-sdk/rest/api.rb +23 -0
- data/lib/paypal-sdk/rest/data_types.rb +2597 -0
- data/lib/paypal-sdk/rest/error_hash.rb +39 -0
- data/lib/paypal-sdk/rest/get_api.rb +20 -0
- data/lib/paypal-sdk/rest/request_data_type.rb +53 -0
- data/lib/paypal-sdk/rest/set_api.rb +42 -0
- data/lib/paypal-sdk/rest/version.rb +7 -0
- data/spec/README.md +22 -0
- data/spec/config/cacert.pem +171 -0
- data/spec/config/cert_key.pem +33 -0
- data/spec/config/paypal.yml +35 -0
- data/spec/config/sample_data.yml +3 -0
- data/spec/core/api/data_type_spec.rb +289 -0
- data/spec/core/api/rest_spec.rb +211 -0
- data/spec/core/config_spec.rb +192 -0
- data/spec/core/logging_spec.rb +28 -0
- data/spec/core/openid_connect_spec.rb +153 -0
- data/spec/invoice_examples_spec.rb +38 -0
- data/spec/log/http.log +175 -0
- data/spec/log/rest_http.log +0 -0
- data/spec/payments_examples_spec.rb +437 -0
- data/spec/payouts_examples_spec.rb +74 -0
- data/spec/rest/data_types_spec.rb +62 -0
- data/spec/rest/error_hash_spec.rb +83 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/subscription_examples_spec.rb +227 -0
- data/spec/support/sample_data.rb +5 -0
- data/spec/web_profile_examples_spec.rb +106 -0
- data/spec/webhooks_examples_spec.rb +93 -0
- metadata +177 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module Paypal
|
2
|
+
module Sdk
|
3
|
+
module Generators
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def copy_config_file
|
8
|
+
copy_file "paypal.yml", "config/paypal.yml"
|
9
|
+
end
|
10
|
+
|
11
|
+
def copy_initializer_file
|
12
|
+
copy_file "paypal.rb", "config/initializers/paypal.rb"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
test: &default
|
2
|
+
|
3
|
+
# Credentials for REST APIs
|
4
|
+
client_id: EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM
|
5
|
+
client_secret: EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM
|
6
|
+
|
7
|
+
# Mode can be 'live' or 'sandbox'
|
8
|
+
mode: sandbox
|
9
|
+
|
10
|
+
# Credentials for Classic APIs
|
11
|
+
app_id: APP-80W284485P519543T
|
12
|
+
username: jb-us-seller_api1.paypal.com
|
13
|
+
password: WX4WTU3S8MY44S7F
|
14
|
+
signature: AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy
|
15
|
+
# # With Certificate
|
16
|
+
# cert_path: "config/cert_key.pem"
|
17
|
+
sandbox_email_address: Platform.sdk.seller@gmail.com
|
18
|
+
|
19
|
+
# # IP Address
|
20
|
+
# ip_address: 127.0.0.1
|
21
|
+
# # HTTP Proxy
|
22
|
+
# http_proxy: http://proxy-ipaddress:3129/
|
23
|
+
|
24
|
+
development:
|
25
|
+
<<: *default
|
26
|
+
|
27
|
+
production:
|
28
|
+
<<: *default
|
29
|
+
mode: live
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module PayPal
|
2
|
+
module SDK
|
3
|
+
module Core
|
4
|
+
|
5
|
+
autoload :VERSION, "paypal-sdk/rest/version"
|
6
|
+
autoload :Config, "paypal-sdk/core/config"
|
7
|
+
autoload :Configuration, "paypal-sdk/core/config"
|
8
|
+
autoload :Logging, "paypal-sdk/core/logging"
|
9
|
+
autoload :Authentication, "paypal-sdk/core/authentication"
|
10
|
+
autoload :Exceptions, "paypal-sdk/core/exceptions"
|
11
|
+
autoload :OpenIDConnect, "paypal-sdk/core/openid_connect"
|
12
|
+
autoload :API, "paypal-sdk/core/api"
|
13
|
+
autoload :Util, "paypal-sdk/core/util"
|
14
|
+
autoload :Credential, "paypal-sdk/core/credential"
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
autoload :OpenIDConnect, "paypal-sdk/core/openid_connect"
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def configure(options = {}, &block)
|
22
|
+
Core::Config.configure(options, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def load(*args)
|
26
|
+
Core::Config.load(*args)
|
27
|
+
end
|
28
|
+
|
29
|
+
def logger
|
30
|
+
Core::Config.logger
|
31
|
+
end
|
32
|
+
|
33
|
+
def logger=(log)
|
34
|
+
Core::Config.logger = log
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module PayPal
|
2
|
+
module SDK
|
3
|
+
module Core
|
4
|
+
module API
|
5
|
+
autoload :Base, "paypal-sdk/core/api/base"
|
6
|
+
autoload :Merchant, "paypal-sdk/core/api/merchant"
|
7
|
+
autoload :Platform, "paypal-sdk/core/api/platform"
|
8
|
+
autoload :REST, "paypal-sdk/core/api/rest"
|
9
|
+
autoload :IPN, "paypal-sdk/core/api/ipn"
|
10
|
+
|
11
|
+
module DataTypes
|
12
|
+
autoload :Base, "paypal-sdk/core/api/data_types/base"
|
13
|
+
autoload :Enum, "paypal-sdk/core/api/data_types/enum"
|
14
|
+
autoload :SimpleTypes, "paypal-sdk/core/api/data_types/simple_types"
|
15
|
+
autoload :ArrayWithBlock, "paypal-sdk/core/api/data_types/array_with_block"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'paypal-sdk/rest/version'
|
2
|
+
|
3
|
+
module PayPal::SDK::Core
|
4
|
+
|
5
|
+
module API
|
6
|
+
# API class provide default functionality for accessing the API web services.
|
7
|
+
# == Example
|
8
|
+
# api = API::Base.new("AdaptivePayments")
|
9
|
+
# response = api.request("GetPaymentOptions", "")
|
10
|
+
class Base
|
11
|
+
|
12
|
+
include Util::HTTPHelper
|
13
|
+
|
14
|
+
attr_accessor :http, :uri, :service_name
|
15
|
+
|
16
|
+
DEFAULT_API_MODE = :sandbox
|
17
|
+
API_MODES = [ :live, :sandbox ]
|
18
|
+
|
19
|
+
# Initialize API object
|
20
|
+
# === Argument
|
21
|
+
# * <tt>service_name</tt> -- (Optional) Service name
|
22
|
+
# * <tt>environment</tt> -- (Optional) Configuration environment to load
|
23
|
+
# * <tt>options</tt> -- (Optional) Override configuration.
|
24
|
+
# === Example
|
25
|
+
# new("AdaptivePayments")
|
26
|
+
# new("AdaptivePayments", :development)
|
27
|
+
# new(:wsdl_service) # It load wsdl_service configuration
|
28
|
+
def initialize(service_name = "", environment = nil, options = {})
|
29
|
+
unless service_name.is_a? String
|
30
|
+
environment, options, service_name = service_name, environment || {}, ""
|
31
|
+
end
|
32
|
+
@service_name = service_name
|
33
|
+
set_config(environment, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def uri
|
37
|
+
@uri ||=
|
38
|
+
begin
|
39
|
+
uri = URI.parse("#{service_endpoint}/#{service_name}")
|
40
|
+
uri.path = uri.path.gsub(/\/+/, "/")
|
41
|
+
uri
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def http
|
46
|
+
@http ||= create_http_connection(uri)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Override set_config method to create http connection on changing the configuration.
|
50
|
+
def set_config(*args)
|
51
|
+
@http = @uri = nil
|
52
|
+
super
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get configured API mode( sandbox or live)
|
56
|
+
def api_mode
|
57
|
+
if config.mode and API_MODES.include? config.mode.to_sym
|
58
|
+
config.mode.to_sym
|
59
|
+
else
|
60
|
+
DEFAULT_API_MODE
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Get service end point
|
65
|
+
def service_endpoint
|
66
|
+
config.endpoint
|
67
|
+
end
|
68
|
+
|
69
|
+
# Default Http header
|
70
|
+
def default_http_header
|
71
|
+
{ "User-Agent" => self.class.user_agent }
|
72
|
+
end
|
73
|
+
|
74
|
+
# Generate HTTP request for given action and parameters
|
75
|
+
# === Arguments
|
76
|
+
# * <tt>http_method</tt> -- HTTP method(get/put/post/delete/patch)
|
77
|
+
# * <tt>action</tt> -- Action to perform
|
78
|
+
# * <tt>params</tt> -- (Optional) Parameters for the action
|
79
|
+
# * <tt>initheader</tt> -- (Optional) HTTP header
|
80
|
+
def api_call(payload)
|
81
|
+
payload[:header] = default_http_header.merge(payload[:header])
|
82
|
+
payload[:uri] ||= uri.dup
|
83
|
+
payload[:http] ||= http.dup
|
84
|
+
payload[:uri].query = encode_www_form(payload[:query]) if payload[:query] and payload[:query].any?
|
85
|
+
format_request(payload)
|
86
|
+
payload[:response] = http_call(payload)
|
87
|
+
format_response(payload)
|
88
|
+
payload[:data]
|
89
|
+
end
|
90
|
+
|
91
|
+
# Generate HTTP request for given action and parameters
|
92
|
+
# === Arguments
|
93
|
+
# * <tt>action</tt> -- Action to perform
|
94
|
+
# * <tt>params</tt> -- (Optional) Parameters for the action
|
95
|
+
# * <tt>initheader</tt> -- (Optional) HTTP header
|
96
|
+
def post(action, params = {}, header = {}, query = {})
|
97
|
+
action, params, header = "", action, params if action.is_a? Hash
|
98
|
+
api_call(:method => :post, :action => action, :query => query, :params => params, :header => header)
|
99
|
+
end
|
100
|
+
alias_method :request, :post
|
101
|
+
|
102
|
+
def get(action, params = {}, header = {})
|
103
|
+
action, params, header = "", action, params if action.is_a? Hash
|
104
|
+
api_call(:method => :get, :action => action, :query => params, :params => nil, :header => header)
|
105
|
+
end
|
106
|
+
|
107
|
+
def patch(action, params = {}, header = {})
|
108
|
+
action, params, header = "", action, params if action.is_a? Hash
|
109
|
+
api_call(:method => :patch, :action => action, :params => params, :header => header)
|
110
|
+
end
|
111
|
+
|
112
|
+
def put(action, params = {}, header = {})
|
113
|
+
action, params, header = "", action, params if action.is_a? Hash
|
114
|
+
api_call(:method => :put, :action => action, :params => params, :header => header)
|
115
|
+
end
|
116
|
+
|
117
|
+
def delete(action, params = {}, header = {})
|
118
|
+
action, params, header = "", action, params if action.is_a? Hash
|
119
|
+
api_call(:method => :delete, :action => action, :params => params, :header => header)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Format Request data. It will be override by child class
|
123
|
+
# == Arguments
|
124
|
+
# * <tt>action</tt> -- Request action
|
125
|
+
# * <tt>params</tt> -- Request parameters
|
126
|
+
# == Return
|
127
|
+
# * <tt>path</tt> -- Formated request uri object
|
128
|
+
# * <tt>params</tt> -- Formated request Parameters
|
129
|
+
# * <tt>header</tt> -- HTTP Header
|
130
|
+
def format_request(payload)
|
131
|
+
payload[:uri].path = url_join(payload[:uri].path, payload[:action])
|
132
|
+
payload[:body] = payload[:params].to_s
|
133
|
+
payload
|
134
|
+
end
|
135
|
+
|
136
|
+
# Format Response object. It will be override by child class
|
137
|
+
# == Argument
|
138
|
+
# * <tt>action</tt> -- Request action
|
139
|
+
# * <tt>response</tt> -- HTTP response object
|
140
|
+
def format_response(payload)
|
141
|
+
payload[:data] = payload[:response].body
|
142
|
+
payload
|
143
|
+
end
|
144
|
+
|
145
|
+
# Format Error object. It will be override by child class.
|
146
|
+
# == Arguments
|
147
|
+
# * <tt>exception</tt> -- Exception object.
|
148
|
+
# * <tt>message</tt> -- Readable error message.
|
149
|
+
def format_error(exception, message)
|
150
|
+
raise exception
|
151
|
+
end
|
152
|
+
|
153
|
+
class << self
|
154
|
+
def sdk_library_details
|
155
|
+
@library_details ||= "paypal-sdk-core #{PayPal::SDK::REST::VERSION}; ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}-#{RUBY_PLATFORM}"
|
156
|
+
begin
|
157
|
+
@library_details << ";#{OpenSSL::OPENSSL_LIBRARY_VERSION}"
|
158
|
+
rescue NameError
|
159
|
+
@library_details << ";OpenSSL #{OpenSSL::OPENSSL_VERSION}"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def user_agent
|
164
|
+
@user_agent ||= "PayPalSDK/rest-sdk-ruby #{PayPal::SDK::REST::VERSION} (#{sdk_library_details})"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module PayPal::SDK::Core
|
2
|
+
module API
|
3
|
+
module DataTypes
|
4
|
+
# Create Array with block.
|
5
|
+
# === Example
|
6
|
+
# ary = ArrayWithBlock.new{|val| val.to_i }
|
7
|
+
# ary.push("123") # [ 123 ]
|
8
|
+
# ary.merge!(["1", "3"]) # [ 123, 1, 3 ]
|
9
|
+
class ArrayWithBlock < ::Array
|
10
|
+
def initialize(&block)
|
11
|
+
@block = block
|
12
|
+
super()
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
super(key) || send(:"[]=", key, nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
def []=(key, value)
|
20
|
+
super(key, @block ? @block.call(value) : value)
|
21
|
+
end
|
22
|
+
|
23
|
+
def push(value)
|
24
|
+
super(@block ? @block.call(value) : value)
|
25
|
+
end
|
26
|
+
|
27
|
+
def merge!(array)
|
28
|
+
if array.is_a? Array
|
29
|
+
array.each do |object|
|
30
|
+
push(object)
|
31
|
+
end
|
32
|
+
elsif array.is_a? Hash and array.keys.first.to_s =~ /^\d+$/
|
33
|
+
array.each do |key, object|
|
34
|
+
self[key.to_i] = object
|
35
|
+
end
|
36
|
+
else
|
37
|
+
push(array)
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,225 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module PayPal::SDK::Core
|
4
|
+
module API
|
5
|
+
|
6
|
+
module DataTypes
|
7
|
+
|
8
|
+
# Create attributes and restrict the object type.
|
9
|
+
# == Example
|
10
|
+
# class ConvertCurrencyRequest < Core::API::DataTypes::Base
|
11
|
+
# object_of :baseAmountList, CurrencyList
|
12
|
+
# object_of :convertToCurrencyList, CurrencyCodeList
|
13
|
+
# object_of :countryCode, String
|
14
|
+
# object_of :conversionType, String
|
15
|
+
# end
|
16
|
+
class Base
|
17
|
+
|
18
|
+
HashOptions = { :attribute => true, :namespace => true, :symbol => false }
|
19
|
+
ContentKey = :value
|
20
|
+
|
21
|
+
include SimpleTypes
|
22
|
+
include Logging
|
23
|
+
|
24
|
+
class << self
|
25
|
+
|
26
|
+
# Add attribute
|
27
|
+
# === Arguments
|
28
|
+
# * <tt>name</tt> -- attribute name
|
29
|
+
# * <tt>options</tt> -- options
|
30
|
+
def add_attribute(name, options = {})
|
31
|
+
add_member(name, SimpleTypes::String, options.merge( :attribute => true ))
|
32
|
+
end
|
33
|
+
|
34
|
+
# Fields list for the DataTye
|
35
|
+
def members
|
36
|
+
@members ||=
|
37
|
+
begin
|
38
|
+
superclass.load_members if defined? superclass.load_members
|
39
|
+
parent_members = superclass.instance_variable_get("@members")
|
40
|
+
parent_members ? parent_members.dup : Util::OrderedHash.new
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Add Field to class variable hash and generate methods
|
45
|
+
# === Example
|
46
|
+
# add_member(:errorMessage, String) # Generate Code
|
47
|
+
# # attr_reader :errorMessage
|
48
|
+
# # alias_method :error_message, :errorMessage
|
49
|
+
# # alias_method :error_message=, :errorMessage=
|
50
|
+
def add_member(member_name, klass, options = {})
|
51
|
+
member_name = member_name.to_sym
|
52
|
+
return if members[member_name]
|
53
|
+
members[member_name] = options.merge( :type => klass )
|
54
|
+
member_variable_name = "@#{member_name}"
|
55
|
+
define_method "#{member_name}=" do |value|
|
56
|
+
object = options[:array] ? convert_array(value, klass) : convert_object(value, klass)
|
57
|
+
instance_variable_set(member_variable_name, object)
|
58
|
+
end
|
59
|
+
default_value = ( options[:array] ? [] : ( klass < Base ? Util::OrderedHash.new : nil ) )
|
60
|
+
define_method member_name do |&block|
|
61
|
+
value = instance_variable_get(member_variable_name) || ( default_value && send("#{member_name}=", default_value) )
|
62
|
+
value.instance_eval(&block) if block
|
63
|
+
value
|
64
|
+
end
|
65
|
+
define_alias_methods(member_name, options)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Define alias methods for getter and setter
|
69
|
+
def define_alias_methods(member_name, options)
|
70
|
+
snakecase_name = snakecase(member_name)
|
71
|
+
alias_method snakecase_name, member_name
|
72
|
+
alias_method "#{snakecase_name}=", "#{member_name}="
|
73
|
+
alias_method "#{options[:namespace]}:#{member_name}=", "#{member_name}=" if options[:namespace]
|
74
|
+
if options[:attribute]
|
75
|
+
alias_method "@#{member_name}=", "#{member_name}="
|
76
|
+
alias_method "@#{options[:namespace]}:#{member_name}=", "#{member_name}=" if options[:namespace]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# define method for given member and the class name
|
81
|
+
# === Example
|
82
|
+
# object_of(:errorMessage, ErrorMessage) # Generate Code
|
83
|
+
# # def errorMessage=(options)
|
84
|
+
# # @errorMessage = ErrorMessage.new(options)
|
85
|
+
# # end
|
86
|
+
# # add_member :errorMessage, ErrorMessage
|
87
|
+
def object_of(key, klass, options = {})
|
88
|
+
add_member(key, klass, options)
|
89
|
+
end
|
90
|
+
|
91
|
+
# define method for given member and the class name
|
92
|
+
# === Example
|
93
|
+
# array_of(:errorMessage, ErrorMessage) # It Generate below code
|
94
|
+
# # def errorMessage=(array)
|
95
|
+
# # @errorMessage = array.map{|options| ErrorMessage.new(options) }
|
96
|
+
# # end
|
97
|
+
# # add_member :errorMessage, ErrorMessage
|
98
|
+
def array_of(key, klass, options = {})
|
99
|
+
add_member(key, klass, options.merge(:array => true))
|
100
|
+
end
|
101
|
+
|
102
|
+
# Generate snakecase string.
|
103
|
+
# === Example
|
104
|
+
# snakecase("errorMessage")
|
105
|
+
# # error_message
|
106
|
+
def snakecase(string)
|
107
|
+
string.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z][a-z])/, '\1_\2').downcase
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
# Initialize options.
|
113
|
+
def initialize(options = {}, &block)
|
114
|
+
merge!(options, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Merge values
|
118
|
+
def merge!(options, &block)
|
119
|
+
if options.is_a? Hash
|
120
|
+
options.each do |key, value|
|
121
|
+
set(key, value)
|
122
|
+
end
|
123
|
+
elsif members[ContentKey]
|
124
|
+
set(ContentKey, options)
|
125
|
+
else
|
126
|
+
raise ArgumentError, "invalid data(#{options.inspect}) for #{self.class.name} class"
|
127
|
+
end
|
128
|
+
self.instance_eval(&block) if block
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
# Set value for given member
|
133
|
+
# === Arguments
|
134
|
+
# * <tt>key</tt> -- member name
|
135
|
+
# * <tt>value</tt> -- value for member
|
136
|
+
def set(key, value)
|
137
|
+
send("#{key}=", value)
|
138
|
+
rescue NoMethodError => error
|
139
|
+
# Uncomment to see missing fields
|
140
|
+
# logger.debug error.message
|
141
|
+
rescue TypeError, ArgumentError => error
|
142
|
+
raise TypeError, "#{error.message}(#{value.inspect}) for #{self.class.name}.#{key} member"
|
143
|
+
end
|
144
|
+
|
145
|
+
# Create array of objects.
|
146
|
+
# === Example
|
147
|
+
# covert_array([{ :amount => "55", :code => "USD"}], CurrencyType)
|
148
|
+
# covert_array({ "0" => { :amount => "55", :code => "USD"} }, CurrencyType)
|
149
|
+
# covert_array({ :amount => "55", :code => "USD"}, CurrencyType)
|
150
|
+
# # @return
|
151
|
+
# # [ <CurrencyType#object @amount="55" @code="USD" > ]
|
152
|
+
def convert_array(array, klass)
|
153
|
+
default_value = ( klass < Base ? Util::OrderedHash.new : nil )
|
154
|
+
data_type_array = ArrayWithBlock.new{|object| convert_object(object || default_value, klass) }
|
155
|
+
data_type_array.merge!(array)
|
156
|
+
end
|
157
|
+
|
158
|
+
# Create object based on given data.
|
159
|
+
# === Example
|
160
|
+
# covert_object({ :amount => "55", :code => "USD"}, CurrencyType )
|
161
|
+
# # @return
|
162
|
+
# # <CurrencyType#object @amount="55" @code="USD" >
|
163
|
+
def convert_object(object, klass)
|
164
|
+
object.is_a?(klass) ? object : ( ( object.nil? or object == "" ) ? nil : klass.new(object) )
|
165
|
+
end
|
166
|
+
|
167
|
+
# Alias instance method for the class method.
|
168
|
+
def members
|
169
|
+
self.class.members
|
170
|
+
end
|
171
|
+
|
172
|
+
# Get configured member names
|
173
|
+
def member_names
|
174
|
+
members.keys
|
175
|
+
end
|
176
|
+
|
177
|
+
# Create Hash based configured members
|
178
|
+
def to_hash(options = {})
|
179
|
+
options = HashOptions.merge(options)
|
180
|
+
hash = Util::OrderedHash.new
|
181
|
+
member_names.each do |member|
|
182
|
+
value = value_to_hash(instance_variable_get("@#{member}"), options)
|
183
|
+
hash[hash_key(member, options)] = value unless skip_value?(value)
|
184
|
+
end
|
185
|
+
hash
|
186
|
+
end
|
187
|
+
|
188
|
+
# Skip nil, empty array and empty hash.
|
189
|
+
def skip_value?(value)
|
190
|
+
value.nil? || ( ( value.is_a?(Array) || value.is_a?(Hash) ) && value.empty? )
|
191
|
+
end
|
192
|
+
|
193
|
+
# Generate Hash key for given member name based on configuration
|
194
|
+
# === Example
|
195
|
+
# hash_key(:amount) # @return :"ebl:amount"
|
196
|
+
# hash_key(:type) # @return :"@type"
|
197
|
+
def hash_key(key, options = {})
|
198
|
+
unless key == ContentKey
|
199
|
+
member_option = members[key]
|
200
|
+
key = member_option[:name] if member_option.include? :name
|
201
|
+
if !member_option[:attribute] and member_option[:namespace] and options[:namespace]
|
202
|
+
key = "#{member_option[:namespace]}:#{key}"
|
203
|
+
end
|
204
|
+
key = "@#{key}" if member_option[:attribute] and options[:attribute]
|
205
|
+
end
|
206
|
+
options[:symbol] ? key.to_sym : key.to_s
|
207
|
+
end
|
208
|
+
|
209
|
+
# Covert the object to hash based on class.
|
210
|
+
def value_to_hash(value, options = {})
|
211
|
+
case value
|
212
|
+
when Array
|
213
|
+
value = value.map{|object| value_to_hash(object, options) }
|
214
|
+
value.delete_if{|v| skip_value?(v) }
|
215
|
+
value
|
216
|
+
when Base
|
217
|
+
value.to_hash(options)
|
218
|
+
else
|
219
|
+
value
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|