Class: Ayadn::CNX

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

Class Method Summary collapse

Class Method Details

.check(response) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ayadn/cnx.rb', line 108

def self.check response
  if response.code != 200
    res = JSON.parse(response)
    message = res['meta']['error_message']
    thor = Thor::Shell::Color.new
    puts "\n"
  end
  case response.code
  when 200
    response
  when 204
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 400
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 401
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 403
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 405
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 429
    thor.say_status :error, message.upcase, :red
    puts "\n\nAyadn made too many requests to the App.net API. You should wait at least ".color(:cyan) + "#{response.headers[:retry_after]} ".color(:red) + "seconds before trying again. Maybe you launched a lot of Ayadn instances at the same time? That's no problem, but in this case you should increase the value of the scroll timer (with `ayadn set scroll timer 5` for example). App.net allows 5000 requests per hour per account maximum.".color(:cyan)
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 500
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  when 507
    thor.say_status :error, message.upcase, :red
    Errors.global_error({error: message, caller: caller, data: [res]})
  else
    response
  end
end

.check_nr(response, url) ⇒ Object



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

def self.check_nr response, url
  case response.code
  when 200
    response
  when 204
    puts "\nError: the NiceRank filter made too many requests to the server. You may either wait for a little while before scrolling the filtered Global again, or set the scroll timer to a greater value (example: `ayadn set scroll timer 5`). (see http://ayadn-app.net/doc).\n".color(:red)
    Errors.global_error({error: "NiceRank: TOO MANY REQUESTS", caller: caller, data: [url, response.inspect, response.headers]})
  else
    response
  end
end

.delete(url) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ayadn/cnx.rb', line 148

def self.delete(url)
  begin
    RestClient.delete(url) do |response, request, result|
      Debug.http response, url
      check response
    end
  rescue SocketError, SystemCallError => e
    Thor::Shell::Color.new.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url]})
  end
end

.download(url) ⇒ Object



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
# File 'lib/ayadn/cnx.rb', line 5

def self.download url
  working = true
  begin
    RestClient.get(url) {|response, request, result| response}
  rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError, RestClient::RequestTimeout => e
    thor = Thor::Shell::Color.new
    if working == true
      working = false
      thor.say_status :error, "'#{url}' didn't respond", :red
      thor.say_status :info, "trying again in 5 secs", :yellow
      sleep 5
      retry
    end
    thor.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url]})
  end
end

.get(url) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ayadn/cnx.rb', line 31

def self.get url
  working = true
  begin
    RestClient.get(url) do |response, request, result|
      Debug.http response, url
      check_nr response, url
    end
  rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError, RestClient::RequestTimeout => e
    if working == true
      working = false
      sleep 0.5
      retry
    end
    Errors.nr "URL: #{url}"
    return {'meta' => {'code' => 666}, 'data' => "#{e}"}.to_json
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url]})
  end
end

.get_response_from(url) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ayadn/cnx.rb', line 69

def self.get_response_from(url)
  try_cnx = 1
  begin
    RestClient.get(url) do |response, request, result| #, :verify_ssl => OpenSSL::SSL::VERIFY_NONE
      Debug.http response, url
      check response
    end
  rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError, RestClient::RequestTimeout => e
    if try_cnx < 4
      try_cnx = retry_adn 10, try_cnx
      retry
    end
    Thor::Shell::Color.new.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url]})
  rescue URI::InvalidURIError => e
    Thor::Shell::Color.new.say_status :error, "connection or authorization problem", :red
    Errors.global_error({error: e, caller: caller, data: [url]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url]})
  end
end

.patch(url, payload) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ayadn/cnx.rb', line 208

def self.patch(url, payload)
  begin
    RestClient.patch(url, payload.to_json, :content_type => :json, :accept => :json) do |response, request, result|
      Debug.http response, url
      check response
    end
  rescue SocketError, SystemCallError => e
    Thor::Shell::Color.new.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  end
end

.post(url, payload = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ayadn/cnx.rb', line 168

def self.post(url, payload = nil)
  begin
    RestClient.post(url, payload.to_json, :content_type => :json, :accept => :json) do |response, request, result|
      Debug.http response, url
      check response
    end
  rescue SocketError, SystemCallError => e
    Thor::Shell::Color.new.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  end
end

.put(url, payload) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ayadn/cnx.rb', line 188

def self.put(url, payload)
  begin
    RestClient.put(url, payload.to_json, :content_type => :json, :accept => :json) do |response, request, result|
      Debug.http response, url
      check response
    end
  rescue SocketError, SystemCallError => e
    Thor::Shell::Color.new.say_status :error, "connection problem", :red
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  rescue Interrupt
    thor = Thor::Shell::Color.new
    puts "\n"
    thor.say_status :canceled, "connection canceled", :red
    puts "\n"
    exit
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [url, payload]})
  end
end

.retry_adn(seconds, try_cnx) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/ayadn/cnx.rb', line 97

def self.retry_adn seconds, try_cnx
  thor = Thor::Shell::Color.new
  thor.say_status :error, "unable to connect to App.net", :red
  thor.say_status :info, "trying again in #{seconds} seconds (#{try_cnx}/3)", :yellow
  Errors.warn "Unable to connect to App.net"
  try_cnx += 1
  sleep seconds
  puts "\e[H\e[2J"
  try_cnx
end