Class: GitHubMarkdownAPI::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • args (Hash) (defaults to: {})


7
8
9
# File 'lib/github_markdown_api/client.rb', line 7

def initialize (args = {})
  set_option args
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



11
12
13
# File 'lib/github_markdown_api/client.rb', line 11

def last_response
  @last_response
end

Instance Method Details

#default_optionsHash

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
# File 'lib/github_markdown_api/client.rb', line 30

def default_options
  return {
    scheme:       GitHubMarkdownAPI::DEFAULT_SCHEME,
    host:         GitHubMarkdownAPI::DEFAULT_HOST,
    port:         GitHubMarkdownAPI::DEFAULT_PORT,
    endpoints:    GitHubMarkdownAPI::DEFAULT_ENDPOINTS,
    auth:         GitHubMarkdownAPI::DEFAULT_AUTH,
    content_type: GitHubMarkdownAPI::DEFAULT_CONTENT_TYPE,
  }
end

#endpoint(type) ⇒ URI

Parameters:

  • (Symbol, #to_sym)

Returns:

  • (URI)


70
71
72
73
74
75
76
77
78
# File 'lib/github_markdown_api/client.rb', line 70

def endpoint(type)
  path  = @endpoints[type.to_sym]
  klass = case @scheme
          when :http;  URI::HTTP
          when :https; URI::HTTPS
          end
  uri = klass.build(host: @host, path: path, port: @port)
  return uri
end

#raw(markdown) ⇒ String

Parameters:

  • markdown (String)

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/github_markdown_api/client.rb', line 43

def raw (markdown)
  raw_uri = endpoint(:raw)
  header  = {
    'Content-Type' => @content_type
  }
  post = Net::HTTP::Post.new(raw_uri, header)
  post.body = markdown

  request = Net::HTTP.new(raw_uri.host, raw_uri.port)
  if @scheme == :https
    request.use_ssl = true
    request.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  response = request.start{|http| http.request(post) }
  @last_response = response

  case response
  when Net::HTTPSuccess
    return response.body
  else
    raise RuntimeError, response
  end
end

#renderObject



13
14
15
# File 'lib/github_markdown_api/client.rb', line 13

def render
  
end

#set_option(args) ⇒ Object

Returns [].

Parameters:

  • args (Hash)

Returns:



19
20
21
22
23
24
25
26
27
# File 'lib/github_markdown_api/client.rb', line 19

def set_option (args)
  option = default_options.merge(args)
  @scheme       = option[:scheme].intern
  @host         = option[:host].to_s
  @port         = option[:port].to_i
  @endpoints    = option[:endpoints].to_hash
  @content_type = option[:content_type].to_s
  return self
end