Last active
August 29, 2015 14:07
-
-
Save ainame/2fae41e2d075c68b55c2 to your computer and use it in GitHub Desktop.
AWS SDKのラッパーを作るとき用の便利concern ref: http://qiita.com/ainame/items/ae6fa3ed3df87d51e523
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApp | |
module AWS | |
class SNS | |
def self.stub_configuration | |
AWS::Core::Configuration.new( | |
access_key_id: 'ACCESS_KEY_ID', | |
secret_access_key: 'SECRET_ACCESS_KEY', | |
stub_requests: true, | |
) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApp::AWS::Configurable | |
extend ActiveSupport::Concern | |
def client | |
self.class.client | |
end | |
included do | |
class << self | |
# cache client object until re-configuring | |
def client | |
@client ||= service_class.new(configuration.to_h) | |
end | |
def configure(&block) | |
# remove client cache at first | |
clear_client_cache! | |
options = Options.new(AWS::Core::Configuration.accepted_options) | |
yield options | |
@aws_core_configuration = AWS::Core::Configuration.new(aws_options.to_h) | |
end | |
def configuration | |
stub_configuration || @aws_core_configuration | |
end | |
def stub_configuration | |
# override at your spec_helper | |
end | |
def clear_client_cache! | |
@client = nil | |
end | |
protected | |
def service_class | |
matched = self.to_s.match(/\AMyApp\:\:([a-zA-Z_\:]+)/).captures[0] | |
(matched + '::Client').constantize | |
end | |
end | |
end | |
class Options < BasicObject | |
def initialize(accepted_options) | |
@accepted_options = accepted_options | |
@config = {} | |
end | |
def to_h | |
@config | |
end | |
def method_missing(method, *args) | |
return super unless setter?(method.to_s) | |
name = extract_name(method.to_s) | |
return super unless accept?(name) | |
@config[name] = args[0] | |
end | |
def setter?(method) | |
method =~ /\A\w*=\z/ | |
end | |
def extract_name(method) | |
method.match(/\A(\w*)=\z/).captures[0].to_sym | |
end | |
def accept?(name) | |
@accepted_options.include?(name) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApp::Hoge | |
def create(xxx) | |
sns = MyApp::AWS::SNS.client | |
sns.create_platform_endpoint(xxx) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApp | |
module AWS | |
class SNS | |
include MyApp::AWS::Configurable | |
# 以下に必要なコードがあれば書いてく | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before do | |
sns = MyApp::AWS::SNS.client | |
resp = sns.stub_for(:create_platform_endpoint) | |
resp.data[:endpoint_arn] = 'arn:aws....' | |
end | |
after do | |
MyApp::AWS::SNS.clear_client_cache! | |
end | |
it 'xxxx xxx xxx' do | |
resp = MyApp::Hoge.create(xxx) | |
expect(resp[:endpoint_arn]).to eq('arn:aws....') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment