Class: DevMate::DevMate

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dev_mate.rb

Class Method Summary collapse

Class Method Details

.CreateCustomer(email, last_name: nil, first_name: nil, note: nil) ⇒ Object

Raises:



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
67
68
69
70
# File 'lib/dev_mate.rb', line 36

def self.CreateCustomer(email, last_name: nil, first_name: nil, note: nil)
  data = { :email => email }
  data[last_name] = last_name if last_name
  data[first_name] = first_name if first_name
  data[note] = note if note
  body = { :data => data }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  options = { :body => body.to_json, :headers => { "Authorization" => @@auth_header } }
  response = self.post('/customers/', options)

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 409
        raise ConflictError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      else
        raise UnknownError
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end

.CreateLicense(customer, licenseId) ⇒ Object

Raises:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dev_mate.rb', line 179

def self.CreateLicense(customer, licenseId)
  id = customer["id"]
  data = { :data => { :license_type_id => licenseId } }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.post("/customers/#{id}/licenses", :body => data.to_json, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end

.FindCustomerById(id) ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dev_mate.rb', line 72

def self.FindCustomerById(id)

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.get("/customers/#{id}", :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        return nil
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end

.FindCustomerWithFilters(email: nil, first_name: nil, last_name: nil, license_key: nil, identifier: nil, order_id: nil, activation_id: nil, invoice: nil, offset: nil, limit: nil, with: nil) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dev_mate.rb', line 98

def self.FindCustomerWithFilters(email: nil, first_name: nil, last_name: nil, license_key: nil, identifier: nil, order_id: nil, activation_id: nil, invoice: nil, offset: nil, limit: nil, with: nil)

  query = {}
  query["filter[email]"] = email if email
  query["filter[first_name]"] = first_name if first_name
  query["filter[last_name]"] = last_name if last_name
  query["filter[license_key]"] = license_key if license_key
  query["filter[identifier]"] = identifier if identifier
  query["filter[order_id]"] = order_id if order_id
  query["filter[activation_id]"] = activation_id if activation_id
  query["filter[invoice]"] = invoice if invoice
  query["offset"] = offset if offset
  query["limit"] = limit if limit
  query["with"] = with if with

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.get("/customers", :query => query, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        return []
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end

.SetToken(token) ⇒ Object



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

def self.SetToken(token)
  @@token = token
  @@auth_header = "Token #{token}"
end

.TokenObject



32
33
34
# File 'lib/dev_mate.rb', line 32

def self.Token
  @@token
end

.UpdateCustomer(customer) ⇒ Object

Raises:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dev_mate.rb', line 137

def self.UpdateCustomer(customer)

  return nil unless customer['id']

  id = customer['id']

  new_customer = {}
  new_customer[:email] = customer["email"] if customer["email"]
  new_customer[:first_name] = customer["first_name"] if customer["first_name"]
  new_customer[:last_name] = customer["last_name"] if customer["last_name"]
  new_customer[:notes] = customer["notes"] if customer["notes"]

  data = { :data => new_customer }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.put("/customers/#{id}", :body => data.to_json, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        raise NotFoundError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 409
        raise ConflictError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end