myob-api 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0589b034162107e25124c705d7d3c56f583333c6
4
- data.tar.gz: cb6242b832c5c83c7cbce02046dbb22d266c52ba
3
+ metadata.gz: cff71416241b80eabc277970013de629ce7d7aee
4
+ data.tar.gz: d6cf941b41a1b49b7451b9a937f5ed5e37d44f2d
5
5
  SHA512:
6
- metadata.gz: ed42a4380bea398bcfb01699f5e3fb2cfabd76edd8b0a4a8daed59177e3e6d9dc0c101d5a67cefd9ff8c20a875b80119cb5b3be87e0d91039c6e38338160e974
7
- data.tar.gz: 3c8ed3b9c3e079a664c462d83fdfbf27e39d5ef1158d91b4e6fc4ceee70e3867d5af0069bd7fe5fdd645a6741d3b346d1f4c150cc4785427e020441be0f58650
6
+ metadata.gz: 899b86fd32a9205f323bdbba03981018e9d235a2005be16dde0ed611b8bce49db34d8b01ed475b5c719250e83eac249b6086679458adfcea4b5271d89a2f0bde
7
+ data.tar.gz: cca7bce1c7195aa774f089fd8672458ff8c044c5e0d9a6f3afc133e9b6e3918de7680a36f5db740624d960dc311c4078d72c536de8d80c09b358de1ffb4be795
data/README.md CHANGED
@@ -135,7 +135,15 @@ Basic pagination based on `NextPageLink` parameter returned via [API](http://dev
135
135
 
136
136
  You can also get an array of all items (which may make several API calls in the background):
137
137
 
138
- api_client.contact.all_items # note this returns an array, *not* a hash the way `api_client.contact.all` does
138
+ api_client.contact.all_items # note this returns an array, *not* a hash the way `api_client.contact.all` does - use it if you only need data, without metadata
139
+
140
+ #### Filtering
141
+
142
+ You can use oData filters:
143
+
144
+ api_client.employee.all(filter: "IsActive eq true")
145
+
146
+ See http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ and http://developer.myob.com/api/accountright/api-overview/retrieving-data/ for information on oData filtering.
139
147
 
140
148
  #### Fetching one entity
141
149
 
@@ -4,6 +4,7 @@ module Myob
4
4
  class Base
5
5
 
6
6
  API_URL = 'https://api.myob.com/accountright/'
7
+ QUERY_OPTIONS = [:orderby, :top, :skip, :filter]
7
8
 
8
9
  def initialize(client, model_name)
9
10
  @client = client
@@ -15,38 +16,39 @@ module Myob
15
16
  @model_name.to_s
16
17
  end
17
18
 
18
- def all(query = nil)
19
- perform_request(self.url, query)
19
+ def all(params = nil)
20
+ perform_request(self.url(nil, params))
21
+ end
22
+ alias_method :get, :all
23
+
24
+ def records(params = nil)
25
+ response = all(params)
26
+ response.is_a?(Hash) && response.key?('Items') ? response['Items'] : response
20
27
  end
21
28
 
22
29
  def next_page?
23
30
  !!@next_page_link
24
31
  end
25
32
 
26
- def next_page(query = nil)
27
- perform_request(@next_page_link, query)
33
+ def next_page(params = nil)
34
+ perform_request(@next_page_link, params)
28
35
  end
29
36
 
30
- def all_items(query = nil)
31
- results = all(query)["Items"]
37
+ def all_items(params = nil)
38
+ results = all(params)["Items"]
32
39
  while next_page?
33
- results += next_page(query)["Items"] || []
40
+ results += next_page(params)["Items"] || []
34
41
  end
35
42
  results
36
43
  end
37
-
38
- def get(query = nil)
39
- all(query)
40
- end
41
44
 
42
45
  def find(id)
43
46
  object = { 'UID' => id }
44
47
  perform_request(self.url(object))
45
48
  end
46
49
 
47
- def first(query = nil)
48
- model_data = self.all(query)
49
- model_data[0] if model_data.length > 0
50
+ def first(params = nil)
51
+ all(params).first
50
52
  end
51
53
 
52
54
  def save(object)
@@ -57,14 +59,19 @@ module Myob
57
59
  @client.connection.delete(self.url(object), :headers => @client.headers)
58
60
  end
59
61
 
60
- def url(object = nil)
61
- if self.model_route == ''
62
+ def url(object = nil, params = nil)
63
+ url = if self.model_route == ''
62
64
  "#{API_URL}"
63
- elsif object && object['UID']
64
- "#{resource_url}/#{object['UID']}"
65
65
  else
66
- resource_url
66
+ "#{API_URL}#{@client.current_company_file[:id]}/#{self.model_route}#{"/#{object['UID']}" if object && object['UID']}"
67
67
  end
68
+
69
+ if params.is_a?(Hash)
70
+ query = query_string(params)
71
+ url += "?#{query}" if !query.nil? && query.length > 0
72
+ end
73
+
74
+ url
68
75
  end
69
76
 
70
77
  def new_record?(object)
@@ -102,15 +109,27 @@ module Myob
102
109
  "#{API_URL}#{@client.current_company_file[:id]}/#{self.model_route}"
103
110
  end
104
111
 
105
- def perform_request(url, query = nil)
112
+ def perform_request(url)
106
113
  model_data = parse_response(@client.connection.get(url, {:headers => @client.headers}))
107
114
  @next_page_link = model_data['NextPageLink'] if self.model_route != ''
108
-
109
- if query
110
- process_query(model_data, query)
111
- else
112
- model_data
113
- end
115
+ model_data
116
+ end
117
+
118
+ def query_string(params)
119
+ params.map do |key, value|
120
+ if QUERY_OPTIONS.include?(key)
121
+ value = build_filter(value) if key == :filter
122
+ key = "$#{key}"
123
+ end
124
+
125
+ "#{key}=#{CGI.escape(value.to_s)}"
126
+ end.join('&')
127
+ end
128
+
129
+ def build_filter(value)
130
+ return value unless value.is_a?(Hash)
131
+
132
+ value.map { |key, value| "#{key} eq '#{value.to_s.gsub("'", %q(\\\'))}'" }.join(' and ')
114
133
  end
115
134
 
116
135
  def parse_response(response)
@@ -14,7 +14,7 @@ module Myob
14
14
 
15
15
  # a timesheet is identified based on an employee UID as well as its start and end date
16
16
  # it does not have a UID of its own
17
- def url(object = nil)
17
+ def url(object = nil, params = nil)
18
18
  if object
19
19
  "#{super()}/#{object['Employee']['UID']}?StartDate=#{object['StartDate']}&EndDate=#{object['EndDate']}"
20
20
  else
@@ -1,5 +1,5 @@
1
1
  module Myob
2
2
  module Api
3
- VERSION = "0.6"
3
+ VERSION = "0.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myob-api
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Lumley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 00:00:00.000000000 Z
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2