Class: Nozbe::ApiCall

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

Overview

Base class for all API call

  • The Nozbe API is based on JSON responses

  • This class is inspired by the deliciousr project

Constant Summary collapse

API_BASE_URL =
'http://www.nozbe.com:80/api'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, parameters = {}) ⇒ ApiCall

define the user_key and all parameters for the API-call

  • Sub-classes should override this method to provide a more user-friendly signature



26
27
28
29
# File 'lib/nozbe/api_call.rb', line 26

def initialize(key, parameters={})
  @parameters = parameters
  @parameters["key"] = key if key
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



13
14
15
# File 'lib/nozbe/api_call.rb', line 13

def action
  @action
end

#parametersObject

Returns the value of attribute parameters.



13
14
15
# File 'lib/nozbe/api_call.rb', line 13

def parameters
  @parameters
end

#required_parametersObject (readonly)

Returns the value of attribute required_parameters.



14
15
16
# File 'lib/nozbe/api_call.rb', line 14

def required_parameters
  @required_parameters
end

Class Method Details

.action(action) ⇒ Object

define the action to call (‘new_project’, ‘actions’, and so on…)

  • Sub-classes MUST define this !!



18
19
20
21
22
# File 'lib/nozbe/api_call.rb', line 18

def self.action(action)
  define_method :action do
    action.to_sym
  end
end

Instance Method Details

#build_query_stringObject

build the query string based on the given parameters

  • the Nozbe-API convention is to use ‘…/paramkey-paramvalue/…’



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nozbe/api_call.rb', line 61

def build_query_string()
  unless parameters.nil? || parameters.empty?
    query_strings = parameters.keys.sort {|a,b|
      a.to_s <=> b.to_s
    }.inject([]) do |result, element|
      value = parameters[element]
      value = value.join(";") if value.class == Array
      result << "#{element.to_s}-#{value.to_s}"
    end
    query_strings.join('/')
  else
    nil
  end
end

#build_request_pathObject

build the request path based on the given action and built query string



51
52
53
54
55
56
57
# File 'lib/nozbe/api_call.rb', line 51

def build_request_path()
  path = "/#{self.action}"
  if query_string = build_query_string
    path += "/#{query_string}"
  end
  path
end

#callObject

execute the API-Call, and return the parsed reponse



32
33
34
35
# File 'lib/nozbe/api_call.rb', line 32

def call()
  json = do_request()
  parse(json)
end

#do_requestObject

low-level method that do the request, and return the body response (without any parsing)



38
39
40
41
42
# File 'lib/nozbe/api_call.rb', line 38

def do_request()
  uri = URI.parse(API_BASE_URL + build_request_path())
  response = Net::HTTP.get_response(uri)
  response.body
end

#parse(json) ⇒ Object

method provides a default parsing strategy for the JSON-response

  • Sub-classes should override this method to provide a specific parsing of the response



46
47
48
# File 'lib/nozbe/api_call.rb', line 46

def parse(json)
  JSON.parse(json) rescue nil
end

#url_encode(param) ⇒ Object

helper method that ‘url-encode’ the given parameter and return it



77
78
79
# File 'lib/nozbe/api_call.rb', line 77

def url_encode(param)
  URI.escape(param, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end