Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small refactor to extract method for determining type. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 27 additions & 31 deletions lib/yomu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,19 @@ def self.read(type, data)
result = @@server_pid ? self._server_read(type, data) : self._client_read(type, data)

case type
when :text
result
when :html
result
when :metadata
JSON.parse(result)
when :mimetype
MIME::Types[JSON.parse(result)['Content-Type']].first
when :text
result
when :html
result
when :metadata
JSON.parse(result)
when :mimetype
MIME::Types[JSON.parse(result)['Content-Type']].first
end
end

def self._client_read(type, data)
switch = case type
when :text
'-t'
when :html
'-h'
when :metadata
'-m -j'
when :mimetype
'-m -j'
end
switch = determine_type(type)

IO.popen "#{java} -Djava.awt.headless=true -jar #{Yomu::JARPATH} #{switch}", 'r+' do |io|
io.write data
Expand All @@ -55,6 +46,19 @@ def self._client_read(type, data)
end
end

def self.determine_type(type)
case type
when :text
'-t'
when :html
'-h'
when :metadata
'-m -j'
when :mimetype
'-m -j'
end
end


def self._server_read(_, data)
s = TCPSocket.new('localhost', @@server_port)
Expand Down Expand Up @@ -151,7 +155,7 @@ def mimetype
return @mimetype if defined? @mimetype

type = metadata["Content-Type"].is_a?(Array) ? metadata["Content-Type"].first : metadata["Content-Type"]

@mimetype = MIME::Types[type].first
end

Expand All @@ -163,7 +167,7 @@ def mimetype

def creation_date
return @creation_date if defined? @creation_date

if metadata['Creation-Date']
@creation_date = Time.parse(metadata['Creation-Date'])
else
Expand Down Expand Up @@ -221,19 +225,10 @@ def data
# Yomu.server(:text, 9294)
#
def self.server(type, custom_port=nil)
switch = case type
when :text
'-t'
when :html
'-h'
when :metadata
'-m -j'
when :mimetype
'-m -j'
end
switch = determine_type(type)

@@server_port = custom_port || DEFAULT_SERVER_PORT

@@server_pid = Process.spawn("#{java} -Djava.awt.headless=true -jar #{Yomu::JARPATH} --server --port #{@@server_port} #{switch}")
sleep(2) # Give the server 2 seconds to spin up.
@@server_pid
Expand Down Expand Up @@ -264,5 +259,6 @@ def self.kill_server!
def self.java
ENV['JAVA_HOME'] ? ENV['JAVA_HOME'] + '/bin/java' : 'java'
end

private_class_method :java
end