Module: Bugly

Defined in:
lib/bugly.rb,
lib/bugly/version.rb

Defined Under Namespace

Modules: APIOperations, Util Classes: APIConnectionError, APIError, APIResource, AuthenticationError, BuglyError, BuglyObject, Category, Changeset, Comment, InvalidRequestError, Issue, Label, Milestone, Page, Priority, Project, Status, User, View, Watcher

Constant Summary collapse

VERSION =
'0.1.0'
@@ssl_bundle_path =
File.join(File.dirname(__FILE__), 'data/ca-certificates.crt')
@@api_key =
nil
@@api_base =
nil
@@verify_ssl_certs =
true

Class Method Summary collapse

Class Method Details

.api_baseObject



527
# File 'lib/bugly.rb', line 527

def self.api_base; @@api_base; end

.api_base=(api_base) ⇒ Object



526
# File 'lib/bugly.rb', line 526

def self.api_base=(api_base); @@api_base = api_base; end

.api_keyObject



525
# File 'lib/bugly.rb', line 525

def self.api_key; @@api_key; end

.api_key=(api_key) ⇒ Object

def self.api_url(url=”); @@api_base; end



524
# File 'lib/bugly.rb', line 524

def self.api_key=(api_key); @@api_key = api_key; end

.api_url(url = '') ⇒ Object



522
# File 'lib/bugly.rb', line 522

def self.api_url(url=''); @@api_base + url; end

.request(method, url, api_key, params = nil, headers = {}) ⇒ Object



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/bugly.rb', line 531

def self.request(method, url, api_key, params=nil, headers={})
  api_key ||= @@api_key
  raise AuthenticationError.new('No API key provided.  (HINT: set your API key using "Bugly.api_key = <API-KEY>".  You can generate API keys from the Bugly web interface.  See http://bug.ly/docs/api for details, or email [email protected] if you have any questions.)') unless api_key
  raise APIConnectionError.new('No API base URL set.  (HINT: set your API base URL using "Bugly.api_base = <API-URL>".  Use the full URL to your account, as well as a version string, for example "https://myaccount.bug.ly/v1".  See http://bug.ly/docs/api for details, or email [email protected] if you have any questions.)') unless @@api_base

  if !verify_ssl_certs
    unless @no_verify
      $stderr.puts "WARNING: Running without SSL cert verification.  Execute 'Bugly.verify_ssl_certs = true' to enable verification."
      @no_verify = true
    end
    ssl_opts = { :verify_ssl => false }
  elsif !Util.file_readable(@@ssl_bundle_path)
    unless @no_bundle
      $stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
      @no_bundle = true
    end
    ssl_opts = { :verify_ssl => false }
  else
    ssl_opts = {
      :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
      :ssl_ca_file => @@ssl_bundle_path
    }
  end
  uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
  lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
  ua = {
    :bindings_version => Bugly::VERSION,
    :lang => 'ruby',
    :lang_version => lang_version,
    :platform => RUBY_PLATFORM,
    :publisher => 'bugly',
    :uname => uname
  }

  params = Util.objects_to_ids(params)
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    headers = { :params => params }.merge(headers)
    payload = nil
  else
    payload = params
  end

  # There's a bug in some version of activesupport where JSON.dump
  # stops working
  begin
    headers = { :x_bugly_client_user_agent => JSON.dump(ua) }.merge(headers)
  rescue => e
    headers = {
      :x_bugly_client_raw_user_agent => ua.inspect,
      :error => "#{e} (#{e.class})"
    }.merge(headers)
  end

  headers = {
    :user_agent => "Bugly/v1 RubyBindings/#{Bugly::VERSION}",
    "X-BuglyToken" => api_key,
    :accept => "application/json"
  }.merge(headers)
  opts = {
    :method => method,
    :url => self.api_url(url),
    # :user => api_key,
    :headers => headers,
    :open_timeout => 30,
    :payload => payload,
    :timeout => 80
  }.merge(ssl_opts)

  begin
    response = execute_request(opts)
  rescue SocketError => e
    self.handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      self.handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      self.handle_api_error(rcode, rbody)
    else
      self.handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    self.handle_restclient_error(e)
  end

  rbody = response.body
  rcode = response.code
  begin
    # Would use :symbolize_names => true, but apparently there is
    # some library out there that makes symbolize_names not work.
    resp = JSON.parse(rbody)
  rescue JSON::ParserError
    raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
  end

  resp = Util.symbolize_names(resp)
  [resp, api_key]
end

.verify_ssl_certsObject



529
# File 'lib/bugly.rb', line 529

def self.verify_ssl_certs; @@verify_ssl_certs; end

.verify_ssl_certs=(verify) ⇒ Object



528
# File 'lib/bugly.rb', line 528

def self.verify_ssl_certs=(verify); @@verify_ssl_certs = verify; end