Class: PersevereClient

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

Constant Summary collapse

HEADERS =
{ 'Accept' => 'application/json',
  'Content-Type' => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ PersevereClient

Returns a new instance of PersevereClient.



59
60
61
62
63
64
65
66
# File 'lib/persevere_client.rb', line 59

def initialize(url)
  @server_url = url
  server = URI.parse(@server_url)
  @base_uri = server.path || ''
  @persevere = Net::HTTP.new(server.host, server.port)
  @client_id = "dm-persevere-adapter-#{(rand*1000)}" # just need a small random string
  @sequence_id = 0
end

Instance Attribute Details

#persevereObject

Returns the value of attribute persevere.



57
58
59
# File 'lib/persevere_client.rb', line 57

def persevere
  @persevere
end

#server_urlObject

Returns the value of attribute server_url.



57
58
59
# File 'lib/persevere_client.rb', line 57

def server_url
  @server_url
end

Instance Method Details

#create(path, resource, headers = {}) ⇒ Object

Pass in a resource hash



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/persevere_client.rb', line 69

def create(path, resource, headers = {})
  path = @base_uri + path
  if headers.has_key?('Content-Type') && headers['Content-Type'] != 'application/json'
    json_blob = resource
  else
    json_blob = resource.delete_if{|key,value| value.nil? }.to_json
  end
  response = nil
  while response.nil?
    begin
      response = @persevere.send_request('POST', URI.encode(path), json_blob, default_headers.merge(headers))
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
          Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      puts "Persevere Create Failed: #{e}, Trying again."
    end
  end
  return PersevereResult.make(response)
end

#delete(path, headers = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/persevere_client.rb', line 117

def delete(path, headers = {})
  path = @base_uri + path
  response = nil
  while response.nil?
    begin
      response = @persevere.send_request('DELETE', URI.encode(path), nil, default_headers.merge(headers))
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
          Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      puts "Persevere Create Failed: #{e}, Trying again."
    end
  end
  return PersevereResult.make(response)
end

#retrieve(path, headers = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/persevere_client.rb', line 88

def retrieve(path, headers = {})
  path = @base_uri + path
  response = nil
  while response.nil?
    begin
      response = @persevere.send_request('GET', URI.encode(path), nil, default_headers.merge(headers))
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
          Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      puts "Persevere Retrieve Failed: #{e}, Trying again."
    end
  end
  return PersevereResult.make(response)
end

#update(path, resource, headers = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/persevere_client.rb', line 102

def update(path, resource, headers = {})
  path = @base_uri + path
  json_blob = resource.to_json
  response = nil
  while response.nil?
    begin
      response = @persevere.send_request('PUT', URI.encode(path), json_blob, default_headers.merge(headers))
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
          Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      puts "Persevere Create Failed: #{e}, Trying again."
    end
  end
  return PersevereResult.make(response)
end