Class: Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/juxta/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, workspace, username, password) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/juxta/connection.rb', line 10

def initialize( url, workspace, username, password )

   @authtoken = "Basic #{Base64.encode64("#{username}:#{password}")}"
   @url = "#{url}/juxta"
   @workspace = workspace

   @timeout = 600        # 10 minute get timeout...
   @open_timeout = 600   # 10 minute post timeout

   opts = {
       :timeout => @timeout,
       :open_timeout=> @open_timeout
   }

   @rest_client = RestClient::Resource.new( @url, opts )
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/juxta/connection.rb', line 7

def url
  @url
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



8
9
10
# File 'lib/juxta/connection.rb', line 8

def workspace
  @workspace
end

Instance Method Details

#delete(request) ⇒ Object



88
89
90
91
92
93
# File 'lib/juxta/connection.rb', line 88

def delete( request )
   start_time = Time.now.to_f
   resp =  @rest_client[ make_url( request ) ].delete :authorization => @authtoken
   dump_time( "delete", start_time )
   return resp
end

#dump_time(what, start_time) ⇒ Object



104
105
106
# File 'lib/juxta/connection.rb', line 104

def dump_time( what, start_time )
  #puts "#{what}: %0.2f mS" % ( ( Time.now.to_f - start_time ) * 1000 )
end

#get(request) ⇒ Object



35
36
37
38
39
40
# File 'lib/juxta/connection.rb', line 35

def get( request )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].get :content_type => "application/json", :accept=>'application/json', :authorization => @authtoken
   dump_time( "get", start_time )
   JSON.parse(resp)
end

#get_html(request) ⇒ Object



42
43
44
45
46
47
# File 'lib/juxta/connection.rb', line 42

def get_html( request )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].get :content_type => "text/html", :authorization => @authtoken
   dump_time( "get", start_time )
   return resp
end

#get_ws_versionObject



27
28
29
30
31
32
33
# File 'lib/juxta/connection.rb', line 27

def get_ws_version()
   start_time = Time.now.to_f
   resp = @rest_client.get :content_type => "application/json", :authorization => @authtoken
   dump_time( "get", start_time )
   json = JSON.parse(resp)
   return json['version']
end

#get_xml(request) ⇒ Object



49
50
51
52
53
54
# File 'lib/juxta/connection.rb', line 49

def get_xml( request )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].get :content_type => "text/xml", :authorization => @authtoken
   dump_time( "get", start_time )
   return resp
end

#make_url(request) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/juxta/connection.rb', line 95

def make_url( request )
  if @workspace.length != 0
    url = "#{@workspace}/#{request}"
  else
    url = request
  end
  return url
end

#post(request, payload) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/juxta/connection.rb', line 56

def post( request, payload )
   start_time = Time.now.to_f
   if payload.nil? == true
      resp = @rest_client[ make_url( request ) ].post "", :authorization => @authtoken
   else
      resp = @rest_client[ make_url( request ) ].post payload.to_json, :authorization => @authtoken, :content_type => "application/json"
   end
   dump_time( "post", start_time )
   return resp
end

#put(request, payload) ⇒ Object



81
82
83
84
85
86
# File 'lib/juxta/connection.rb', line 81

def put( request, payload )
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( request ) ].put payload.to_json, :authorization => @authtoken, :content_type => "application/json"
   dump_time( "put", start_time )
   return resp
end

#upload_file(file_name, content_type, file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/juxta/connection.rb', line 67

def upload_file( file_name, content_type, file ) 
   opts = {
      :sourceName=> file_name,
      :contentType => content_type, 
      :sourceFile=> file, 
      :multipart => true
   }
   start_time = Time.now.to_f
   resp = @rest_client[ make_url( "source" ) ].post opts, :authorization => @authtoken
   dump_time( "post", start_time )
   json = JSON.parse(resp)
   return json[ 0 ]
end