Class: MediumSdk::Client
- Inherits:
-
Object
- Object
- MediumSdk::Client
- Defined in:
- lib/medium_sdk/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #body_key(res, key) ⇒ Object
- #get_url(url) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #me(reload = nil) ⇒ Object
- #me_id ⇒ Object
- #post(post) ⇒ Object
- #publication_contributors(publication_id) ⇒ Object
- #user_publications(user_id = nil) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 |
# File 'lib/medium_sdk/client.rb', line 5 def initialize(opts = {}) if opts.key? :client_id @connection = MediumSdk::Connection::AuthCode.new opts elsif opts.key? :integration_token @connection = MediumSdk::Connection::IntegrationToken.new opts end end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
3 4 5 |
# File 'lib/medium_sdk/client.rb', line 3 def connection @connection end |
Instance Method Details
#body_key(res, key) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/medium_sdk/client.rb', line 19 def body_key(res, key) if res.status >= 400 raise "HTTP Error #{res.status} " + res.pretty_inspect end return res.body.key?(key) ? res.body[key] : nil end |
#get_url(url) ⇒ Object
13 14 15 16 17 |
# File 'lib/medium_sdk/client.rb', line 13 def get_url(url) return @connection.http.get do |req| req.url url end end |
#me(reload = nil) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/medium_sdk/client.rb', line 26 def me(reload = nil) if @_me.nil? || reload @_me = body_key get_url('me'), 'data' end return @_me end |
#me_id ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/medium_sdk/client.rb', line 33 def me_id me unless @_me unless @_me.is_a?(Hash) && @_me.key?('id') && @_me['id'].to_s.length>0 raise 'Authorized User Id is unknown' end return @_me['id'] end |
#post(post) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/medium_sdk/client.rb', line 47 def post(post) url = '' if post.key? :publicationId publication_id = post[:publicationId].clone post.delete :publicationId url = File.join 'publications', publication_id, 'posts' else url = File.join 'users', me_id(), 'posts' end res = @connection.http.post do |req| req.url url req.body = post end return body_key(res, 'data') end |
#publication_contributors(publication_id) ⇒ Object
63 64 65 66 |
# File 'lib/medium_sdk/client.rb', line 63 def publication_contributors(publication_id) res = get_url File.join 'publications', publication_id, 'contributors' return body_key(res, 'data') end |
#user_publications(user_id = nil) ⇒ Object
41 42 43 44 45 |
# File 'lib/medium_sdk/client.rb', line 41 def user_publications(user_id = nil) user_id = me_id if user_id.nil? res = get_url File.join 'users', user_id.to_s, 'publications' return body_key(res, 'data') end |