Class: Rubervu::Ubervu

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

Overview

Main Ubervu Class You use it by doing the following:

ubervu = Rubervu::Ubervu.new('API_KEY')

Next you can call the Ubervu subclasses like:

result = ubervu.resources.show(url)

Direct Known Subclasses

UbervuReaction, UbervuResource

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_url = 'http://api.contextvoice.com') ⇒ Ubervu

Initializes the Ubervu Class.



16
17
18
19
# File 'lib/rubervu/ubervu.rb', line 16

def initialize(api_key, api_url = 'http://api.contextvoice.com')
  @api_key    = api_key
  @api_url    = api_url
end

Instance Method Details

#reactionsObject

Maps UbervuReaction. See UbervuReaction for more information about methods



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

def reactions
  UbervuReaction.new(@api_key, @api_url)
end

#request(resource, function, params, method = 'get', post_data = nil, api_key = @api_key, api_url = @api_url) ⇒ Object

Processes the request. Creates the API call and returns a JSON parsed result.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubervu/ubervu.rb', line 32

def request(resource, function, params, method = 'get', post_data = nil, api_key = @api_key, api_url = @api_url)
  rq = "#{api_url}/#{API_VER}/#{resource}/#{function}?apikey=#{CGI.escape api_key}&format=#{CGI.escape FORMAT}"

  if params and params.size > 0 
    params.each_pair do |key, value|
      rq += "&#{key}=#{CGI.escape value}"
    end
  end
   
  new_post_data = []
  if post_data and post_data.size > 0
    post_data.each_pair do |key, value|
      new_post_data.push("#{key}=#{CGI.escape value}")
    end
  end
  post_data = new_post_data.join('&')

  result = case method
             when 'get'
               get(rq)
             when 'post'
               post(rq, post_data)
             when 'put'
               put(rq, post_data)
             when 'delete'
               delete(rq)
           end

  case result
    when Net::HTTPSuccess
      JSON.parse(result.body)
    else
      result.error!
  end
end

#resourcesObject

Maps UbervuResource. See UbervuResource for more information about methods.



22
23
24
# File 'lib/rubervu/ubervu.rb', line 22

def resources
  UbervuResource.new(@api_key, @api_url)
end