Class: HAPI::APICall

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ APICall

Returns a new instance of APICall.



11
12
13
# File 'lib/hapi.rb', line 11

def initialize(url)
    @uri = URI.parse(url)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/hapi.rb', line 10

def params
  @params
end

Instance Method Details

#call(method) ⇒ Object



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

def call(method)
    http = Net::HTTP.new(@uri.host, @uri.port)
    http.use_ssl = (@uri.scheme == 'https')
    if http.use_ssl
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    if method == 'post'
        request = Net::HTTP::Post.new(@uri.path)
        request.set_form_data(@params)
    else
        params = @params.keys.map { |r|
            "#{r}=#{@params[r]}"
        }.join("&")
        request = Net::HTTP::Get.new("#{@uri.path}?#{params}")
    end
    response = http.request(request)
    response = response.body.gsub(/^\<\?xml.+\?\>/, '')
    XMLObject.new(response)
end

#getObject



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

def get
    call("get")
end

#postObject



23
24
25
# File 'lib/hapi.rb', line 23

def post
    call("post")
end

#urlObject



15
16
17
# File 'lib/hapi.rb', line 15

def url
    "#{@uri.scheme}://#{@uri.host}#{@uri.path}"
end

#url=(url) ⇒ Object



19
20
21
# File 'lib/hapi.rb', line 19

def url=(url)
    @uri = URI.parse(url)
end