Class: GitHubMarkdownAPI::APIClient Abstract

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

Overview

This class is abstract.

Abstract class of API Clients

Direct Known Subclasses

Raw

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, sub_args = {}) ⇒ APIClient

Returns a new instance of APIClient.

Parameters:

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


9
10
11
12
13
14
15
16
# File 'lib/github_markdown_api/api_client.rb', line 9

def initialize (args = {}, sub_args = {})
  case args
  when Hash
    set_option args
  when String
    # pending
  end
end

Instance Attribute Details

#content_typeString

Returns HTTP ContentType.

Returns:

  • (String)

    HTTP ContentType



34
35
36
# File 'lib/github_markdown_api/api_client.rb', line 34

def content_type
  @content_type
end

#endpointsHash

Returns Endpoint (Server path of API).

Returns:

  • (Hash)

    Endpoint (Server path of API)



31
32
33
# File 'lib/github_markdown_api/api_client.rb', line 31

def endpoints
  @endpoints
end

#hostString

Returns Hostname.

Returns:

  • (String)

    Hostname



25
26
27
# File 'lib/github_markdown_api/api_client.rb', line 25

def host
  @host
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



18
19
20
# File 'lib/github_markdown_api/api_client.rb', line 18

def last_response
  @last_response
end

#portFixnum

Returns Port number.

Returns:

  • (Fixnum)

    Port number



28
29
30
# File 'lib/github_markdown_api/api_client.rb', line 28

def port
  @port
end

#schemeSymbol

Note:

:http of :https

Returns Scheme of API.

Returns:

  • (Symbol)

    Scheme of API



22
23
24
# File 'lib/github_markdown_api/api_client.rb', line 22

def scheme
  @scheme
end

Class Method Details

.render(markdown) ⇒ Object



108
109
110
# File 'lib/github_markdown_api/api_client.rb', line 108

def self.render (markdown)
  return self.new.render markdown
end

Instance Method Details

#default_optionsHash

Returns hash of default options

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
59
# File 'lib/github_markdown_api/api_client.rb', line 50

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

Returns endpoint of API type

Parameters:

  • type (Symbol, #to_sym)

Returns:

  • (URI)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/github_markdown_api/api_client.rb', line 93

def endpoint (type)
  path  = @endpoints[type.to_sym]
  klass = case @scheme
          when :http;  URI::HTTP
          when :https; URI::HTTPS
          end
  param = {
    host: @host,
    path: path,
  }
  param[:port] = @port || 443

  return  klass.build(param)
end

#render(markdown) ⇒ Object

This method is abstract.

Renders HTML from Markdown

Parameters:

  • markdown (String)

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/github_markdown_api/api_client.rb', line 86

def render (markdown)
  raise NotImplementedError, "#{__method__} is a abstract method."
end

#request(raw_uri, post) ⇒ String

Requests API

Parameters:

  • raw_uri (URI)
  • request (Hash)

Returns:

  • (String)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/github_markdown_api/api_client.rb', line 65

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

  response = http.start{ http.request post }
  @last_response = response

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

#set_option(args) ⇒ self

Parameters:

  • args (Hash)

Returns:

  • (self)


38
39
40
41
42
43
44
45
46
# File 'lib/github_markdown_api/api_client.rb', line 38

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