Class: Wonde::Endpoints

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

Overview

Top level Endpoints class, most of our classes inherit from this Some methods use this directly

Constant Summary collapse

@@endpoint =

Main endpoint, base URI

'https://api.wonde.com/v1.0/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, uri = false) ⇒ Endpoints

Returns a new instance of Endpoints.



14
15
16
17
18
19
20
# File 'lib/endpoints.rb', line 14

def initialize(token, uri=false)
  self.endpoint = @@endpoint
  self.uri = String.new()
  self.version = '0.0.1'
  self.token = token
  self.uri = uri if uri
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



9
10
11
# File 'lib/endpoints.rb', line 9

def endpoint
  @endpoint
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/endpoints.rb', line 9

def token
  @token
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/endpoints.rb', line 9

def uri
  @uri
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/endpoints.rb', line 9

def version
  @version
end

Instance Method Details

#all(includes = Array.new(), parameters = Hash.new()) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/endpoints.rb', line 131

def all(includes = Array.new(), parameters = Hash.new())
  unless includes.nil? or includes.empty?
    parameters['include'] = includes.join(",")
  end

  unless parameters.nil? or parameters.empty?
    uriparams = Addressable::URI.new
    uriparams.query_values = parameters
    uriparams.query
    uri = self.uri + '?' + uriparams.query
  else
    uri = self.uri
  end
  response = getRequest(uri).body
  puts response if ENV["debug_wonde"]
  object = JSON.parse(response, object_class: OpenStruct)
  puts object if ENV["debug_wonde"]
  ResultIterator.new(object,self.token)
end

#delete(body = Hash.new()) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/endpoints.rb', line 123

def delete(body=Hash.new())
  hash_response = self.deleteRequest(self.uri, body).body
  if hash_response.nil?
    return Hash.new()
  end
  OpenStruct.new hash_response
end

#deleteRequest(endpoint, body = Hash.new()) ⇒ Object



101
102
103
104
105
106
# File 'lib/endpoints.rb', line 101

def deleteRequest(endpoint, body=Hash.new())
  puts "self.endpoint: " + self.endpoint if ENV["debug_wonde"]
  puts "endpoint:" + endpoint if ENV["debug_wonde"]
  puts "body:" + body.to_json if ENV["debug_wonde"]
  deleteUrl(self.endpoint + endpoint, body)
end

#deleteUrl(url, body = Hash.new()) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/endpoints.rb', line 108

def deleteUrl(url, body=Hash.new())
  puts body.to_json if ENV["debug_wonde"]
  RestClient::Request.execute(
    method: :delete,
    url: url,
    headers: {
      "Authorization" => "Basic #{self.token}",
      "User-Agent" => "wonde-rb-client-#{self.version}",
      "Accept" => "application/json",
      "Content-Type" => "application/json",
    },
    payload: body.to_json,
  )
end

#get(id, includes = Hash.new(), parameters = Hash.new()) ⇒ Object

Builds get request and passes it along

Parameters:

  • id (String)
  • includes (Hash) (defaults to: Hash.new())
  • parameters (Hash) (defaults to: Hash.new())

Returns:

  • (Object)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/endpoints.rb', line 53

def get(id, includes = Hash.new(), parameters = Hash.new())
  unless includes.nil? or includes.empty?
    parameters['include'] = includes.join(",")
  end
  unless parameters.empty?
    uriparams = Addressable::URI.new
    uriparams.query_values = parameters
    uri = self.uri + id + "?" + uriparams.query
  else
    uri = self.uri + id
  end
  response = getRequest(uri).body["data"]
  puts response if ENV["debug_wonde"]
  object = JSON.parse(response, object_class: OpenStruct)
  puts object if ENV["debug_wonde"]
  object
end

#getRequest(endpoint) ⇒ Object

Forwards request info, eventually to unirest

Parameters:

  • endpoint (String)

Returns:

  • (Object)


26
27
28
29
30
# File 'lib/endpoints.rb', line 26

def getRequest(endpoint)
  puts "self.endpoint: " + self.endpoint if ENV["debug_wonde"]
  puts "endpoint:" + endpoint if ENV["debug_wonde"]
  getUrl(self.endpoint + endpoint)
end

#getUrl(url) ⇒ Object

Forwards request info to unirest

Parameters:

  • url (String)

Returns:

  • (Object)


36
37
38
39
40
41
42
43
44
45
# File 'lib/endpoints.rb', line 36

def getUrl(url)
  RestClient::Request.execute(
    method: :get,
    url: url,
    headers: {
      "Authorization" => "Bearer #{self.token}",
      "User-Agent" => "wonde-rb-client-#{self.version}"
    }
  )
end

#post(body = Hash.new()) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/endpoints.rb', line 93

def post(body=Hash.new())
  hash_response = JSON.parse(self.postRequest(self.uri, body).body)
  if hash_response.nil?
    return Hash.new()
  end
  OpenStruct.new hash_response
end

#postRequest(endpoint, body = Hash.new()) ⇒ Object



71
72
73
74
75
76
# File 'lib/endpoints.rb', line 71

def postRequest(endpoint, body=Hash.new())
  puts "self.endpoint:\n " + self.endpoint if ENV["debug_wonde"]
  puts "endpoint:\n" + endpoint if ENV["debug_wonde"]
  puts "body:\n" + body.to_json if ENV["debug_wonde"]
  postUrl(self.endpoint + endpoint, body)
end

#postUrl(url, body = Hash.new()) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/endpoints.rb', line 78

def postUrl(url, body=Hash.new())
  puts body.to_json if ENV["debug_wonde"]
  RestClient::Request.execute(
    method: :post,
    url: url,
    headers: {
      "Authorization" => "Basic #{self.token}",
      "User-Agent" => "wonde-rb-client-#{self.version}",
      "Accept" => "application/json",
      "Content-Type" => "application/json",
    },
    payload: body.to_json,
  )
end