Module: Streak

Defined in:
lib/streak.rb,
lib/streak/box.rb,
lib/streak/file.rb,
lib/streak/user.rb,
lib/streak/util.rb,
lib/streak/field.rb,
lib/streak/stage.rb,
lib/streak/search.rb,
lib/streak/version.rb,
lib/streak/pipeline.rb,
lib/streak/field_value.rb,
lib/streak/streak_error.rb,
lib/streak/streak_object.rb

Defined Under Namespace

Modules: Util Classes: APIError, AuthenticationError, Box, Field, FieldValue, File, InvalidRequestError, Pipeline, Search, Stage, StreakError, StreakObject, User

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



27
28
29
# File 'lib/streak.rb', line 27

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



27
28
29
# File 'lib/streak.rb', line 27

def api_key
  @api_key
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



27
28
29
# File 'lib/streak.rb', line 27

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '') ⇒ Object



30
31
32
# File 'lib/streak.rb', line 30

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

.execute_request(opts) ⇒ Object



72
73
74
# File 'lib/streak.rb', line 72

def self.execute_request(opts)
  RestClient::Request.execute(opts)
end

.handle_api_error(rcode, rbody) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/streak.rb', line 76

def self.handle_api_error(rcode, rbody)
  case rcode
  when 400, 404
    raise InvalidRequestError.new("Your request is invalid: #{rbody.inspect}", rcode, rbody)
  when 401
    raise AuthenticationError.new("Your API key is invalid: #{rbody.inspect}", rcode, rbody)
  else
    raise APIError.new("API Error: #{rbody.inspect}", rcode, rbody)
  end
end

.parse(response) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/streak.rb', line 87

def self.parse(response)
  begin
    response = MultiJson.load(response.body)
  rescue MultiJson::DecodeError
    raise APIError.new("Invalid response from the API: #{response.body.inspect}")
  end
end

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



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
# File 'lib/streak.rb', line 34

def self.request(method, url, params = {}, headers = {})
  http_method = method.to_s.downcase.to_sym
  case http_method
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    payload = params.is_a?(String) ? params : uri_encode(params)

    if http_method == :post
      headers[:content_type] ||= "application/json"
    end
  end

  request_opts = {
    :headers => headers,
    :method => method,
    :verify_ssl => false,
    :url  => api_url(url),
    :user => api_key,
    :payload => payload
  }

  begin
    response = execute_request(request_opts)
    handle_api_error(response.code, response.body) unless response.code == 200
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      raise
    end
  end

  parse(response)
end