Class: Cloverly

Inherits:
Object
  • Object
show all
Defined in:
lib/cloverly.rb,
lib/cloverly/version.rb

Defined Under Namespace

Modules: Base Classes: Account, Error, Estimate, Purchase, Source

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Cloverly

Returns a new instance of Cloverly.



29
30
31
# File 'lib/cloverly.rb', line 29

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/cloverly.rb', line 5

def api_key
  @api_key
end

Class Method Details

.accountObject



16
17
18
# File 'lib/cloverly.rb', line 16

def 
  Cloverly::Account.parse(default, default.get('/2019-03-beta/account'))
end

.defaultObject



12
13
14
# File 'lib/cloverly.rb', line 12

def default
  @@default
end

.default=(cloverly) ⇒ Object



8
9
10
# File 'lib/cloverly.rb', line 8

def default=(cloverly)
  @@default = cloverly
end

.estimate_offset(type, args = {}) ⇒ Object



20
21
22
# File 'lib/cloverly.rb', line 20

def estimate_offset(type, args = {})
  Cloverly::Estimate.parse(default, default.post("/2019-03-beta/estimates/#{type}", args))
end

.offset(type, args = {}) ⇒ Object



24
25
26
# File 'lib/cloverly.rb', line 24

def offset(type, args = {})
  Cloverly::Purchase.parse(default, default.post("/2019-03-beta/purchases/#{type}", args))
end

Instance Method Details

#accountObject



33
34
35
# File 'lib/cloverly.rb', line 33

def 
  Cloverly::Account.parse(self, get('/2019-03-beta/account'))
end

#delete(path) ⇒ Object



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

def delete(path)
  response = conn.delete do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response.is_a?(Array) || json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end

#estimate_offset(type, args = {}) ⇒ Object



37
38
39
# File 'lib/cloverly.rb', line 37

def estimate_offset(type, args = {})
  Cloverly::Estimate.parse(self, post("/2019-03-beta/estimates/#{type}", args))
end

#get(path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloverly.rb', line 62

def get(path)
  response = conn.get do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response.is_a?(Array) || json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end

#offset(type, args = {}) ⇒ Object



41
42
43
# File 'lib/cloverly.rb', line 41

def offset(type, args = {})
  Cloverly::Purchase.parse(self, post("/2019-03-beta/purchases/#{type}", args))
end

#post(path, data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cloverly.rb', line 45

def post(path, data)
  response = conn.post do |req|
    req.url path
    req.body = data.to_json
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end