Class: Reign::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/reign-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
# File 'lib/reign-client.rb', line 8

def initialize args
  #example endpoint: https://int-usw2.reign.io
  @endpoint = args[:endpoint] or raise ArgumentError.new("must pass endpoint")
  @endpoint = @endpoint.chomp("/")
  @api_key = args[:api_key] or raise ArgumentError.new("must pass api_key")
  @secret_key = args[:secret_key] or raise ArgumentError.new("must pass secret_key")
  @region = args[:region] || "default"
end

Instance Method Details

#acquire_lease(args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/reign-client.rb', line 115

def acquire_lease args
  uri = Reign.generate_acquire_lease_uri args
  headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)
  
  begin
    return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers))
  rescue RestClient::ExceptionWithResponse => e
	url = @endpoint + uri
	raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#add_contacts_to_watch(args) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/reign-client.rb', line 170

def add_contacts_to_watch args
  args[:action] = "add"
  if args[:sig] == nil
    raise ArgumentError.new("Must pass sig parameter to modify watch") 
  end
  return modify_watches_helper args
end

#create_watch_with_contacts(args) ⇒ Object



165
166
167
168
# File 'lib/reign-client.rb', line 165

def create_watch_with_contacts args
  args[:action] = "set"
  return modify_watches_helper args
end

#delete_key_value(args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/reign-client.rb', line 103

def delete_key_value args
  uri = Reign.generate_delete_kv_uri args 
  headers = Reign.generate_header_for_request(:method => :delete, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)      
  begin
    RestClient::Request.execute(:method => :delete, :url => @endpoint + uri, :headers => headers)
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#get_categories_in_namespace(args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/reign-client.rb', line 44

def get_categories_in_namespace args
  uri = Reign.generate_categories_uri(args[:namespace])
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
                                              :secret_key => @secret_key, :region => @region)

  begin
    return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers)) 
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}.  URL = #{url}"
  end
end

#get_key_value(args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/reign-client.rb', line 87

def get_key_value args
  uri = Reign.generate_get_kv_uri args
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)      
  begin
    response = RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers)
    sig = response.headers[:etag]
    owner_id = response.headers[:"reign_owner_id"]
    data = Base64.strict_decode64(response.to_str)
    return {:sig => sig, :kv_data => data, :owner_id => owner_id}
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#get_lease_info(args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/reign-client.rb', line 153

def get_lease_info args
  uri = Reign.generate_get_lease_uri args
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)
  begin
    return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers))
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
	raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#get_nodes_in_category(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/reign-client.rb', line 57

def get_nodes_in_category args
  uri = Reign.generate_nodes_in_category_uri args
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
                                              :secret_key => @secret_key, :region => @region)
  begin
    return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers))
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}.  URL = #{url}"
  end
end

#get_regionsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/reign-client.rb', line 32

def get_regions
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => Reign::GET_REGIONS_URI, :api_key => @api_key, 
                                              :secret_key => @secret_key, :region => @region)
  begin
    return JSON.parse(RestClient::Request.execute(:method => :get, :url => @endpoint + Reign::GET_REGIONS_URI, 
                                                  :headers => headers))
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}.  URL = #{url}"
  end
end

#get_watches(args) ⇒ Object



186
187
188
189
190
191
# File 'lib/reign-client.rb', line 186

def get_watches args
  uri = Reign.generate_watch_uri(args[:namespace], args[:watch_entity_path])
  headers = Reign.generate_header_for_request(:method => :get, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)
  return RestClient::Request.execute(:method => :get, :url => @endpoint + uri, :headers => headers)
end

#heartbeat(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reign-client.rb', line 17

def heartbeat args
  request_body = Reign.generate_heartbeat_body(args)
  request_body = JSON.generate(request_body)
  uri = Reign.generate_heartbeat_uri(args[:namespace])
  headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key, 
                                              :secret_key => @secret_key, :payload => request_body, :region => @region) 
  begin
    return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri, 
                                                  :payload => request_body, :headers => headers))
  rescue RestClient::ExceptionWithResponse => e 
    url = @endpoint + uri
    raise "Bad response from Reign server.  Message = #{e.response}.  URL = #{url}. payload = #{request_body}"
  end
end

#modify_watches_helper(args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/reign-client.rb', line 193

def modify_watches_helper args
  sig = args[:sig]
  template = Reign.generate_watch_body_template(args[:owner_id], args[:emails], args[:webhooks])
  request_payload = JSON.generate(template)

  query_params = {"op" => args[:action]}
  if sig != nil
    query_params["sig"] = sig
  end
  uri = Reign.generate_watch_uri(args[:namespace], args[:watch_entity_path]) + Reign.generate_query_string(query_params) 
  headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region, :payload => request_payload)

  begin     
    return JSON.parse(RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers,
                                                  :payload => request_payload))
  rescue => e
    url = @endpoint + uri
    raise "Bad response from Reign server.  Message: #{e.response}.  URL: #{url}. Request body: #{request_payload}"
  end
end

#release_lease(args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/reign-client.rb', line 128

def release_lease args
  uri = Reign.generate_release_lease_uri args
  headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)

  begin
    return RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers)
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
	raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#remove_contacts_from_watch(args) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/reign-client.rb', line 178

def remove_contacts_from_watch args
  args[:action] = "rm"
  if args[:sig] == nil
    raise ArgumentError.new("Must pass sig parameter to modify watch") 
  end
  return modify_watches_helper args
end

#revoke_lease(args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/reign-client.rb', line 141

def revoke_lease args
  uri = Reign.generate_revoke_lease_uri args
  headers = Reign.generate_header_for_request(:method => :post, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region)
  begin
    return RestClient::Request.execute(:method => :post, :url => @endpoint + uri, :headers => headers)
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
	raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end

#upsert_key_value(args) ⇒ Object



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

def upsert_key_value args
  if args[:kv_data] == nil
    raise ArgumentError.new("must pass some data in raw bytes for kv_data")
  end
  raw_data = Base64.strict_encode64(args[:kv_data])
  uri = Reign.generate_upsert_kv_uri args
  headers = Reign.generate_header_for_request(:method => :put, :request_uri => uri, :api_key => @api_key,
                                        :secret_key => @secret_key, :region => @region, :payload => raw_data)      
  begin
    response = RestClient::Request.execute(:method => :put, :url => @endpoint + uri, :headers => headers, 
                                     :payload => raw_data)
    return response.headers[:etag]
  rescue RestClient::ExceptionWithResponse => e
    url = @endpoint + uri
    raise "Bad response from Reign server. Message = #{e.response}. URL = #{url}"
  end
end