and-son 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +0 -1
- data/lib/and-son/client.rb +31 -15
- data/lib/and-son/connection.rb +1 -1
- data/lib/and-son/stored_responses.rb +33 -0
- data/lib/and-son/version.rb +1 -1
- data/test/system/making_requests_test.rb +22 -0
- data/test/unit/stored_responses_test.rb +82 -0
- metadata +7 -4
    
        data/Gemfile
    CHANGED
    
    
    
        data/lib/and-son/client.rb
    CHANGED
    
    | @@ -2,6 +2,7 @@ require 'ostruct' | |
| 2 2 | 
             
            require 'sanford-protocol'
         | 
| 3 3 | 
             
            require 'and-son/connection'
         | 
| 4 4 | 
             
            require 'and-son/response'
         | 
| 5 | 
            +
            require 'and-son/stored_responses'
         | 
| 5 6 |  | 
| 6 7 | 
             
            module AndSon
         | 
| 7 8 |  | 
| @@ -31,46 +32,61 @@ module AndSon | |
| 31 32 |  | 
| 32 33 | 
             
              end
         | 
| 33 34 |  | 
| 34 | 
            -
              class Client | 
| 35 | 
            +
              class Client
         | 
| 35 36 | 
             
                include CallRunnerMethods
         | 
| 36 37 |  | 
| 37 38 | 
             
                DEFAULT_TIMEOUT = 60 #seconds
         | 
| 38 39 |  | 
| 40 | 
            +
                attr_reader :host, :port, :version, :responses
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                def initialize(host, port, version)
         | 
| 43 | 
            +
                  @host, @port, @version = host, port, version
         | 
| 44 | 
            +
                  @responses = AndSon::StoredResponses.new
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 39 47 | 
             
                # proxy the call method to the call runner
         | 
| 40 48 | 
             
                def call(*args, &block); self.call_runner.call(*args, &block); end
         | 
| 41 49 |  | 
| 42 50 | 
             
                def call_runner
         | 
| 43 51 | 
             
                  # always start with this default CallRunner
         | 
| 44 52 | 
             
                  CallRunner.new({
         | 
| 45 | 
            -
                    :host | 
| 46 | 
            -
                    :port | 
| 47 | 
            -
                    :version | 
| 53 | 
            +
                    :host     => host,
         | 
| 54 | 
            +
                    :port     => port,
         | 
| 55 | 
            +
                    :version  => version,
         | 
| 48 56 | 
             
                    :timeout_value => (ENV['ANDSON_TIMEOUT'] || DEFAULT_TIMEOUT).to_f,
         | 
| 49 | 
            -
                    :params_value  => {}
         | 
| 57 | 
            +
                    :params_value  => {},
         | 
| 58 | 
            +
                    :responses     => @responses,
         | 
| 50 59 | 
             
                  })
         | 
| 51 60 | 
             
                end
         | 
| 52 61 | 
             
              end
         | 
| 53 62 |  | 
| 54 | 
            -
              class CallRunner < OpenStruct | 
| 63 | 
            +
              class CallRunner < OpenStruct
         | 
| 64 | 
            +
                # {:host, :port, :version, :timeout_value, :params_value, :responses}
         | 
| 55 65 | 
             
                include CallRunnerMethods
         | 
| 56 66 |  | 
| 57 67 | 
             
                # chain runner methods by returning itself
         | 
| 58 68 | 
             
                def call_runner; self; end
         | 
