Class: Socketlab::SocketlabRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/socketlab/socketklab_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_user = nil, api_key = nil, api_server = nil, api_version = nil) ⇒ SocketlabRequest

Returns a new instance of SocketlabRequest.



6
7
8
9
10
11
# File 'lib/socketlab/socketklab_request.rb', line 6

def initialize(api_user = nil, api_key = nil,api_server=nil,api_version=nil)
  @api_user = api_user || Socketlab.api_user
  @api_password = api_password || Socketlab.api_password
  @api_version = api_version || Socketlab.api_version || "v1"

end

Instance Attribute Details

#api_passwordObject

Returns the value of attribute api_password.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def api_password
  @api_password
end

#api_responseObject

Returns the value of attribute api_response.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def api_response
  @api_response
end

#api_serverObject

Returns the value of attribute api_server.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def api_server
  @api_server
end

#api_userObject

Returns the value of attribute api_user.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def api_user
  @api_user
end

#api_versionObject

Returns the value of attribute api_version.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def api_version
  @api_version
end

#countObject

Returns the value of attribute count.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def count
  @count
end

#errorObject

Returns the value of attribute error.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def error
  @error
end

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def items
  @items
end

#query_paramsObject

Returns the value of attribute query_params.



3
4
5
# File 'lib/socketlab/socketklab_request.rb', line 3

def query_params
  @query_params
end

#timestampObject

Returns the value of attribute timestamp.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def timestamp
  @timestamp
end

#total_countObject

Returns the value of attribute total_count.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def total_count
  @total_count
end

#total_pagesObject

Returns the value of attribute total_pages.



4
5
6
# File 'lib/socketlab/socketklab_request.rb', line 4

def total_pages
  @total_pages
end

Instance Method Details

#set_query_params_typeObject

default response type is json



18
19
20
# File 'lib/socketlab/socketklab_request.rb', line 18

def set_query_params_type
  @query_params[:type]= @query_params[:type] || "json"
end

#set_request_optionsObject



22
23
24
25
26
27
28
# File 'lib/socketlab/socketklab_request.rb', line 22

def set_request_options
  set_query_params_type
  options= {}
  options[:query] = @query_params
  options[:basic_auth] = {:username => @api_user, :password => @api_password}
  options
end

#set_response(item_class_name) ⇒ Object

Assuming type is json



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/socketlab/socketklab_request.rb', line 31

def set_response(item_class_name)
  if @api_response.success?
    @total_count = @api_response["totalCount"]
    @total_pages = @api_response["totalPages"]
    @count = @api_response["count"]
    @timestamp = @api_response["timestamp"]
    @items = []
    unless @api_response["collection"].nil?
      @api_response["collection"].each do |attr_item|
        item = item_class_name.new
        item.set_item(attr_item)
        @items << item
      end
    end
  else
    @error = @api_response.parsed_response
  end
end

#socketlab_request(module_name) ⇒ Object



13
14
15
# File 'lib/socketlab/socketklab_request.rb', line 13

def socketlab_request(module_name)
  @api_response =HTTParty.get("https://#{BASE_URI}/#{@api_version}/#{module_name}", set_request_options)
end

#success?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/socketlab/socketklab_request.rb', line 50

def success?
  @api_response.success?
end