Class: StackMob::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stackmob/client.rb

Defined Under Namespace

Classes: BadResponseBody, InvalidRequestMethod, RequestError

Constant Summary collapse

VALID_METHODS =
[:get, :post, :put, :delete]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, app_name, app_vsn, oauth_key, oauth_secret) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
# File 'lib/stackmob/client.rb', line 29

def initialize(base_url, app_name, app_vsn, oauth_key, oauth_secret)
  self.app_name = app_name
  self.app_vsn = app_vsn

  create_oauth_client(oauth_key, oauth_secret, base_url)
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#app_vsnObject

Returns the value of attribute app_vsn.



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

def app_vsn
  @app_vsn
end

Instance Method Details

#request(method, service, path, params = {}, raw = false, headers = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stackmob/client.rb', line 36

def request(method, service, path, params = {}, raw = false, headers = {})
  request_path, request_body = generate_path_and_body(method, service, path, params, raw)

  args = [method, request_path]
  if [:post, :put].include?(method)
    headers.merge!("Content-Type" => "application/json") 
    args << request_body << headers
  else
    args << headers
  end

  response = @oauth_client.send(*args)

  if raw
    response
  else
    rcode = response.code.to_i
    if rcode >= 200 && rcode <= 299
      parse_response(response) if method != :delete
    else
      raise RequestError.new("\nReq Method: #{method}\nReq. Path: #{request_path}\nReq. Body: #{request_body}\nResp. Code: #{rcode}, Resp Body: #{response.respond_to?(:body) ? response.body : 'unknown'}")
    end
  end
end