Module: Devcenter::Client

Extended by:
Client
Includes:
Helpers
Included in:
Client
Defined in:
lib/devcenter/client.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Methods included from Helpers

#article_api_path, #article_path, #article_url?, #broken_link_checks_path, #devcenter_base_url, #html_file_path, #md_file_path, #netrc_path, #search_api_path, #slug_from_article_url, #update_article_path, #validate_article_path, #write_file

Instance Method Details

#auth_request(method, token, args) ⇒ Object



73
74
75
76
77
78
# File 'lib/devcenter/client.rb', line 73

def auth_request(method, token, args)
  args[:headers] ||= {}
  args[:headers].merge!(default_headers)
  args[:headers]["Authorization"] = "Basic #{encode64(token)}"
  response client.send(method, args)
end


52
53
54
55
# File 'lib/devcenter/client.rb', line 52

def check_broken_links(token, content)
  form = URI.encode_www_form({content: content})
  auth_request(:post, token, path: broken_link_checks_path, body: form, :headers => { "Content-Type" => "application/x-www-form-urlencoded" })
end

#clientObject



80
81
82
# File 'lib/devcenter/client.rb', line 80

def client
  @client ||= Excon.new(devcenter_base_url, :headers => default_headers)
end

#default_headersObject



84
85
86
# File 'lib/devcenter/client.rb', line 84

def default_headers
  {'User-agent' => 'DevCenterCLI'}
end

#encode64(str) ⇒ Object



98
99
100
# File 'lib/devcenter/client.rb', line 98

def encode64(str)
  Base64.encode64(str).strip
end

#get(args) ⇒ Object



65
66
67
# File 'lib/devcenter/client.rb', line 65

def get(args)
  response client.get(args)
end

#get_oauth_token(email, password) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/devcenter/client.rb', line 88

def get_oauth_token(email, password)
  auth = encode64("#{email}:#{password}")
  response Excon.post('https://api.heroku.com/oauth/authorizations',
              headers: {
                "Content-Type" => "application/json",
                "Accept" => "application/vnd.heroku+json; version=3",
                "Authorization" => "Basic #{auth}"
            })
end

#head(args) ⇒ Object



61
62
63
# File 'lib/devcenter/client.rb', line 61

def head(args)
  response client.head(args)
end

#put(args) ⇒ Object



69
70
71
# File 'lib/devcenter/client.rb', line 69

def put(args)
  response client.put(args)
end

#response(request_call) ⇒ Object



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

def response(request_call)
  Response.new request_call
end

#update_article(token, article_id, article_params) ⇒ Object



47
48
49
50
# File 'lib/devcenter/client.rb', line 47

def update_article(token, article_id, article_params)
  form = URI.encode_www_form(article_params)
  auth_request(:put, token, path: update_article_path(article_id), body: form, :headers => { "Content-Type" => "application/x-www-form-urlencoded" })
end

#validate_article(token, article_id, article_params) ⇒ Object



42
43
44
45
# File 'lib/devcenter/client.rb', line 42

def validate_article(token, article_id, article_params)
  form = URI.encode_www_form(article_params)
  auth_request(:post, token, path: validate_article_path(article_id), body: form, :headers => { "Content-Type" => "application/x-www-form-urlencoded" })
end