nusii 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Base
4
+
5
+ def call
6
+ if [200, 201].include?(response.status)
7
+ update_rate_limit && build_ok_response
8
+ else
9
+ update_rate_limit && raise_error_response
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def response
16
+ raise NotImplementedError,
17
+ "Each subclass must implement this method"
18
+ end
19
+
20
+ def build_ok_response
21
+ raise NotImplementedError,
22
+ "Each subclass must implement this method"
23
+ end
24
+
25
+ def resource_path
26
+ raise NotImplementedError,
27
+ "Each subclass must implement this method"
28
+ end
29
+
30
+ def update_rate_limit
31
+ Nusii.rate_limit_remaining = headers["x-ratelimit-remaining"].to_i
32
+ Nusii.rate_limit_retry_after = headers["x-ratelimit-retry-after"].to_i
33
+ end
34
+
35
+ def raise_error_response
36
+ current_error = NusiiError.error_for(status)
37
+
38
+ raise current_error.new(status, body, reason_phrase)
39
+ end
40
+
41
+ def parsed_body
42
+ @parsed_body ||= JSON.parse body
43
+ end
44
+
45
+ def resource_url_string
46
+ resource_class.class_name.underscore.pluralize
47
+ end
48
+
49
+ def connection
50
+ @connection ||= Connection.new
51
+ end
52
+
53
+ delegate :status, :body, :reason_phrase, :headers,
54
+ :to => :response
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ module Nusii
2
+ class Connection
3
+
4
+ delegate :get, :post, :put, :delete,
5
+ :to => :connection
6
+
7
+ HOST = "https://app.nusii.com"
8
+
9
+ private
10
+
11
+ def connection
12
+ @connection ||= Faraday.new(HOST) do |conn|
13
+ conn.adapter :net_http
14
+ conn.headers['Authorization'] = "Token token=#{Nusii.api_key}"
15
+ conn.headers['User-Agent'] = Nusii.user_agent
16
+ conn.headers['Content-Type'] = 'application/json'
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Create < Base
4
+
5
+ attr_reader :resource_class, :resource
6
+
7
+ def initialize resource_class, resource
8
+ @resource_class = resource_class
9
+ @resource = resource
10
+ end
11
+
12
+ private
13
+
14
+ def response
15
+ @response ||= connection.post resource_path, resource.save_params.to_json
16
+ end
17
+
18
+ def build_ok_response
19
+ builder = Utils::JsonApiBuilder.new(parsed_body['data'], parsed_body['included'])
20
+ builder.call
21
+ end
22
+
23
+ def resource_path
24
+ "/api/v2/#{resource_url_string}"
25
+ end
26
+
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Delete < Base
4
+
5
+ attr_reader :resource_class, :resource_id, :options
6
+
7
+ def initialize resource_class, resource_id, options={}
8
+ @resource_class = resource_class
9
+ @resource_id = resource_id
10
+ @options = options
11
+ end
12
+
13
+ private
14
+
15
+ def response
16
+ @response ||= connection.delete resource_path, options
17
+ end
18
+
19
+ def build_ok_response
20
+ true
21
+ end
22
+
23
+ def resource_path
24
+ "/api/v2/#{resource_url_string}/#{resource_id}"
25
+ end
26
+
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Index < Base
4
+
5
+ attr_reader :resource_class, :options
6
+
7
+ def initialize resource_class, options={}
8
+ @resource_class = resource_class
9
+ @options = options
10
+ end
11
+
12
+ private
13
+
14
+ def response
15
+ @response ||= connection.get resource_path, options
16
+ end
17
+
18
+ def build_ok_response
19
+ ResponseObject.new(resources, meta, resource_class)
20
+ end
21
+
22
+ def resources
23
+ builder = Utils::JsonApiBuilder.new(parsed_body['data'], parsed_body['included'])
24
+ builder.call
25
+ end
26
+
27
+ def meta
28
+ parsed_body['meta']
29
+ end
30
+
31
+ def resource_path
32
+ "/api/v2/#{resource_url_string}"
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class NestedCreate < Create
4
+
5
+ attr_reader :resource_class, :nested_resource_id,
6
+ :nested_resource_class, :params
7
+
8
+ def initialize resource_class, nested_resource_id, nested_resource_class, params
9
+ @resource_class = resource_class
10
+ @nested_resource_id = nested_resource_id
11
+ @nested_resource_class = nested_resource_class
12
+ @params = params
13
+ end
14
+
15
+ private
16
+
17
+ def response
18
+ @response ||= connection.post resource_path, params.to_json
19
+ end
20
+
21
+ def resource_path
22
+ "/api/v2/#{nested_resource_url_string}/#{nested_resource_id}/#{resource_url_string}"
23
+ end
24
+
25
+ def nested_resource_url_string
26
+ nested_resource_class.class_name.downcase.pluralize
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class NestedIndex < Index
4
+
5
+ attr_reader :resource_class, :nested_resource_id,
6
+ :nested_resource_class, :options
7
+
8
+ def initialize resource_class, nested_resource_id, nested_resource_class, options={}
9
+ @resource_class = resource_class
10
+ @nested_resource_id = nested_resource_id
11
+ @nested_resource_class = nested_resource_class
12
+ @options = options
13
+ end
14
+
15
+ private
16
+
17
+ def resource_path
18
+ "/api/v2/#{nested_resource_url_string}/#{nested_resource_id}/#{resource_url_string}"
19
+ end
20
+
21
+ def nested_resource_url_string
22
+ nested_resource_class.class_name.downcase.pluralize
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Send < Base
4
+
5
+ attr_reader :resource_class, :resource, :params
6
+
7
+ def initialize resource_class, resource, params
8
+ @resource_class = resource_class
9
+ @resource = resource
10
+ @params = params
11
+ end
12
+
13
+ private
14
+
15
+ def response
16
+ @response ||= connection.put resource_path, params.to_json
17
+ end
18
+
19
+ def build_ok_response
20
+ parsed_body
21
+ end
22
+
23
+ def resource_path
24
+ "/api/v2/#{resource_url_string}/#{resource.id}/send_proposal"
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Show < Base
4
+
5
+ attr_reader :resource_class, :resource_id, :options
6
+
7
+ def initialize resource_class, resource_id, options={}
8
+ @resource_class = resource_class
9
+ @resource_id = resource_id
10
+ @options = options
11
+ end
12
+
13
+ private
14
+
15
+ def response
16
+ @response ||= connection.get resource_path, options
17
+ end
18
+
19
+ def build_ok_response
20
+ builder = Utils::JsonApiBuilder.new(parsed_body['data'], parsed_body['included'])
21
+ builder.call
22
+ end
23
+
24
+ def resource_path
25
+ (resource_class.respond_to?(:resource_path) && resource_class.resource_path) ||
26
+ "/api/v2/#{resource_url_string}/#{resource_id}"
27
+ end
28
+
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ module Nusii
2
+ module ApiOperations
3
+ class Update < Base
4
+
5
+ attr_reader :resource_class, :resource
6
+
7
+ def initialize resource_class, resource
8
+ @resource_class = resource_class
9
+ @resource = resource
10
+ end
11
+
12
+ private
13
+
14
+ def response
15
+ @response ||= connection.put resource_path, resource.save_params.to_json
16
+ end
17
+
18
+ def build_ok_response
19
+ builder = Utils::JsonApiBuilder.new(parsed_body['data'], parsed_body['included'])
20
+ builder.call
21
+ end
22
+
23
+ def resource_path
24
+ "/api/v2/#{resource_url_string}/#{resource.id}"
25
+ end
26
+
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ module Nusii
2
+ class Client < Resource
3
+ extend Nusii::Crud::Get
4
+ extend Nusii::Crud::List
5
+ extend Nusii::Crud::Create
6
+ include Nusii::Crud::Save
7
+ extend Nusii::Crud::Destroy
8
+ include Nusii::Crud::DestroySelf
9
+
10
+ attr_accessor :id, :email, :name, :surname, :full_name,
11
+ :currency, :business, :locale, :pdf_page_size,
12
+ :web, :telephone, :address, :city, :postcode,
13
+ :country, :city, :state
14
+
15
+ private
16
+
17
+ def editable_attributes
18
+ [ :email, :name, :surname, :currency, :business,
19
+ :locale, :pdf_page_size, :web, :telephone, :address,
20
+ :city, :postcode, :country, :state ]
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module Nusii
2
+ module Crud
3
+ module Create
4
+
5
+ def create params, options={}
6
+ new_resource = self.new(params)
7
+ new_resource.save
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Nusii
2
+ module Crud
3
+ module Destroy
4
+
5
+ def destroy id, options={}
6
+ requester = Nusii::Request.new
7
+ requester.delete_call self, id, options
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Nusii
2
+ module Crud
3
+ module DestroySelf
4
+
5
+ def destroy options={}
6
+ raise ArgumentError, "This #{self.class.class_name} has no id" if self.id.blank?
7
+
8
+ requester = Nusii::Request.new
9
+ requester.delete_call self.class, self.id, options
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module Nusii
2
+ module Crud
3
+ module Get
4
+
5
+ def get id, options={}
6
+ requester = Nusii::Request.new
7
+ requester.show_call self, id, options
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Nusii
2
+ module Crud
3
+ module List
4
+
5
+ def list options={}
6
+ requester = Nusii::Request.new
7
+ requester.index_call self, options
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ module Nusii
2
+ module Crud
3
+ module Save
4
+
5
+ def save
6
+ self.id.present? ? update_call : create_call
7
+ end
8
+
9
+ private
10
+
11
+ def create_call
12
+ requester = Nusii::Request.new
13
+ requester.create_call self.class, self
14
+ end
15
+
16
+ def update_call
17
+ requester = Nusii::Request.new
18
+ requester.update_call self.class, self
19
+ end
20
+
21
+ end
22
+ end
23
+ end