fulfil_api 0.1.2 → 0.1.4

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
  SHA256:
3
- metadata.gz: 10fcad8a31843f9c7c15e2c0d7e9ed00331fbc665091f088f6fd7794fae7159a
4
- data.tar.gz: 367ed1508c804dc8fffc4da5089683702ca18d35433c5d6ae8744a06fae7bde7
3
+ metadata.gz: 6f8ba18240c4cbc9a772505388ca2cc1c86a5afbe6b68e28c2244d622cc2d827
4
+ data.tar.gz: a6c6864e7b9753cfb0e92c72940effc3b6b9d14845616941e63ea909c2ffe042
5
5
  SHA512:
6
- metadata.gz: 84f5db4b17bb3c6e0011f30dd5b9a8d9472199a49f2562ad1e2646540e0d08f1d89372e42fc3f4492731c6df52e4343cda4547b20dbdcafc48c388aa176780f1
7
- data.tar.gz: fa251dcc692380fa34543b6ae0d82338cc09b9cadd5d2d8875d02a02ee6c49168017808607a7508996f61ab10dc1d389b5915c7269b24792997ba65113071c44
6
+ metadata.gz: 1a6942e7212111fc5a0f62aed933a9b290c21d495588894487271623f6f2fcb767fbaaf5ccca4aa941063903934d676207af00b56ea705c32c2ee59d542f5e68
7
+ data.tar.gz: 94a91fc36310e2ad898605972262f94762c075754c74c3711762fc83400ba111b19585bd97c655dc9092dd5ab1e9d6bce6c6d465d882cbc308c20af1d0818895
data/README.md CHANGED
@@ -60,6 +60,8 @@ The following configuration options are (currently) available throught both conf
60
60
 
61
61
  - `merchant_id` (`String`): The `merchant_id` is the subdomain that the Fulfil instance is hosted on. This configuration option is required to be able to query Fulfil's API endpoints.
62
62
 
