Class: BillysBilling::Client

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/billys_billing/client.rb

Overview

Wrapper for the BillysBilling REST API

Instance Method Summary collapse

Methods included from Request

#delete_request, #get_request, #post_request, #put_request

Constructor Details

#initialize(attrs = {}) ⇒ BillysBilling::Client

Initializes a new API object

Parameters:

  • attrs (Hash) (defaults to: {})


22
23
24
25
26
27
# File 'lib/billys_billing/client.rb', line 22

def initialize(attrs={})
  attrs = BillysBilling.options.merge(attrs)
  Config::VALID_OPTIONS_KEYS.each do |key|
    instance_variable_set("@#{key}".to_sym, attrs[key])
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/billys_billing/client.rb', line 29

def method_missing(method, *arguments, &block)
  # the first argument is a Symbol, so you need to_s it if you want to pattern match
  if method.to_s =~ /^(#{valid_actions.join("|")})_(#{valid_queries.join("s?|")})(!?)/
    send("#{$1}#{$3}", *[$2.billyfy].concat(arguments), &block)
  else
    super
  end
end

Instance Method Details

#create(class_name, params, options = {}) ⇒ Object Also known as: new, add



99
100
101
102
103
104
105
106
# File 'lib/billys_billing/client.rb', line 99

def create(class_name, params, options={})
  response = post_request("/#{class_name}", params, options)
  if response.success?
    show(class_name, response["id"], options)
  else
    options[:return_error_message] ? response["error"] : false
  end
end

#create!(class_name, params, options = {}) ⇒ Object Also known as: new!, add!



57
58
59
# File 'lib/billys_billing/client.rb', line 57

def create!(class_name, params, options={})
  create(class_name, params, options.merge({return_error_message: true}))
end

#destroy(class_name, id, options = {}) ⇒ Object Also known as: delete



119
120
121
122
123
124
125
126
127
# File 'lib/billys_billing/client.rb', line 119

def destroy(class_name, id, options={})
  item = show(class_name, id, options)
  response = delete_request("/#{class_name}/#{id}", options)
  if response.success?
    return item
  else
    options[:return_error_message] ? response["error"] : false
  end
end

#destroy!(class_name, id, options = {}) ⇒ Object Also known as: delete!



67
68
69
# File 'lib/billys_billing/client.rb', line 67

def destroy!(class_name, id, options={})
  destroy(class_name, id, options.merge({return_error_message: true}))
end

#index(class_name, params = {}, options = {}) ⇒ Object Also known as: list



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/billys_billing/client.rb', line 83

def index(class_name, params={}, options={})
  params = {q: params} if params.is_a?(String)
  list = []
  response = get_request("/#{class_name}", params, options)
  if response.success?
    response[class_name.rubyify].each do |instance|
      list << get_class_instance(class_name, instance)
    end
    return list
  else
    options[:return_error_message] ? response["error"] : false
  end
  
end

#index!(class_name, params = {}, options = {}) ⇒ Object Also known as: list!



52
53
54
# File 'lib/billys_billing/client.rb', line 52

def index!(class_name, params={}, options={})
  index(class_name, params, options.merge({return_error_message: true}))
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/billys_billing/client.rb', line 38

def respond_to?(method, include_private=false)
  if method.to_s =~ /^(#{valid_actions.join("|")})_(#{valid_queries.join("|")})/
    true
  else
    super
  end
end

#show(class_name, id, options = {}) ⇒ Object Also known as: find, get



72
73
74
75
76
77
78
79
# File 'lib/billys_billing/client.rb', line 72

def show(class_name, id, options={})
  response = get_request("/#{class_name}/#{id}", options)
  if response.success?
    get_class_instance(class_name, response)
  else
    options[:return_error_message] ? response["error"] : false
  end
end

#show!(class_name, id, options = {}) ⇒ Object Also known as: find!, get!



46
47
48
# File 'lib/billys_billing/client.rb', line 46

def show!(class_name, id, options={})
  show(class_name, id, options.merge({return_error_message: true}))
end

#update(class_name, id, params, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/billys_billing/client.rb', line 110

def update(class_name, id, params, options={})
  response = put_request("/#{class_name}/#{id}", params, options)
  if response.success?
    show(class_name, id, options)
  else
    options[:return_error_message] ? response["error"] : false
  end
end

#update!(class_name, id, params, options = {}) ⇒ Object



63
64
65
# File 'lib/billys_billing/client.rb', line 63

def update!(class_name, id, params, options={})
  update(class_name, id, params, options.merge({return_error_message: true}))
end