socialfred 0.2.1 → 0.2.2

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
  SHA256:
3
- metadata.gz: cdf0d16bc214f06cf82bbece379057ab5027a2b4332cff4bf54f967f8e93a1e8
4
- data.tar.gz: 5d53c8d181642879d89ce77d72bae239522880cf1d3904fa4f9587c0dad841c8
3
+ metadata.gz: b559cfec32ea0140e747b97aee8f920e08c8ed01871c48247086ac4579153c2e
4
+ data.tar.gz: c134c68092d6bce0560781f3460be1b793cd8f1bf5287a68eba27f0a96d94c20
5
5
  SHA512:
6
- metadata.gz: ec533b07db247b4c2d720b2b66f7c7fa0ee58284670126f5e50d30a9e261e0dcba1f161e0bea18f5b90f9f4476f3a5618b4aa5e4ade24d4365135281d2c9bf41
7
- data.tar.gz: 857112d106a002a7315ed12018e8bab8a0e3af96e461abcba9c69b004cfb1e19b32c0053e0d93fea49f04e91e7ccb1e7e3191f42faa690e339bf221f457d011c
6
+ metadata.gz: 1c797cf6b9d3f41621ba72ff80da5c14df24d6d71aeab8534a0e9b4948f17f5426c9661faff9bf300f49c22ea7ca69ee7b1072505a5f55a5ad5bc7133882fce3
7
+ data.tar.gz: 53b5c11d5db48e048db96dd45cd86455233bdfe93d16c5422cd3ef70fdd02119747d021c4bb961fcb3c8f2fe0a3b28f406404268beac4b164b99a411374b1034
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- socialfred (0.2.1)
4
+ socialfred (0.2.2)
5
5
  faraday (>= 0.9.0)
6
6
  json (~> 2.2)
7
7
 
data/README.md CHANGED
@@ -31,6 +31,8 @@ api = Socialfred::Api.new(api_key)
31
31
  #### Create a social post
32
32
 
