Class: MediumSdk::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
# File 'lib/medium_sdk/client.rb', line 7

def initialize(opts = {})
  if opts.key? :integration_token
    @connection = MediumSdk::Connection::IntegrationToken.new opts
  elsif opts.key? :client_id
    @connection = MediumSdk::Connection::AuthCode.new opts
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/medium_sdk/client.rb', line 3

def connection
  @connection
end

#meObject (readonly)

Returns the value of attribute me.



5
6
7
# File 'lib/medium_sdk/client.rb', line 5

def me
  @me
end

Instance Method Details

#body_key(res, key) ⇒ Object



21
22
23
# File 'lib/medium_sdk/client.rb', line 21

def body_key(res, key)
  return res.body.key?(key) ? res.body[key] : nil
end

#get_url(url) ⇒ Object



15
16
17
18
19
# File 'lib/medium_sdk/client.rb', line 15

def get_url(url)
  return @connection.http.get do |req|
    req.url url
  end
end

#post(post) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/medium_sdk/client.rb', line 35

def post(post)
  url = ''
  if post.key? :publicationId
    publication_id = post[:publicationId].clone
    post.delete :publicationId
    url = File.join 'publications', publication_id, 'posts'
  else
    me unless @me
    unless @me.is_a?(Hash) && @me.key?('id') && @me['id'].to_s.length>0
      raise 'Authorized User Id is unknown'
    end
    id = @me['id']
    url = File.join 'users', 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



56
57
58
59
# File 'lib/medium_sdk/client.rb', line 56

def publication_contributors(publication_id)
  res = get_url File.join 'publications', publication_id, 'contributors'
  return body_key(res, 'data')
end

#user_publications(user_id) ⇒ Object



30
31
32
33
# File 'lib/medium_sdk/client.rb', line 30

def user_publications(user_id)
  res = get_url File.join 'users', user_id, 'publications'
  return body_key(res, 'data')
end