1b491994d6011d720e332af6e1ffd03b

this is an access library to a database shard my company is working on. I'm trying to do the right thing by modularizing it, but have my doubts that it is well done. Also, requiring this code is just a matter of including the module in some other class... right? (total n00b here)

thanks for any input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Anubis
	module ShardAccess
	
		require "net/http"
		
		LIB_VERSION = 'r1b'
		
		class Read
			
			attr_accessor :path, :body, :headers
			
			def initialize( path_in, body_in, accept='xml', limit=10 )
				self.path = path_in
				self.body = body_in
				self.headers = {  
					'Interface' => 'S1',
					'Accept' => accept,
					'Limit' => limit.to_s,
					'Content-Type' => 'application/x-www-form-urlencoded',
					'User-Agent' => 'RB_SHARD_ACCESS_LIB_' + Anubis::ShardAccess::LIB_VERSION
				}
			end
			
			def call ( host, port )
				http = Net::HTTP.new( host, port )
				resp = http.post2( @path, @body, @headers )
			end
		end
	end
end

host = 'IP.AD.RE.SS'
port = 10001
path = '/message'
body = "query_string"

req = Anubis::ShardAccess::Read.new( path, body, 'csv', 1 )

data = req.call( host, port )

puts data.body

Refactorings

No refactoring yet !

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 2, 2009, April 02, 2009 23:02, permalink

1 rating. Login to rate!

Just refactoring exactly what you have (could be refactored much more of course) and below are some tips

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# no need to put this within the module body as it will be loaded regardless
require 'net/http'

module Anubis
  module ShardAccess
    
    LIB_VERSION = 'r1b'
    
    class Read
      attr_accessor :path, :body, :headers
      
      #usually past a few args you should provide an options has to make things more readable
      # such as Read.new foo, bar, :accept => :csv, :limit => 1
      def initialize path, body, options = {}
        @path, @body = path, body
        @headers = {  
          'Interface' => 'S1',
          'Accept' => options.fetch(:accept, :xml), # Hash#fetch will use the keys value when found, otherwise the second argument is used (:xml)
          'Limit' => options.fetch(:limit, 10),
          'Content-Type' => 'application/x-www-form-urlencoded',
          'User-Agent' => 'RB_SHARD_ACCESS_LIB_' + LIB_VERSION
        }
      end
      
      def call host, port
        Net::HTTP.new(host, port).post2 path, body, headers
      end
    end
  end
end
1b491994d6011d720e332af6e1ffd03b

bruno antunes

April 3, 2009, April 03, 2009 09:21, permalink

No rating. Login to rate!

Thanks for the input, looks much more Ruby-ish now :)

1b491994d6011d720e332af6e1ffd03b

bruno antunes

April 3, 2009, April 03, 2009 09:41, permalink

No rating. Login to rate!

However, since the net/http module uses #Strip on the header fields, this is the solution that I got to make it work. No symbols and I must convert the limit Integer to string - looks kludgy, but works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Anubis
  module ShardAccess

    require "net/http"

    LIB_VERSION = 'r1b'

    class Read

      attr_accessor :path, :body, :headers

      def initialize( path, body, options = {} )
        @path, @body = path, body
        @headers = {
          'Interface' => 'S1',
          'Accept' => options.fetch(:accept, "xml"),
          'Limit' => options.fetch(:limit, 10).to_s,
          'Content-Type' => 'application/x-www-form-urlencoded',
          'User-Agent' => 'RB_SHARD_ACCESS_LIB_' + LIB_VERSION
        }
      end

      def call ( host, port )
        Net::HTTP.new( host, port ).post2( path, body, headers )
      end
    end
  end
end

# with a call being 

req = Anubis::ShardAccess::Read.new( path, body, :accept => 'json', :limit => 2 )
data = req.call( host, port )

Your refactoring





Format Copy from initial code

or Cancel