apisync 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3baebf434178862cf7ad0513124cfa460f1ed8c
4
- data.tar.gz: 89cfa8062601ae2a64b15a52072303789e97f027
3
+ metadata.gz: b45094cd8302949db5ed09514381ba31e9901ec4
4
+ data.tar.gz: 99ba7178a22c9a1894a9ffac0b7ec9bbc5d72e70
5
5
  SHA512:
6
- metadata.gz: 7e87c8cf404ed809e67588896489dafc1bfcba4a3fdd5e069be68d4f38b38b365f54b9fac016568e15a22c1afbd4b12815a6a03296edcde66e6a62b75f09650e
7
- data.tar.gz: c1bd720f4e8700182878c26f649c7b0ddfc10dab926159052194007c22f9646dd22859d7063c7840339058d573dd2278907b4265ef53c75f757240f732c5fb4c
6
+ metadata.gz: 9e54efe32dadb1fe4832132ffa6635c8f737042a74d218887af6d81ef5d6e46585fbc6e34a3e1e9d455f4c99e38d6af2651b193aee6bbb008c802575940e15d1
7
+ data.tar.gz: e5da6cfb47427a575b9db484382e7f491471678193790e2716f3f13ca5c3478efc4c9193001958d9921b43344e8a7f4e50325ef0c6dfcc991ba2b4b953ad041b
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Apisync
1
+ # apisync-ruby
2
2
 
3
3
  This gem gives you the tools to interact with [apisync.io](apisync.io).
4
4
 
@@ -12,6 +12,10 @@ gem 'apisync'
12
12
 
13
13
  ## Usage
14
14
 
