Class: Castle::API
- Inherits:
-
Object
- Object
- Castle::API
- Defined in:
- lib/castle-rb/api.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http ⇒ Object
Returns the value of attribute http.
Instance Method Summary collapse
- #client_user_agent ⇒ Object
- #get_uname ⇒ Object
-
#initialize(cookie_id, ip, headers) ⇒ API
constructor
A new instance of API.
- #request(endpoint, args) ⇒ Object
Constructor Details
#initialize(cookie_id, ip, headers) ⇒ API
Returns a new instance of API.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/castle-rb/api.rb', line 5 def initialize(, ip, headers) @http = Net::HTTP.new(Castle.config.api_endpoint.host, Castle.config.api_endpoint.port) @http.read_timeout = Castle.config.request_timeout if Castle.config.api_endpoint.scheme == 'https' @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_PEER end @headers = { "Content-Type" => "application/json", "X-Castle-Cookie-Id" => , "X-Castle-Ip" => ip, "X-Castle-Headers" => headers, "X-Castle-Client-User-Agent" => JSON.generate(client_user_agent), "X-Castle-Source" => Castle.config.source_header, "User-Agent" => "Castle/v1 RubyBindings/#{Castle::VERSION}" } @headers.delete_if { |k, v| v.nil? } end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/castle-rb/api.rb', line 3 def headers @headers end |
#http ⇒ Object
Returns the value of attribute http.
3 4 5 |
# File 'lib/castle-rb/api.rb', line 3 def http @http end |
Instance Method Details
#client_user_agent ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/castle-rb/api.rb', line 73 def client_user_agent @uname ||= get_uname lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})" { :bindings_version => Castle::VERSION, :lang => 'ruby', :lang_version => lang_version, :platform => RUBY_PLATFORM, :publisher => 'castle', :uname => @uname } end |
#get_uname ⇒ Object
87 88 89 90 91 |
# File 'lib/castle-rb/api.rb', line 87 def get_uname `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i rescue Errno::ENOMEM # couldn't create subprocess "uname lookup failed" end |
#request(endpoint, args) ⇒ Object
29 30 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/castle-rb/api.rb', line 29 def request(endpoint, args) req = Net::HTTP::Post.new( "#{Castle.config.api_endpoint.path}/#{endpoint}", @headers) req.basic_auth("", Castle.config.api_secret) req.body = args.to_json begin response = @http.request(req) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Net::ReadTimeout => e raise Castle::RequestError, 'Castle API connection error' end case response.code.to_i when 200..299 # OK when 400 raise Castle::BadRequestError, response[:message] when 401 raise Castle::UnauthorizedError, response[:message] when 403 raise Castle::ForbiddenError, response[:message] when 404 raise Castle::NotFoundError, response[:message] when 419 raise Castle::UserUnauthorizedError, response[:message] when 422 raise Castle::InvalidParametersError, response[:message] else raise Castle::ApiError, response[:message] end if response.body.nil? || response.body.empty? {} else begin JSON.parse(response.body, :symbolize_names => true) rescue JSON::ParserError raise Castle::ApiError, 'Invalid response from Castle API' end end end |