Class: Mushikago::Http::Client

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

Overview

Mushikagoサービスに接続するクライアント

Direct Known Subclasses

Mushikago::Hotaru::Client, Mitsubachi::Client, Tombo::Client

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(options = {})

A new instance of Client

Parameters:

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

Options Hash (options):

  • :api_key(Configured (String)

    value) APIキー

  • :secret_key(Configured (String)

    value) 秘密鍵



21
22
23
24
25
# File 'lib/mushikago/http/client.rb', line 21

def initialize options={}
  @api_key = (options[:api_key] || Mushikago.config.api_key).to_s
  @signer = Mushikago::Auth::Signer.new((options[:secret_key] || Mushikago.config.secret_key).to_s)
  @use_ssl = options.has_key?(:use_ssl) ? options[:use_ssl] : Mushikago.config.use_ssl
end

Instance Attribute Details

- (String) api_key (readonly)

Api_key

Returns:

  • (String)

    api_key



10
11
12
# File 'lib/mushikago/http/client.rb', line 10

def api_key
  @api_key
end

- (Signer) signer (readonly)

Signer

Returns:

  • (Signer)

    signer



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

def signer
  @signer
end

- (Boolean) use_ssl (readonly)

Use_ssl

Returns:

  • (Boolean)

    use_ssl



16
17
18
# File 'lib/mushikago/http/client.rb', line 16

def use_ssl
  @use_ssl
end

Instance Method Details

- (Mushikago::Http::Response) send_request(request)

Response

Parameters:

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mushikago/http/client.rb', line 29

def send_request request
  # add authorization
  request['api_key'] = api_key
  request['timestamp'] = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  request.add_signature!(signer)

  # send request
  port = use_ssl ? 443 : 80
  port = request.port unless request.port.nil?
  session = Net::HTTP.new(request.host, port)
  session.use_ssl = use_ssl
  session.verify_mode = OpenSSL::SSL::VERIFY_NONE if use_ssl
  session.start do |http|
    http_request = request.to_http_request
    http_response = http.request(http_request)
    return Mushikago::Http::Response.new(JSON.parse(http_response.body))
  end
end