15
+ **On Rails?** Please use
16
+ [apisync-rails](https://github.com/apisync/apisync-rails) instead. It has
17
+ automatic integration with ActiveRecord.
18
+
15
19
  ### Vanilla Ruby
16
20
 
17
21
  To create an inventory item:
@@ -21,15 +25,29 @@ client = Apisync.new(api_key: token)
21
25
  client.inventory_items.save({
22
26
  attributes: {
23
27
  ad_template_type: "vehicle",
24
- availability: "on-sale",
25
- brand: "brand",
26
- condition: "new",
27
- content_language: "pt-br"
28
- # ... more attributes
28
+ available: true,
29
+ brand: "brand",
30
+ condition: "new",
31
+ content_language: "pt-br",
32
+ reference_id: "1"
33
+
34
+ # more attributes
29
35
  }
30
36
  })
31
37
  ```
32
38
 
39
+ For details on the attributes, see the
40
+ [API Reference documentation](https://docs.apisync.io/api/).
41
+
42
+ You can also define a global API key:
43
+
44
+ ```ruby
45
+ Apisync.api_key = "my-key"
46
+
47
+ # Instantiate the client now without passing a token
48
+ client = Apisync.new
49
+ ```
50
+
33
51
  ## Development
34
52
 
35
53
  To run tests:
data/apisync.gemspec CHANGED
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "rspec", "~> 3.0"
28
28
  spec.add_development_dependency "awesome_print"
29
29
  spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "webmock"
30
31
  end
@@ -4,4 +4,5 @@ class Apisync
4
4
  # List of exceptions. They are all inherited from Apisync::Exception
5
5
  class UrlAndPayloadIdMismatch < Apisync::Exception; end
6
6
  class InvalidFilter < Apisync::Exception; end
7
+ class TooManyRequests < Apisync::Exception; end
7
8
  end
@@ -7,58 +7,58 @@ class Apisync
7
7
  "Accept" => "application/vnd.api+json"
8
8
  }.freeze
9
9
 
10
- def self.post(resource_name:, data:, options: {})
11
- url = Apisync::Http::Url.new(
12
- resource_name: resource_name,
13
- options: options
14
- )
15
- payload = payload_from_data(data)
16
- HTTParty.post(
17
- url.to_s,
18
- body: {data: payload}.to_json,
19
- headers: header(api_key: options[:api_key])
20
- )
10
+ def initialize(resource_name:, options: {})
11
+ @resource_name = resource_name
12
+ @options = options
21
13
  end
22
14
 
23
- def self.put(resource_name:, id:, data:, options: {})
15
+ def post(data:, headers: {})
16
+ wrap_response(HTTParty.post(
17
+ url,
18
+ body: {data: payload_from_data(data)}.to_json,
19
+ headers: header.merge(headers)
20
+ ))
21
+ end
22
+
23
+ def put(id:, data:, headers: {})
24
24
  raise Apisync::UrlAndPayloadIdMismatch unless id == data[:id]
25
25
 
26
- url = Apisync::Http::Url.new(
27
- resource_name: resource_name,
28
- id: id,
29
- options: options
30
- )
31
- payload = payload_from_data(data)
32
- HTTParty.put(
33
- url.to_s,
34
- body: {data: payload}.to_json,
35
- headers: header(api_key: options[:api_key])
36
- )
26
+ wrap_response(HTTParty.put(
27
+ url(id: id),
28
+ body: {data: payload_from_data(data)}.to_json,
29
+ headers: header.merge(headers)
30
+ ))
37
31
  end
38
32
 
39
- def self.get(resource_name:, id: nil, filters: nil, options: {})
33
+ def get(id: nil, filters: nil, headers: {})
40
34
  raise Apisync::InvalidFilter if !filters.nil? && !filters.is_a?(Hash)
41
35
 
42
- url = Apisync::Http::Url.new(
43
- resource_name: resource_name,
44
- id: id,
45
- filters: filters,
46
- options: options
47
- )
48
- HTTParty.get(url.to_s, headers: header(api_key: options[:api_key]))
36
+ wrap_response(HTTParty.get(
37
+ url(id: id, filters: filters),
38
+ headers: header.merge(headers)
39
+ ))
49
40
  end
50
41
 
51
42
  private
52
43
 
53
- def self.header(api_key: nil)
44
+ def url(id: nil, filters: nil)
45
+ Apisync::Http::Url.new(
46
+ resource_name: @resource_name,
47
+ id: id,
48
+ filters: filters,
49
+ options: @options
50
+ ).to_s
51
+ end
52
+
53
+ def header
54
54
  final = HEADER
55
- if api_key
56
- final = final.merge("Authorization" => "ApiToken #{api_key}")
55
+ if @options[:api_key]
56
+ final = final.merge("Authorization" => "ApiToken #{@options[:api_key]}")
57
57
  end
58
58
  final
59
59
  end
60
60
 
61
- def self.payload_from_data(data)
61
+ def payload_from_data(data)
62
62
  transformed_payload = {}
63
63
  data.each do |key, value|
64
64
  if value.is_a?(Hash)
@@ -70,5 +70,13 @@ class Apisync
70
70
  end
71
71
  transformed_payload
72
72
  end
73
+
74
+ def wrap_response(response)
75
+ if response.code.to_i == 429
76
+ raise Apisync::TooManyRequests
77
+ else
78
+ response
79
+ end
80
+ end
73
81
  end
74
82
  end
@@ -12,10 +12,12 @@ class Apisync
12
12
  #
13
13
  def save(data)
14
14
  data[:type] = @name.to_s.gsub("_", "-")
15
+ headers = data.delete(:headers) || {}
16
+
15
17
  if data[:id].nil?
16
- post(data)
18
+ post(data, headers: headers)
17
19
  else
18
- put(data)
20
+ put(data, headers: headers)
19
21
  end
20
22
  end
21
23
 
@@ -30,38 +32,41 @@ class Apisync
30
32
  # get(filters: {column_name: 'customer-id' }})
31
33
  #
32
34
  def get(conditions)
33
- http_client.get(
35
+ client = Apisync::HttpClient.new(
34
36
  resource_name: @name,
37
+ options: @options
38
+ )
39
+ client.get(
35
40
  id: conditions[:id],
36
41
  filters: conditions[:filters],
37
- options: @options
42
+ headers: conditions[:headers] || {}
38
43
  )
39
44
  end
40
45
 
41
46
  private
42
47
 
43
48
 
44
- def post(data)
45
- http_client.post(
49
+ def post(data, headers: {})
50
+ client = Apisync::HttpClient.new(
46
51
  resource_name: @name,
47
- data: data,
48
52
  options: @options
49
53
  )
54
+ client.post(
55
+ data: data,
56
+ headers: headers
57
+ )
50
58
  end
51
59
 
52
- def put(data)
53
- http_client.put(
60
+ def put(data, headers: {})
61
+ client = Apisync::HttpClient.new(
54
62
  resource_name: @name,
63
+ options: @options
64
+ )
65
+ client.put(
55
66
  id: data[:id],
56
67
  data: data,
57
- options: @options
68
+ headers: headers
58
69
  )
59
70
  end
60
-
61
- private
62
-
63
- def http_client
64
- Apisync::HttpClient
65
- end
66
71
  end
67
72
  end
@@ -1,3 +1,3 @@
1
1
  class Apisync
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apisync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre de Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-10 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description:
98
112
  email:
99
113
  - chavedomundo@gmail.com