Sha256: 1b78e1cbd1efba21551efdc00b8f172672ad70463a433c4afccc5504a434c463

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "httparty"
require "json"

module Astrobot
  class Client

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

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

    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 
        TransmissionApi.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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
astrobot-0.0.5 lib/astrobot/client.rb