33
33
  ```ruby
34
+ image1 = {data: Base64.encode64(image.download), filename: 'image.jpeg', content_type: 'image/jpeg'}
35
+ # ...
34
36
  api.social_posts.create(
35
37
  publish_at: Time.now,
36
38
  text: "example text",
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'faraday'
4
3
  require 'socialfred/social_posts'
5
4
 
6
5
  module Socialfred
7
6
  class Api
8
7
  attr_reader :api_key, :api_url
9
8
 
10
- API_URL = 'https://socialfred.com/api/'
9
+ API_URL = 'https://app.socialfred.com/api/'
11
10
 
12
11
  def initialize(api_key, api_url: API_URL)
13
12
  @api_key = api_key
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Socialfred
6
+ class Requester
7
+ attr_reader :api_key, :api_url
8
+
9
+ CONTENT_TYPE = 'application/json'
10
+
11
+ def initialize(api_key, api_url)
12
+ @api_key = api_key
13
+ @api_url = api_url
14
+ end
15
+
16
+ def get(endpoint, parameters = nil)
17
+ conn.get(endpoint, parameters) do |req|
18
+ req.headers[:content_type] = CONTENT_TYPE
19
+ end
20
+ end
21
+
22
+ def post(endpoint, parameters = nil)
23
+ conn.post(endpoint) do |req|
24
+ req.headers[:content_type] = CONTENT_TYPE
25
+ req.body = JSON.generate(parameters)
26
+ end
27
+ end
28
+
29
+ def put(endpoint, parameters = nil)
30
+ conn.put(endpoint) do |req|
31
+ req.headers[:content_type] = CONTENT_TYPE
32
+ req.body = JSON.generate(parameters)
33
+ end
34
+ end
35
+
36
+ def delete(endpoint)
37
+ conn.delete(endpoint) do |req|
38
+ req.headers[:content_type] = CONTENT_TYPE
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def conn
45
+ @conn ||= Faraday.new(url: api_url) do |faraday|
46
+ faraday.adapter Faraday.default_adapter
47
+ faraday.headers['Api-Key'] = api_key
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'socialfred/requester'
3
4
  require 'time'
4
5
 
5
6
  module Socialfred
@@ -14,7 +15,7 @@ module Socialfred
14
15
  end
15
16
 
16
17
  def all(page: 1, per_page: 10)
17
- response = conn.get(ENDPOINT, page: page, per_page: per_page)
18
+ response = requester.get(ENDPOINT, page: page, per_page: per_page)
18
19
 
19
20
  raise Socialfred::Error unless response.status == 200
20
21
 
@@ -22,7 +23,7 @@ module Socialfred
22
23
  end
23
24
 
24
25
  def find(social_post_id)
25
- response = conn.get(ENDPOINT + "/#{social_post_id}")
26
+ response = requester.get(ENDPOINT + "/#{social_post_id}")
26
27
 
27
28
  raise Socialfred::Error unless response.status == 200
28
29
 
@@ -30,12 +31,11 @@ module Socialfred
30
31
  end
31
32
 
32
33
  def create(publish_at: nil, text:, images: nil, options: nil)
34
+ check_images(images) if images
35
+
33
36
  publish_at = Time.parse(publish_at.to_s).iso8601 if publish_at
34
37
  parameters = { social_post: { published_at: publish_at, text: text, images: images, options: options }.compact }
35
- response = conn.post(ENDPOINT) do |req|
36
- req.headers[:content_type] = 'application/json'
37
- req.body = JSON.generate(parameters)
38
- end
38
+ response = requester.post(ENDPOINT, parameters)
39
39
 
40
40
  raise Socialfred::Error unless response.status == 200
41
41
 
@@ -43,12 +43,11 @@ module Socialfred
43
43
  end
44
44
 
45
45
  def update(social_post_id, publish_at: nil, text:, images: nil, options: nil)
46
+ check_images(images) if images
47
+
46
48
  publish_at = Time.parse(publish_at.to_s).iso8601 if publish_at
47
49
  parameters = { social_post: { published_at: publish_at, text: text, images: images, options: options }.compact }
48
- response = conn.put(ENDPOINT + "/#{social_post_id}") do |req|
49
- req.headers[:content_type] = 'application/json'
50
- req.body = JSON.generate(parameters)
51
- end
50
+ response = requester.put(ENDPOINT + "/#{social_post_id}", parameters)
52
51
 
53
52
  raise Socialfred::Error unless response.status == 200
54
53
 
@@ -56,7 +55,7 @@ module Socialfred
56
55
  end
57
56
 
58
57
  def destroy(social_post_id)
59
- response = conn.delete(ENDPOINT + "/#{social_post_id}")
58
+ response = requester.delete(ENDPOINT + "/#{social_post_id}")
60
59
 
61
60
  raise Socialfred::Error unless response.status == 200
62
61
 
@@ -65,11 +64,18 @@ module Socialfred
65
64
 
66
65
  private
67
66
 
68
- def conn
69
- @conn ||= Faraday.new(url: api_url) do |faraday|
70
- faraday.adapter Faraday.default_adapter
71
- faraday.headers['Api-Key'] = api_key
72
- end
67
+ def check_images(images)
68
+ raise(Socialfred::Error, 'images must be array') unless images.is_a?(Array)
69
+ return if images.all? { |image| image.key?(:data) && image.key?(:filename) && image.key?(:content_type) }
70
+
71
+ raise(
72
+ Socialfred::Error,
73
+ 'images must contain the following attributes: data (base64 encoded image), filename and content_type'
74
+ )
75
+ end
76
+
77
+ def requester
78
+ @requester ||= Requester.new(api_key, api_url)
73
79
  end
74
80
  end
75
81
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Socialfred
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: socialfred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Krasnoperov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-01 00:00:00.000000000 Z
11
+ date: 2019-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -157,6 +157,7 @@ files:
157
157
  - bin/setup
158
158
  - lib/socialfred.rb
159
159
  - lib/socialfred/api.rb
160
+ - lib/socialfred/requester.rb
160
161
  - lib/socialfred/social_posts.rb
161
162
  - lib/socialfred/version.rb
162
163
  - socialfred.gemspec