Class: PostProxy::Client
- Inherits:
-
Object
- Object
- PostProxy::Client
- Defined in:
- lib/postproxy/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#profile_group_id ⇒ Object
readonly
Returns the value of attribute profile_group_id.
Instance Method Summary collapse
-
#initialize(api_key, base_url: DEFAULT_BASE_URL, profile_group_id: nil, faraday_client: nil) ⇒ Client
constructor
A new instance of Client.
- #posts ⇒ Object
- #profile_groups ⇒ Object
- #profiles ⇒ Object
- #request(method, path, params: nil, json: nil, data: nil, files: nil, profile_group_id: nil) ⇒ Object
- #webhooks ⇒ Object
Constructor Details
#initialize(api_key, base_url: DEFAULT_BASE_URL, profile_group_id: nil, faraday_client: nil) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/postproxy/client.rb', line 13 def initialize(api_key, base_url: DEFAULT_BASE_URL, profile_group_id: nil, faraday_client: nil) @api_key = api_key @base_url = base_url @profile_group_id = profile_group_id @faraday_client = faraday_client @posts = nil @profiles = nil @profile_groups = nil @webhooks = nil end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/postproxy/client.rb', line 11 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/postproxy/client.rb', line 11 def base_url @base_url end |
#profile_group_id ⇒ Object (readonly)
Returns the value of attribute profile_group_id.
11 12 13 |
# File 'lib/postproxy/client.rb', line 11 def profile_group_id @profile_group_id end |
Instance Method Details
#posts ⇒ Object
24 25 26 |
# File 'lib/postproxy/client.rb', line 24 def posts @posts ||= Resources::Posts.new(self) end |
#profile_groups ⇒ Object
32 33 34 |
# File 'lib/postproxy/client.rb', line 32 def profile_groups @profile_groups ||= Resources::ProfileGroups.new(self) end |
#profiles ⇒ Object
28 29 30 |
# File 'lib/postproxy/client.rb', line 28 def profiles @profiles ||= Resources::Profiles.new(self) end |
#request(method, path, params: nil, json: nil, data: nil, files: nil, profile_group_id: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/postproxy/client.rb', line 40 def request(method, path, params: nil, json: nil, data: nil, files: nil, profile_group_id: nil) url = "/api#{path}" query = {} pgid = profile_group_id || @profile_group_id query[:profile_group_id] = pgid if pgid query.merge!(params) if params response = if files conn = multipart_connection parts = [] data&.each { |k, v| parts << [k.to_s, v.to_s] } files.each do |field, filename, io, content_type| if io.nil? # Plain text part (filename holds the string value) parts << [field, filename] elsif io.is_a?(String) # Plain text part parts << [field, io] else # File upload parts << [field, Faraday::Multipart::FilePart.new(io, content_type, filename)] end end # Build payload preserving duplicate keys payload = parts.each_with_object({}) do |(key, val), h| if h.key?(key) h[key] = [h[key]] unless h[key].is_a?(Array) h[key] << val else h[key] = val end end conn.send(method, url) do |req| req.params = query unless query.empty? req.body = payload end else conn = json_connection conn.send(method, url) do |req| req.params = query unless query.empty? req.body = json.to_json if json end end handle_response(response) end |