centrum_faktur 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Ruby client for [Centrum Faktur API](http://centrumfaktur.pl/api/)
4
4
 
5
+ ## Installation ##
6
+
7
+ ```
8
+ gem install centrum_faktur
9
+ ```
10
+
5
11
  ## Configuration ##
6
12
 
7
13
  ``` ruby
@@ -14,6 +20,133 @@ CentrumFaktur.configure do |config|
14
20
  end
15
21
  ```
16
22
 
23
+ ## Usage ##
24
+
25
+ Requests return Array or Hash, where keys are strings.
26
+ When other format than json (default) or yaml is specified, response is not parsed.
27
+ So for xml and pickle requests string is returned.
28
+
29
+ ``` ruby
30
+ CentrumFaktur::Invoice.show("/api/1.0/invoices/1/", :format => :xml)
31
+ ```
32
+
33
+ All params that respond to `strftime` (i.e. Date, Time) will be normalized to format
34
+ required by API, that is: `"YYYY-MM-DD"`
35
+
36
+ Writing invoice to pdf can be done as follows:
37
+
38
+ ``` ruby
39
+ File.open("my-invoice.pdf", "w") { |file| file.write(CentrumFaktur::Invoice.show("/api/1.0/invoices/1/", :format => :pdf)) }
40
+ ```
41
+
42
+ ### Account ###
43
+
44
+ Only listing accounts is supported via API
45
+
46
+ ``` ruby
47
+ CentrumFaktur::Account.list
48
+ ```
49
+
50
+ ### Comment ###
51
+
52
+ Listing all comments:
53
+
54
+ ``` ruby
55
+ CentrumFaktur::Comment.list
56
+ ```
57
+
58
+ Or listing comments for given resource:
59
+
60
+ ``` ruby
61
+ CentrumFaktur::Comment.list("/api/1.0/estimates/1/comments/")
62
+ ```
63
+
64
+ Creating comment:
65
+
66
+ You must pass path to resource comment and required attributes:
67
+
68
+ ``` ruby
69
+ CentrumFaktur::Comment.create("/api/1.0/estimates/1/comments/", {:body => "cool", :is_public => false})
70
+ ```
71
+
72
+ ### Estimate ###
73
+
74
+ Listing all estimates:
75
+
76
+ ``` ruby
77
+ CentrumFaktur::Estimate.list
78
+ ```
79
+
80
+ Monitoring estimate changes (with optional filter param):
81
+
82
+ ``` ruby
83
+ CentrumFaktur::Estimate.list_updates(:updated_since => "2012-01-12")
84
+ ```
85
+
86
+ Creating estimate (check required attributes in API description):
87
+
88
+ ``` ruby
89
+ CentrumFaktur::Estimate.create({})
90
+ ```
91
+
92
+ Updating estimate:
93
+
94
+ ``` ruby
95
+ CentrumFaktur::Estimate.update("/api/1.0/estimates/1/", {})
96
+ ```
97
+
98
+ Removing estimate:
99
+
100
+ ``` ruby
101
+ CentrumFaktur::Estimate.destroy("/api/1.0/estimates/1/")
102
+ ```
103
+
104
+ ### Invoice ###
105
+
106
+ Listing all invoices:
107
+
108
+ ``` ruby
109
+ CentrumFaktur::Invoice.list
110
+ ```
111
+
112
+ Monitoring invoice changes:
113
+
114
+ ``` ruby
115
+ CentrumFaktur::Invoice.list_updates
116
+ ```
117
+
118
+ Displaying invoice:
119
+
120
+ ``` ruby
121
+ CentrumFaktur::Invoice.show("/api/1.0/invoices/1/")
122
+ ```
123
+
124
+ Creating invoice (check required attributes in API description):
125
+
126
+ ``` ruby
127
+ CentrumFaktur::Invoice.create({})
128
+ ```
129
+
130
+ Updating invoice:
131
+
132
+ ``` ruby
133
+ CentrumFaktur::Invoice.update("/api/1.0/invoices/1/", {})
134
+ ```
135
+
136
+ Removing invoice:
137
+
138
+ ``` ruby
139
+ CentrumFaktur::Invoice.destroy("/api/1.0/invoices/1/")
140
+ ```
141
+
142
+ ### User ###
143
+
144
+ Only listing users is supported via API
145
+
146
+ ``` ruby
147
+ CentrumFaktur::User.list
148
+ ```
149
+
17
150
  ## Copyright ##
18
151
 
19
152
  Created during development for [Ragnarson](http://ragnarson.com/)
@@ -1,11 +1,11 @@
1
1
  class CentrumFaktur::Comment
2
- def self.list(options = {})
3
- request = CentrumFaktur::Connection.new.get("/api/1.0/comments/", options)
2
+ def self.list(comment_uri = "/api/1.0/comments/", options = {})
3
+ request = CentrumFaktur::Connection.new.get(comment_uri, options)
4
4
  request.handle_response
5
5
  end
6
6
 
7
- def self.create(params)
8
- request = CentrumFaktur::Connection.new.post("/api/1.0/comments/", params)
7
+ def self.create(comment_uri, params)
8
+ request = CentrumFaktur::Connection.new.post(comment_uri, params)
9
9
  request.handle_response
10
10
  end
11
11
  end
@@ -43,7 +43,7 @@ class CentrumFaktur::Connection
43
43
  @path = URI.parse(to).to_s
44
44
  request = Net::HTTP::Post.new(@path, headers)
45
45
  request.basic_auth(login, password)
46
- request.body = params.to_json
46
+ request.body = MultiJson.encode(normalize_params(params))
47
47
  @response = http.request(request)
48
48
  self
49
49
  end
@@ -52,7 +52,7 @@ class CentrumFaktur::Connection
52
52
  @path = URI.parse(to).to_s
53
53
  request = Net::HTTP::Put.new(@path, headers)
54
54
  request.basic_auth(login, password)
55
- request.body = params.to_json
55
+ request.body = MultiJson.encode(normalize_params(params))
56
56
  @response = http.request(request)
57
57
  self
58
58
  end
@@ -74,6 +74,24 @@ class CentrumFaktur::Connection
74
74
  URI.parse(path)
75
75
  end
76
76
 
77
+ def normalize_params(params)
78
+ params.inject({}) do |normalized, (key, value)|
79
+ normalized[key] =
80
+ if value.is_a?(Hash)
81
+ normalize_params(value)
82
+ elsif value.is_a?(Array)
83
+ value.map { |v| v.is_a?(Hash) ? normalize_params(v) : normalize_value(v) }
84
+ else
85
+ normalize_value(value)
86
+ end
87
+ normalized
88
+ end
89
+ end
90
+
91
+ def normalize_value(value)
92
+ value.respond_to?(:strftime) ? value.strftime("%Y-%m-%d") : value
93
+ end
94
+
77
95
  def parse_response
78
96
  case format.to_sym
79
97
  when :json
@@ -19,6 +19,11 @@ class CentrumFaktur::Estimate
19
19
  request.handle_response
20
20
  end
21
21
 
22
+ def self.update(estimate_uri, params)
23
+ request = CentrumFaktur::Connection.new.put(estimate_uri, params)
24
+ request.handle_response
25
+ end
26
+
22
27
  def self.destroy(estimate_uri)
23
28
  request = CentrumFaktur::Connection.new.delete(estimate_uri)
24
29
  request.handle_response
@@ -19,6 +19,11 @@ class CentrumFaktur::Invoice
19
19
  request.handle_response
20
20
  end
21
21
 
22
+ def self.update(invoice_uri, params)
23
+ request = CentrumFaktur::Connection.new.put(invoice_uri, params)
24
+ request.handle_response
25
+ end
26
+
22
27
  def self.destroy(invoice_uri)
23
28
  request = CentrumFaktur::Connection.new.delete(invoice_uri)
24
29
  request.handle_response
@@ -1,3 +1,3 @@
1
1
  module CentrumFaktur
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -27,4 +27,38 @@ describe "Connection" do
27
27
  it "should return url to custom profile" do
28
28
  assert_equal "https://fake.centrumfaktur.pl", CentrumFaktur::Connection.new.uri.to_s
29
29
  end
30
+
31
+ it "should normalize date to required format" do
32
+ normalized = CentrumFaktur::Connection.new.normalize_value(Time.mktime(2012, 01, 01))
33
+ assert_equal "2012-01-01", normalized
34
+ end
35
+
36
+ it "should not normalize not Date values" do
37
+ normalized = CentrumFaktur::Connection.new.normalize_value("value")
38
+ assert_equal "value", normalized
39
+ end
40
+
41
+ it "should normalize params hash" do
42
+ params = {:body => "value", :date => Time.mktime(2012, 01, 01)}
43
+ normalized = CentrumFaktur::Connection.new.normalize_params(params)
44
+ assert_equal({:body => "value", :date => "2012-01-01"}, normalized)
45
+ end
46
+
47
+ it "should normalize nested params hash" do
48
+ params = {:body => "value", :nested => {:date => Time.mktime(2012, 01, 01)}}
49
+ normalized = CentrumFaktur::Connection.new.normalize_params(params)
50
+ assert_equal({:body => "value", :nested => {:date => "2012-01-01"}}, normalized)
51
+ end
52
+
53
+ it "should normalize nested array params hash" do
54
+ params = {:body => "value", :items => [:date1 => Time.mktime(2012, 01, 01), :date2 => Time.mktime(2012, 01, 02)]}
55
+ normalized = CentrumFaktur::Connection.new.normalize_params(params)
56
+ assert_equal({:body => "value", :items => [:date1 => "2012-01-01", :date2 => "2012-01-02"]}, normalized)
57
+ end
58
+
59
+ it "should normalize nested array values" do
60
+ params = {:body => "value", :items => [Time.mktime(2012, 01, 01), "value"]}
61
+ normalized = CentrumFaktur::Connection.new.normalize_params(params)
62
+ assert_equal({:body => "value", :items => ["2012-01-01", "value"]}, normalized)
63
+ end
30
64
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: centrum_faktur
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Wojciech Wn\xC4\x99trzak"