Class: Astrobot::Client

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

Class Method Summary collapse

Class Method Details

.build(method, opts = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/astrobot/client.rb', line 7

def self.build(method, opts = {})
  Astrobot::Client.post(
    :method => method,
    :arguments => opts
  )
end

.http_post(opts, try_counter = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/astrobot/client.rb', line 18

def self.http_post(opts, try_counter = 0)
  post_options = {
    :body => opts.to_json,
    :headers => { "x-transmission-session-id" => Astrobot.config[:session_id] }
  }
  post_options.merge!( :basic_auth => Astrobot.config[:basic_auth]) unless 
    Astrobot.config[:basic_auth].nil?

  Astrobot::Logger.add "url: #{Astrobot.config[:url]}"
  Astrobot::Logger.add "post_body:"
  Astrobot::Logger.add JSON.parse(post_options[:body]).to_yaml
  Astrobot::Logger.add "------------------"

  response = HTTParty.post(Astrobot.config[:url], post_options)

  Astrobot::Logger.debug response

  # retry connection 3 times if session_id incorrect
  if( response.code == 409 and try_counter <= 3)
    Astrobot::Logger.add "changing session_id"
    Astrobot.configure(:session_id => response.headers["x-transmission-session-id"])
    try_counter.next
    response = http_post(opts, try_counter)
  end
  response
end

.post(opts) ⇒ Object



14
15
16
# File 'lib/astrobot/client.rb', line 14

def self.post(opts)
  JSON::parse( http_post(opts).body )
end