Class: Yhd::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/yhd/client.rb

Overview

API Client

Constant Summary collapse

DEFAULT_ENDPOINT =
"http://openapi.yhd.com"
DEFAULT_PATH =
"/app/api/rest/router"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Orders

#get_detail_orders, #get_orders

Constructor Details

#initialize(options) ⇒ Client

Initializes a new Client object

Parameters:

  • options (Hash)

Options Hash (options):

  • :app_key (String)

    Required.

  • :app_secret (String)

    Required.

  • :session_key (String)

    Required.

  • :endpoint (String)

    Optional. API endpoint



22
23
24
25
26
27
# File 'lib/yhd/client.rb', line 22

def initialize(options)
  @app_key = options.fetch(:app_key)
  @app_secret = options.fetch(:app_secret)
  @session_key = options.fetch(:session_key)
  @endpoint = options[:endpoint]
end

Instance Attribute Details

#app_keyObject (readonly)

Returns the value of attribute app_key.



14
15
16
# File 'lib/yhd/client.rb', line 14

def app_key
  @app_key
end

#app_secretObject (readonly)

Returns the value of attribute app_secret.



14
15
16
# File 'lib/yhd/client.rb', line 14

def app_secret
  @app_secret
end

#session_keyObject (readonly)

Returns the value of attribute session_key.



14
15
16
# File 'lib/yhd/client.rb', line 14

def session_key
  @session_key
end

Instance Method Details

#connection_optionsHash

Connection options for faraday

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yhd/client.rb', line 42

def connection_options
  {
    :builder => middleware,
    :headers => {
      :accept => 'application/json',
      :user_agent => user_agent,
    },
    :request => {
      :open_timeout => 10,
      :timeout => 30,
    }
  }
end

#delete(params = {}) ⇒ Object

Perform an HTTP DELETE request



86
87
88
# File 'lib/yhd/client.rb', line 86

def delete(params = {})
  request(:delete, params)
end

#endpointString

Returns:

  • (String)


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

def endpoint
  @endpoint ||= DEFAULT_ENDPOINT
end

#get(params = {}) ⇒ Object

Perform an HTTP GET request



71
72
73
# File 'lib/yhd/client.rb', line 71

def get(params = {})
  request(:get, params)
end

#middlewareFaraday::RackBuilder

Returns:

  • (Faraday::RackBuilder)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yhd/client.rb', line 57

def middleware
  Faraday::RackBuilder.new do |faraday|
    # Checks for files in the payload, otherwise leaves everything untouched
    faraday.request :multipart
    # Encodes as "application/x-www-form-urlencoded" if not already encoded
    faraday.request :url_encoded
    # Parse JSON response bodies
    faraday.response :parse_json
    # Set default HTTP adapter
    faraday.adapter :net_http
  end
end

#post(params = {}) ⇒ Object

Perform an HTTP POST request



76
77
78
# File 'lib/yhd/client.rb', line 76

def post(params = {})
  request(:post, params)
end

#put(params = {}) ⇒ Object

Perform an HTTP PUT request



81
82
83
# File 'lib/yhd/client.rb', line 81

def put(params = {})
  request(:put, params)
end

#user_agentString

Returns:

  • (String)


35
36
37
# File 'lib/yhd/client.rb', line 35

def user_agent
  "Yhd Ruby Gem #{Yhd::VERSION}"
end