Class: PostProxy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/postproxy/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url: DEFAULT_BASE_URL, profile_group_id: nil, faraday_client: nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/postproxy/client.rb', line 18

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
  @queues = nil
  @comments = nil
  @profile_comments = nil
  @chats = nil
  @messages = nil
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/postproxy/client.rb', line 16

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



16
17
18
# File 'lib/postproxy/client.rb', line 16

def base_url
  @base_url
end

#profile_group_idObject (readonly)

Returns the value of attribute profile_group_id.



16
17
18
# File 'lib/postproxy/client.rb', line 16

def profile_group_id
  @profile_group_id
end

Instance Method Details

#chatsObject



62
63
64
# File 'lib/postproxy/client.rb', line 62

def chats
  @chats ||= Resources::Chats.new(self)
end

#commentsObject



54
55
56
# File 'lib/postproxy/client.rb', line 54

def comments
  @comments ||= Resources::Comments.new(self)
end

#messagesObject



66
67
68
# File 'lib/postproxy/client.rb', line 66

def messages
  @messages ||= Resources::Messages.new(self)
end

#postsObject



34
35
36
# File 'lib/postproxy/client.rb', line 34

def posts
  @posts ||= Resources::Posts.new(self)
end

#profile_commentsObject



58
59
60
# File 'lib/postproxy/client.rb', line 58

def profile_comments
  @profile_comments ||= Resources::ProfileComments.new(self)
end

#profile_groupsObject



42
43
44
# File 'lib/postproxy/client.rb', line 42

def profile_groups
  @profile_groups ||= Resources::ProfileGroups.new(self)
end

#profilesObject



38
39
40
# File 'lib/postproxy/client.rb', line 38

def profiles
  @profiles ||= Resources::Profiles.new(self)
end

#queuesObject



50
51
52
# File 'lib/postproxy/client.rb', line 50

def queues
  @queues ||= Resources::Queues.new(self)
end

#request(method, path, params: nil, json: nil, data: nil, files: nil, profile_group_id: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/postproxy/client.rb', line 70

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

#webhooksObject



46
47
48
# File 'lib/postproxy/client.rb', line 46

def webhooks
  @webhooks ||= Resources::Webhooks.new(self)
end