63
+ - `request_options` (`Hash`): The `request_options` are the configuration options for the HTTP client. See [https://lostisland.github.io/faraday/#/customization/request-options](https://lostisland.github.io/faraday/#/customization/request-options) in `faraday`.
64
+
63
65
  ### Querying the Fulfil API
64
66
 
65
67
  > **NOTE:** Currently, the gem is under heavy development. The querying interface of the gem is really basic at the moment. In the future, we will closer match the querying interface of `ActiveRecord`.
@@ -58,8 +58,11 @@ module FulfilApi
58
58
 
59
59
  # @return [Faraday::Connection]
60
60
  def connection
61
- # TODO: Allow passing configuration options for the request
62
- @connection ||= Faraday.new(headers: request_headers, url: api_endpoint) do |connection|
61
+ @connection ||= Faraday.new(
62
+ headers: request_headers,
63
+ url: api_endpoint,
64
+ request: configuration.request_options
65
+ ) do |connection|
63
66
  connection.adapter :net_http_persistent # TODO: Allow passing configuration options
64
67
 
65
68
  # Configuration of the request middleware
@@ -6,7 +6,10 @@ module FulfilApi
6
6
  # This model holds configuration settings and provides thread-safe access
7
7
  # to these settings.
8
8
  class Configuration
9
- attr_accessor :access_token, :api_version, :merchant_id
9
+ attr_accessor :access_token, :api_version, :merchant_id, :request_options
10
+
11
+ DEFAULT_API_VERSION = "v2"
12
+ DEFAULT_REQUEST_OPTIONS = { open_timeout: 1, read_timeout: 5, write_timeout: 5, timeout: 5 }.freeze
10
13
 
11
14
  # Initializes the configuration with optional settings.
12
15
  #
@@ -52,7 +55,8 @@ module FulfilApi
52
55
  #
53
56
  # @return [void]
54
57
  def set_default_options
55
- self.api_version = "v2" if api_version.nil?
58
+ self.api_version = DEFAULT_API_VERSION if api_version.nil?
59
+ self.request_options = DEFAULT_REQUEST_OPTIONS if request_options.nil?
56
60
  end
57
61
  end
58
62
 
@@ -12,18 +12,49 @@ module FulfilApi
12
12
  extend ActiveSupport::Concern
13
13
 
14
14
  class_methods do
15
+ # Creates a new resource on the model name.
16
+ #
17
+ # @param model_name [String] The name of the model to which the resource belongs.
18
+ # @params attributes [Hash] The attributes to create the resource with.
19
+ # @return [FulfilApi::Resource, false] The created resource.
20
+ #
21
+ # @example Creating a resource
22
+ # FulfilApi::Resource.create(model_name: "sale.sale", reference: "MK123")
23
+ def create(model_name:, **attributes)
24
+ resource = new(model_name: model_name)
25
+ resource.create(attributes)
26
+ rescue FulfilApi::Error
27
+ false
28
+ end
29
+
30
+ # Creates a new resource on the model name, raising an error if the create fails.
31
+ #
32
+ # @param model_name [String] The name of the model to which the resource belongs.
33
+ # @params attributes [Hash] The attributes to create the resource with.
34
+ # @return [FulfilApi::Resource] The created resource.
35
+ # @raise [FulfilApi::Error] If the create fails.
36
+ #
37
+ # @example Creating a resource
38
+ # FulfilApi::Resource.create!(model_name: "sale.sale", reference: "MK123")
39
+ def create!(model_name:, **attributes)
40
+ resource = new(model_name: model_name)
41
+ resource.create!(attributes)
42
+ end
43
+
15
44
  # Updates a resource by its ID and model name.
16
45
  #
17
46
  # @param id [String, Integer] The ID of the resource to update.
18
47
  # @param model_name [String] The name of the model to which the resource belongs.
19
48
  # @param attributes [Hash] The attributes to update on the resource.
20
- # @return [FulfilApi::Resource] The updated resource.
49
+ # @return [FulfilApi::Resource, false] The updated resource.
21
50
  #
22
51
  # @example Updating a resource
23
52
  # FulfilApi::Resource.update(id: 123, model_name: "sale.sale", reference: "MK123")
24
53
  def update(id:, model_name:, **attributes)
25
54
  resource = new(id: id, model_name: model_name)
26
55
  resource.update(attributes)
56
+ rescue FulfilApi::Error
57
+ false
27
58
  end
28
59
 
29
60
  # Updates a resource by its ID and model name, raising an error if the update fails.
@@ -42,6 +73,31 @@ module FulfilApi
42
73
  end
43
74
  end
44
75
 
76
+ # Creates a resource with the given attributes and saves it.
77
+ #
78
+ # @param attributes [Hash] The attributes to assign to the resource.
79
+ # @return [FulfilApi::Resource] The created resource.
80
+ #
81
+ # @example Creating a resource
82
+ # resource.create(reference: "MK123")
83
+ def create(attributes)
84
+ assign_attributes(attributes)
85
+ save
86
+ end
87
+
88
+ # Creates a resource with the given attributes and saves it, raising an error if saving fails.
89
+ #
90
+ # @param attributes [Hash] The attributes to assign to the resource.
91
+ # @return [FulfilApi::Resource] The created resource.
92
+ # @raise [FulfilApi::Error] If an error occurs during the create.
93
+ #
94
+ # @example Creating a resource with error raising
95
+ # resource.create(reference: "MK123")
96
+ def create!(attributes)
97
+ assign_attributes(attributes)
98
+ save!
99
+ end
100
+
45
101
  # Saves the current resource, rescuing any errors that occur and handling them based on error type.
46
102
  #
47
103
  # @return [FulfilApi::Resource, nil] Returns the resource if saved successfully, otherwise nil.
@@ -67,8 +123,8 @@ module FulfilApi
67
123
 
68
124
  if id.present?
69
125
  FulfilApi.client.put("/model/#{model_name}/#{id}", body: to_h)
70
- else # rubocop:disable Style/EmptyElse
71
- # TODO: Implement the {#create} and {#create!} methods to save a new resource
126
+ else
127
+ FulfilApi.client.post("/model/#{model_name}", body: to_h)
72
128
  end
73
129
 
74
130
  self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FulfilApi
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fulfil_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Vermaas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-09 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubygems_version: 3.5.18
124
+ rubygems_version: 3.5.11
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: A HTTP client to interact the Fulfil.io API