Class: Mediumite::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(options = {})
  @options = options
end

Instance Method Details

#agentObject



19
20
21
22
23
24
# File 'lib/mediumite/client.rb', line 19

def agent
  @agent ||= Sawyer::Agent.new("https://api.medium.com/v1") do |http|
    http.headers[:content_type] = "application/json"
    http.authorization "Bearer", token
  end
end

#create_post(post) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mediumite/client.rb', line 38

def create_post(post)
  if post.under_publication?
    request(:post, "publications/#{post.publication_id}/posts", post.to_json)
  else
    request(:post, "users/#{user.id}/posts", post.to_json)
  end
end

#get_publicationsObject



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

def get_publications
  request(:get, "users/#{user.id}/publications")
end

#inspectObject



9
10
11
12
13
# File 'lib/mediumite/client.rb', line 9

def inspect
  inspected = super
  inspected = inspected.gsub! token, "#{'*'*token.length}" if token
  inspected
end

#request(method, path, data = {}) ⇒ Object



26
27
28
# File 'lib/mediumite/client.rb', line 26

def request(method, path, data = {})
  agent.call(method, URI::Parser.new.escape(path.to_s), data).data.data
end

#tokenObject



15
16
17
# File 'lib/mediumite/client.rb', line 15

def token
  @options[:token]
end

#userObject



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

def user
  @user ||= request(:get, "me")
end