| 59 69 |  | 
| 60 | 
            -
                def call(name, params =  | 
| 70 | 
            +
                def call(name, params = nil)
         | 
| 71 | 
            +
                  params ||= {}
         | 
| 61 72 | 
             
                  if !params.kind_of?(Hash)
         | 
| 62 | 
            -
                    raise ArgumentError, "expected params to be a Hash instead of a #{ | 
| 73 | 
            +
                    raise ArgumentError, "expected params to be a Hash instead of a #{params.class}"
         | 
| 63 74 | 
             
                  end
         | 
| 75 | 
            +
                  client_response = self.responses.find(name, params) if ENV['ANDSON_TEST_MODE']
         | 
| 76 | 
            +
                  client_response ||= self.call!(name, params)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                  if block_given?
         | 
| 79 | 
            +
                    yield client_response.protocol_response
         | 
| 80 | 
            +
                  else
         | 
| 81 | 
            +
                    client_response.data
         | 
| 82 | 
            +
                  end
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                def call!(name, params)
         | 
| 64 86 | 
             
                  call_params = self.params_value.merge(params)
         | 
| 65 87 | 
             
                  AndSon::Connection.new(host, port).open do |connection|
         | 
| 66 88 | 
             
                    connection.write(Sanford::Protocol::Request.new(version, name, call_params).to_hash)
         | 
| 67 | 
            -
                     | 
| 68 | 
            -
             | 
| 69 | 
            -
                    if block_given?
         | 
| 70 | 
            -
                      yield client_response.protocol_response
         | 
| 71 | 
            -
                    else
         | 
| 72 | 
            -
                      client_response.data
         | 
| 73 | 
            -
                    end
         | 
| 89 | 
            +
                    AndSon::Response.parse(connection.read(timeout_value))
         | 
| 74 90 | 
             
                  end
         | 
| 75 91 | 
             
                end
         | 
| 76 92 |  | 
    
        data/lib/and-son/connection.rb
    CHANGED
    
    
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            require 'sanford-protocol'
         | 
| 2 | 
            +
            require 'and-son/response'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module AndSon
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class StoredResponses
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                RequestData = Struct.new(:name, :params)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def initialize
         | 
| 11 | 
            +
                  @hash = {}
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def add(name, params = nil)
         | 
| 15 | 
            +
                  request_data = RequestData.new(name, params || {})
         | 
| 16 | 
            +
                  response = yield
         | 
| 17 | 
            +
                  if !response.kind_of?(Sanford::Protocol::Response)
         | 
| 18 | 
            +
                    response = Sanford::Protocol::Response.new(200, response)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                  @hash[request_data] = AndSon::Response.new(response)
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def find(name, params = nil)
         | 
| 24 | 
            +
                  @hash[RequestData.new(name, params || {})]
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def remove(name, params = nil)
         | 
| 28 | 
            +
                  @hash.delete(RequestData.new(name, params || {}))
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            end
         | 
    
        data/lib/and-son/version.rb
    CHANGED
    
    
| @@ -29,6 +29,28 @@ class MakingRequestsTest < Assert::Context | |
| 29 29 |  | 
| 30 30 | 
             
              end
         | 
| 31 31 |  | 
| 32 | 
            +
              class WithStoredResponsesTest < MakingRequestsTest
         | 
| 33 | 
            +
                desc "is stored with and-son and with testing ENV var set"
         | 
| 34 | 
            +
                setup do
         | 
| 35 | 
            +
                  ENV['ANDSON_TEST_MODE'] = 'yes'
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                teardown do
         | 
| 38 | 
            +
                  ENV.delete('ANDSON_TEST_MODE')
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                should "return the registered response" do
         | 
| 42 | 
            +
                  client = AndSon.new('localhost', 12000, 'v1')
         | 
| 43 | 
            +
                  client.responses.add('echo', 'message' => 'test'){ 'test' }
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  client.call('echo', 'message' => 'test') do |response|
         | 
| 46 | 
            +
                    assert_equal 200,     response.code
         | 
| 47 | 
            +
                    assert_equal nil,     response.status.message
         | 
| 48 | 
            +
                    assert_equal 'test',  response.data
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 32 54 | 
             
              class AuthorizeTest < MakingRequestsTest
         | 
| 33 55 | 
             
                setup do
         | 
| 34 56 | 
             
                  @fake_server.add_handler('v1', 'authorize_it') do |params|
         | 
| @@ -0,0 +1,82 @@ | |
| 1 | 
            +
            require 'assert'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class AndSon::StoredResponses
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              class BaseTest < Assert::Context
         | 
| 6 | 
            +
                desc "AndSon::StoredResponses"
         | 
| 7 | 
            +
                setup do
         | 
| 8 | 
            +
                  @responses = AndSon::StoredResponses.new
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
                subject{ @responses }
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                should have_instance_methods :add, :remove, :find
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              class AddTest < BaseTest
         | 
| 17 | 
            +
                desc "add"
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                should "allow adding responses given an name and optional params" do
         | 
| 20 | 
            +
                  subject.add('test', { 'id' => 1 }) do
         | 
| 21 | 
            +
                    Sanford::Protocol::Response.new([ 404, 'not found' ])
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                  response = subject.find('test', { 'id' => 1 }).protocol_response
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  assert_equal 404,         response.code
         | 
| 26 | 
            +
                  assert_equal 'not found', response.status.message
         | 
| 27 | 
            +
                  assert_equal nil,         response.data
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  subject.add('test'){ Sanford::Protocol::Response.new([ 404, 'not found' ]) }
         | 
| 30 | 
            +
                  response = subject.find('test').protocol_response
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  assert_equal 404,         response.code
         | 
| 33 | 
            +
                  assert_equal 'not found', response.status.message
         | 
| 34 | 
            +
                  assert_equal nil,         response.data
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                should "default the response as a 200 when only given response data" do
         | 
| 38 | 
            +
                  subject.add('test'){ true }
         | 
| 39 | 
            +
                  response = subject.find('test').protocol_response
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  assert_equal 200,   response.code
         | 
| 42 | 
            +
                  assert_equal nil,   response.status.message
         | 
| 43 | 
            +
                  assert_equal true,  response.data
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              class FindTest < BaseTest
         | 
| 49 | 
            +
                desc "find"
         | 
| 50 | 
            +
                setup do
         | 
| 51 | 
            +
                  @responses.add('test', { 'id' => 1 }){ true }
         | 
| 52 | 
            +
                  @responses.add('test'){ true }
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                should "allow finding a response given a name and optional params" do
         | 
| 56 | 
            +
                  response = subject.find('test', { 'id' => 1 }).protocol_response
         | 
| 57 | 
            +
                  assert_equal true, response.data
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                  response = subject.find('test').protocol_response
         | 
| 60 | 
            +
                  assert_equal true, response.data
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              class RemoveTest < BaseTest
         | 
| 66 | 
            +
                desc "remove"
         | 
| 67 | 
            +
                setup do
         | 
| 68 | 
            +
                  @responses.add('test', { 'id' => 1 }){ true }
         | 
| 69 | 
            +
                  @responses.add('test'){ true }
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                should "remove responses given a name and optional params" do
         | 
| 73 | 
            +
                  subject.remove('test', { 'id' => 1 })
         | 
| 74 | 
            +
                  assert_nil subject.find('test', { 'id' => 1 })
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                  subject.remove('test')
         | 
| 77 | 
            +
                  assert_nil      subject.find('test')
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: and-son
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 4 | 
            +
              hash: 21
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 | 
            +
              - 2
         | 
| 8 9 | 
             
              - 1
         | 
| 9 | 
            -
               | 
| 10 | 
            -
              version: 0.1.1
         | 
| 10 | 
            +
              version: 0.2.1
         | 
| 11 11 | 
             
            platform: ruby
         | 
| 12 12 | 
             
            authors: 
         | 
| 13 13 | 
             
            - Collin Redding
         | 
| @@ -16,7 +16,7 @@ autorequire: | |
| 16 16 | 
             
            bindir: bin
         | 
| 17 17 | 
             
            cert_chain: []
         | 
| 18 18 |  | 
| 19 | 
            -
            date:  | 
| 19 | 
            +
            date: 2013-02-05 00:00:00 Z
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 22 22 | 
             
              prerelease: false
         | 
| @@ -85,6 +85,7 @@ files: | |
| 85 85 | 
             
            - lib/and-son/connection.rb
         | 
| 86 86 | 
             
            - lib/and-son/exceptions.rb
         | 
| 87 87 | 
             
            - lib/and-son/response.rb
         | 
| 88 | 
            +
            - lib/and-son/stored_responses.rb
         | 
| 88 89 | 
             
            - lib/and-son/version.rb
         | 
| 89 90 | 
             
            - test/helper.rb
         | 
| 90 91 | 
             
            - test/support/fake_server.rb
         | 
| @@ -92,6 +93,7 @@ files: | |
| 92 93 | 
             
            - test/unit/and-son_test.rb
         | 
| 93 94 | 
             
            - test/unit/client_test.rb
         | 
| 94 95 | 
             
            - test/unit/response_test.rb
         | 
| 96 | 
            +
            - test/unit/stored_responses_test.rb
         | 
| 95 97 | 
             
            homepage: https://github.com/redding/and-son
         | 
| 96 98 | 
             
            licenses: []
         | 
| 97 99 |  | 
| @@ -132,3 +134,4 @@ test_files: | |
| 132 134 | 
             
            - test/unit/and-son_test.rb
         | 
| 133 135 | 
             
            - test/unit/client_test.rb
         | 
| 134 136 | 
             
            - test/unit/response_test.rb
         | 
| 137 | 
            +
            - test/unit/stored_responses_test.rb
         |