freefeed-client 0.1.0 → 1.1.0

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: 51d600de0410a6c2e6341267d64986af26b39add
4
- data.tar.gz: 2c3b31d286d4b5cb477d38389b00306cb973540e
3
+ metadata.gz: 22b40faff7e8f35015ea4eafb279f5d6e8f7fe57
4
+ data.tar.gz: 1a20bc8d5471ea2871815496e6ec5f371d7107dc
5
5
  SHA512:
6
- metadata.gz: a1aed947a23f1fcc9b9ae852841ecc72688455e0ae499cd504b05a510a823edf3cd0e0a762c8d5cad748673be74d0958824e5c0451628c5a6e4a2469eb81d174
7
- data.tar.gz: 2f6299a90a0d4e7e783604d2c7d27f00fcc917ce9ac7f212c6303826231aa31727be25bcac965fe3bb4f134738a86bcd14d8f16a20deca7cd248c8a09a7a0e73
6
+ metadata.gz: c1518543888e251f82c382f63500b8b741fe01ef76f9dffb31f263d70ad11033159a443f9856ad31636f128252458b3fc6957c45c38b9632c56cdd53d82c9f73
7
+ data.tar.gz: 140e6030f65557cb10fe73a887e2aa3d9c23345bc459f878015490a77a921669d732cc7b2d7fd6b6711e91827e5e9db03e1f8f476b9c5ca4f0c0115fc2ad1bd6
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Ruby wrapper for [Freefeed API](https://fetsh.github.io/freefeed-api).
4
4
 
5
- [![Gem Version](https://badge.fury.io/rb/feefeed-client.svg)](http://badge.fury.io/rb/freefeed-client)
5
+ [![Gem Version](https://badge.fury.io/rb/freefeed-client.svg)](https://badge.fury.io/rb/freefeed-client)
6
6
 
7
7
  ## Installation
8
8
 
@@ -44,12 +44,10 @@ post = Freefeed::Types::PostCreate.new(
44
44
  }
45
45
  )
46
46
 
47
- post_resource = Freefeed::Post.new(client: client)
48
-
49
- post_resource.create(post)
47
+ Freefeed::Post.create(client, post)
50
48
  ```
51
49
 
52
- You can skip instantiating resource, but I'm not sure if this is a good practice:
50
+ You can achive the same result without addressing `Freefeed::Post` class directly:
53
51
 
54
52
  ```ruby
55
53
  require 'freefeed'
@@ -67,10 +65,10 @@ post = Freefeed::Types::PostCreate.new(
67
65
  }
68
66
  )
69
67
 
70
- client.posts.create(post)
68
+ client.posts_create(post)
71
69
  ```
72
70
 
73
- You can even skip instantiating post type, but you will lose some validation:
71
+ You can even skip instantiating `PostCreate` type, but you will lose some validation:
74
72
 
75
73
 
76
74
  ```ruby
@@ -78,12 +76,12 @@ require 'freefeed'
78
76
 
79
77
  client = Freefeed::Client.new('yourFreefeedAPIToken')
80
78
 
81
- client.posts.create({post: {body: 'Hello World!'}, meta: {feeds: ['yourusername']}})
79
+ client.posts_create({post: {body: 'Hello World!'}, meta: {feeds: ['yourusername']}})
82
80
  ```
83
81
 
84
82
  ## Logging
85
83
 
86
- By default, `freefeed-client` logs everything to STDOUT. You can change this behavior and provide your own logger class with someo ptions. See example below:
84
+ By default, `freefeed-client` logs everything to STDOUT. You can change this behavior and provide your own logger class with some options. See example below:
87
85
 
88
86
  ```ruby
89
87
  client = Freefeed::Client.new(
data/lib/freefeed.rb CHANGED
@@ -31,16 +31,16 @@ require 'freefeed/types/comment'
31
31
  require 'freefeed/types/post'
32
32
 
33
33
  ResourceKitling::Resource.subclasses.each do |sc|
34
- mn = sc.to_s.downcase.split('::').last.pluralize
35
- if Freefeed::Client.method_defined?(mn)
36
- raise(
37
- ArgumentError,
38
- "Method '#{mn}' is already defined on Freefeed::Client"
39
- )
40
- end
41
- Freefeed::Client.send(:define_method, mn) do
42
- instance_variable_get("@#{mn}") || instance_variable_set(
43
- "@#{mn}", sc.new(client: self)
44
- )
34
+ sc.actions.each do |an|
35
+ mn = "#{sc.to_s.downcase.split('::').last.pluralize}_#{an.name}"
36
+ if Freefeed::Client.method_defined?(mn)
37
+ raise(
38
+ ArgumentError,
39
+ "Method '#{mn}' is already defined on Freefeed::Client"
40
+ )
41
+ end
42
+ Freefeed::Client.send(:define_method, mn) do |*args|
43
+ sc.create(self, *args)
44
+ end
45
45
  end
46
46
  end
@@ -10,20 +10,11 @@ module Freefeed
10
10
  end
11
11
 
12
12
  def authenticate(username:, password:)
13
- Session.new(client: self)
14
- .create(username: username, password: password).tap do |re|
13
+ Session.create(self, username: username, password: password).tap do |re|
15
14
  @api_token = re.fetch('authToken')
16
15
  end
17
16
  end
18
17
 
19
- def call(endpoint, params = {}, headers = {})
20
- guard_wrong_endpoint(endpoint)
21
- call!(
22
- ENDPOINTS[endpoint][:method], path(endpoint),
23
- build_options(endpoint, params), headers
24
- )
25
- end
26
-
27
18
  def call!(verb, endpoint, params = {}, headers = {})
28
19
  response = conn.send(
29
20
  verb, endpoint, params, auth_header.merge(headers)
@@ -47,29 +38,6 @@ module Freefeed
47
38
  end
48
39
  end
49
40
 
50
- def guard_wrong_endpoint(endpoint)
51
- raise Exceptions::WrongEndpoint unless ENDPOINTS.keys.include?(endpoint)
52
- end
53
-
54
- def build_options(endpoint, params)
55
- default_params(endpoint).merge(sanitize_params(endpoint, params))
56
- end
57
-
58
- def path(endpoint)
59
- (endpoint.start_with?('/') ? '' : BASE_PATH) + endpoint.split('#').first
60
- end
61
-
62
- def default_params(endpoint)
63
- {}.merge(ENDPOINTS[endpoint][:default_params] || {})
64
- end
65
-
66
- def sanitize_params(endpoint, params)
67
- return {} unless ENDPOINTS[endpoint][:params]
68
- params.delete_if do |key, _|
69
- !ENDPOINTS[endpoint][:params].include?(key.to_s)
70
- end
71
- end
72
-
73
41
  def handle_erros(response)
74
42
  raise(
75
43
  Exceptions::ResponseError.new(response),
@@ -3,14 +3,16 @@ require 'dry-struct'
3
3
  module Freefeed
4
4
  module Types
5
5
  class CommentCreate < Dry::Struct
6
- attribute :comment, Types::Hash.schema(
7
- body: Types::Body,
8
- postId: Types::UID
9
- )
6
+ attribute :comment do
7
+ attribute :body, Types::Body
8
+ attribute :postId, Types::UID
9
+ end
10
10
  end
11
11
 
12
12
  class CommentUpdate < Dry::Struct
13
- attribute :comment, Types::Hash.schema(body: Types::Body)
13
+ attribute :comment do
14
+ attribute :body, Types::Body
15
+ end
14
16
  end
15
17
  end
16
18
  end
@@ -3,22 +3,22 @@ require 'dry-struct'
3
3
  module Freefeed
4
4
  module Types
5
5
  class PostCreate < Dry::Struct
6
- attribute :post, Types::Hash.schema(
7
- body: Types::Body,
8
- attachments: Types::Attachments.meta(omittable: true)
9
- )
10
- attribute :meta, Types::Hash.schema(
11
- commentsDisabled: Types::Strict::Bool.default(false),
12
- feeds: Types::AccountName | Types::Feeds
13
- )
6
+ attribute :post do
7
+ attribute :body, Types::Body
8
+ attribute :attachments, Types::Attachments.meta(omittable: true)
9
+ end
10
+ attribute :meta do
11
+ attribute :commentsDisabled, Types::Strict::Bool.default(false)
12
+ attribute :feeds, Types::AccountName | Types::Feeds
13
+ end
14
14
  end
15
15
 
16
16
  class PostUpdate < Dry::Struct
17
- attribute :post, Types::Hash.schema(
18
- body: Types::Body.meta(omittable: true),
19
- attachments: Types::Attachments.meta(omittable: true),
20
- feeds: Types::Feeds.meta(omittable: true)
21
- )
17
+ attribute :post do
18
+ attribute :body, Types::Body.meta(omittable: true)
19
+ attribute :attachments, Types::Attachments.meta(omittable: true)
20
+ attribute :feeds, Types::Feeds.meta(omittable: true)
21
+ end
22
22
  end
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module Freefeed
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -2,7 +2,7 @@ module ResourceKitling
2
2
  require 'forwardable'
3
3
  class ActionCollection
4
4
  extend Forwardable
5
- def_delegators :@collection, :find, :<<, :each, :include?
5
+ def_delegators :@collection, :find, :<<, :each, :include?, :map
6
6
  def initialize
7
7
  @collection = []
8
8
  end
@@ -46,6 +46,10 @@ module ResourceKitling
46
46
  np.gsub(":#{key}", value.to_s)
47
47
  end
48
48
  end
49
+
50
+ def to_s
51
+ name
52
+ end
49
53
  end
50
54
  class Resource
51
55
  class << self
@@ -62,16 +66,15 @@ module ResourceKitling
62
66
  self._actions ||= ActionCollection.new
63
67
  if block_given?
64
68
  self._actions.instance_eval(&block)
65
- # MethodFactory.construct(self, self._actions)
66
69
  self._actions.each do |action|
67
- if method_defined?(action.name)
70
+ if respond_to?(action.name.to_sym)
68
71
  raise(
69
72
  ArgumentError,
70
73
  "Action '#{action.name}' is already defined on `#{self}`"
71
74
  )
72
75
  end
73
76
  method_block = method_for_action(action)
74
- send(:define_method, action.name, &method_block)
77
+ send(:define_singleton_method, action.name, &method_block)
75
78
  end
76
79
  end
77
80
  self._actions
@@ -79,6 +82,7 @@ module ResourceKitling
79
82
 
80
83
  def self.method_for_action(action)
81
84
  Proc.new do |*args|
85
+ client = args.shift
82
86
  pathopts = action.path.include?(':') ? args.shift : {}
83
87
  payload = args.shift
84
88
  payload = payload.to_h if payload.respond_to?(:to_h)
@@ -92,10 +96,7 @@ module ResourceKitling
92
96
  self.class.actions
93
97
  end
94
98
 
95
- attr_reader :client
96
-
97
- def initialize(client: nil)
98
- @client = client
99
+ def initialize
99
100
  end
100
101
  end
101
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freefeed-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilia Zemskov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-13 00:00:00.000000000 Z
11
+ date: 2018-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday