Module: Screenbeacon

Defined in:
lib/screenbeacon.rb,
lib/screenbeacon/test.rb,
lib/screenbeacon/util.rb,
lib/screenbeacon/alert.rb,
lib/screenbeacon/project.rb,
lib/screenbeacon/version.rb,
lib/screenbeacon/api_resource.rb,
lib/screenbeacon/errors/api_error.rb,
lib/screenbeacon/api_operations/list.rb,
lib/screenbeacon/screenbeacon_object.rb,
lib/screenbeacon/api_operations/create.rb,
lib/screenbeacon/api_operations/delete.rb,
lib/screenbeacon/api_operations/update.rb,
lib/screenbeacon/api_operations/request.rb,
lib/screenbeacon/errors/screenbeacon_error.rb,
lib/screenbeacon/errors/api_connection_error.rb,
lib/screenbeacon/errors/authentication_error.rb,
lib/screenbeacon/errors/invalid_request_error.rb

Defined Under Namespace

Modules: APIOperations, Util Classes: APIConnectionError, APIError, APIResource, Alert, AuthenticationError, InvalidRequestError, Project, ScreenbeaconError, ScreenbeaconObject, Test

Constant Summary collapse

DEFAULT_CA_BUNDLE_PATH =
File.dirname(__FILE__) + '/data/ca-certificates.crt'
VERSION =
'1.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

, :connect_base, :uploads_base



46
47
48
# File 'lib/screenbeacon.rb', line 46

def api_base
  @api_base
end

.api_idObject

, :connect_base, :uploads_base



46
47
48
# File 'lib/screenbeacon.rb', line 46

def api_id
  @api_id
end

.api_tokenObject

, :connect_base, :uploads_base



46
47
48
# File 'lib/screenbeacon.rb', line 46

def api_token
  @api_token
end

.api_versionObject

, :connect_base, :uploads_base



46
47
48
# File 'lib/screenbeacon.rb', line 46

def api_version
  @api_version
end

.verify_ssl_certsObject

, :connect_base, :uploads_base



46
47
48
# File 'lib/screenbeacon.rb', line 46

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '', api_base_url = nil) ⇒ Object



49
50
51
# File 'lib/screenbeacon.rb', line 49

def self.api_url(url='', api_base_url=nil)
  (api_base_url || @api_base) + url
end

.request(method, url, api_id, api_token, params = {}, headers = {}, api_base_url = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
96
97
98
99
100
101
102
103
104
105
106
107
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
# File 'lib/screenbeacon.rb', line 53

def self.request(method, url, api_id, api_token, params={}, headers={}, api_base_url=nil)
  api_base_url = api_base_url || @api_base

  unless api_id ||= @api_id
    raise AuthenticationError.new('No API ID provided. ' \
      'Set your API ID using "Screenbeacon.api_id = <API-ID>". ' \
      'You can generate API ID from the Screenbeacon web interface. ' \
      'See https://screenbeacon.com/dashboard/settings for details, or email [email protected] ' \
      'if you have any questions.')
  end

  unless api_token ||= @api_token
    raise AuthenticationError.new('No API token provided. ' \
      'Set your API token using "Screenbeacon.api_token = <API-TOKEN>". ' \
      'You can generate API token from the Screenbeacon web interface. ' \
      'See https://screenbeacon.com/dashboard/settings for details, or email [email protected] ' \
      'if you have any questions.')
  end

  if api_id =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' \
      'whitespace. (HINT: You can double-check your API key from the ' \
      'Screenbeacon web interface. See https://screenbeacon.com/dashboard/settings for details, or ' \
      'email [email protected] if you have any questions.)')
  end

  if api_token =~ /\s/
    raise AuthenticationError.new('Your API token is invalid, as it contains ' \
      'whitespace. (HINT: You can double-check your API token from the ' \
      'Screenbeacon web interface. See https://screenbeacon.com/dashboard/settings for details, or ' \
      'email [email protected] if you have any questions.)')
  end

  if verify_ssl_certs
    request_opts = {:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
                    :ssl_ca_file => @ssl_bundle_path}
  else
    request_opts = {:verify_ssl => false}
    unless @verify_ssl_warned
      @verify_ssl_warned = true
      $stderr.puts("WARNING: Running without SSL cert verification. " \
        "You should never do this in production. " \
        "Execute 'Screenbeacon.verify_ssl_certs = true' to enable verification.")
    end
  end

  params = Util.objects_to_ids(params)
  url = api_url(url, api_base_url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    if headers[:content_type] && headers[:content_type] == "multipart/form-data"
      payload = params
    else
      payload = uri_encode(params)
    end
  end

  request_opts.update(:headers => request_headers(api_id, api_token).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

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

  [parse(response), api_id, api_token]
end