Class: Getclicky::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/getclicky/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, params = {}) ⇒ Request

Returns a new instance of Request.



8
9
10
11
# File 'lib/getclicky/request.rb', line 8

def initialize(type, params = {})
  @type = type
  @params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/getclicky/request.rb', line 6

def params
  @params
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/getclicky/request.rb', line 6

def type
  @type
end

Instance Method Details

#build_params(type, params = {}) ⇒ Object

Build the hash of options for make resquest to API



36
37
38
39
# File 'lib/getclicky/request.rb', line 36

def build_params(type, params = {})
  query = { :site_id => Getclicky.site_id, :sitekey => Getclicky.sitekey, :type => type.to_s.gsub(/_/, '-'), :output => :json }
  query.merge(params) if params
end

#build_urlObject

Build the url for make request to API



28
29
30
31
32
# File 'lib/getclicky/request.rb', line 28

def build_url
  uri = URI.parse(Getclicky.endpoint)
  uri.query = Getclicky::Encode.encode(build_params(@type, @params))
  uri
end

#getObject

Handle all HTTP::Get request, wrapping all the logic



15
16
17
18
19
20
21
22
23
24
# File 'lib/getclicky/request.rb', line 15

def get      
  response = Net::HTTP.get_response(build_url())

  case response.code
    when Net::HTTPNotFound
      raise Getclicky::NotFoundError
    else
      Getclicky::Response.new(response.body, @params[:output])
  end
end