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 = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stackmob/client.rb', line 36

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

  args = [method, request_path, request_body]
  args << {"Content-Type" => "application/json"} if [:post, :put].include?(method)
  response = @oauth_client.send(*args)

  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