Skip to content

Quick Tutorial English

Akinori Yamada edited this page Aug 21, 2014 · 5 revisions

Setup

Firstly, install Aeromock in reference to Setup

Clone Tutorial

There is tutorial in repository of Aeromock.

$ git clone [email protected]:CyberAgent/aeromock.git

This tutorial is contents you can check behaviors by using supported template.

Make ~/.aeromock/config.yaml

Make ~/.aeromock/config.yaml to run this tutorial. At config.yaml, specify path of project.yaml as configuration file of Aeromock project.

In the case of Freemarker, specify the following. path-to-path is the location cloned Aeromock.

project_config_path: ~/path-to-path/aeromock/tutorial/freemarker/project.yaml

Check the

Start Aeromock, then check behaviors.

$ aeromock

Aeromock is listening on port 3183. You can check http://localhost:3183/test image

Explanation

Template file is tutorial/freemarker/template/test.ftl, data file is tutorial/data/test.yaml. Please check these files.

Basic data

For example, area of simple nest.

<h4>simple nest</h4>
<ul>
	<li>nest.level1 = ${nest.level1}</li>
	<li>nest.property1 = ${nest.property1}</li>
	<li>nest.child.level2 = ${nest.child.level2}</li>
	<li>nest.child.property2 = ${nest.child.property2}</li>
</ul>

Data structure is the following. Data include variables as REQUEST_URI, then be able to embed builtin variables of Aeromock.

nest:
  level1: ${REQUEST_URI}_level1
  property1: hoge1
  child:
    level2: ${REQUEST_URI}_level2
    property2: hoge2
Invoke method

In area nest with method, invoke method of objects.

<h4>nest with method</h4>
<ul>
	<li>nestWithMethod.level1 = ${nestWithMethod.level1}</li>
	<li>nestWithMethod.property1 = ${nestWithMethod.property1}</li>
	<li>nestWithMethod.execute1() = ${nestWithMethod.execute1()}</li>
	<#assign execute2Result = nestWithMethod.execute2()/>
	<li>nestWithMethod.execute2().id = ${execute2Result.id}</li>
	<li>nestWithMethod.execute2().value = ${execute2Result.value}</li>
	<#assign execute3Result = nestWithMethod.execute3()/>
	<li>nestWithMethod.execute3().property1 = ${execute3Result.property1}</li>
	<#list execute3Result as element>
	<li>nestWithMethod.execute3()[${element_index}] = ${element}</li>
	</#list>
	<li>nestWithMethod.execute3().execute().childProperty = ${execute3Result.execute().childProperty}</li>
	<#assign execute4Result = nestWithMethod.execute4()/>
	<li>execute4Result.property1 = ${nestWithMethod.execute4().property1}</li>
	<#list execute4Result as element>
	<li>nestWithMethod.execute4()[${element_index}].level1 = ${element.level1}</li>
	<li>nestWithMethod.execute4()[${element_index}].execute() = ${element.execute()}</li>
	</#list>
</ul>
```

Create property of `__methods`, be able to define methods and return value.
```Ruby
nestWithMethod:
  level1: ${REQUEST_URI}_level1
  property1: hoge1
  __methods:
    - name: execute1
      value: execute1_result
    - name: execute2
      value:
        id: execute2_result
        value: ${REQUEST_URI}_execute2
    - name: execute3
      value:
        property1: hoge3_1
        __list:
          - ${REQUEST_URI}
          - element3_2
        __methods:
          - name: execute
            value:
              childProperty: ${REQUEST_URI}_child_3
    - name: execute4
      value:
        property1: hoge4_1
        __list:
          - level1: ${REQUEST_URI}_1
            __methods:
              - name: execute
                value: execute_${REQUEST_URI}_1
          - level1: ${REQUEST_URI}_2
            __methods:
              - name: execute
                value: execute_${REQUEST_URI}_2
```

##### Common data
In area of `common data`, refers not defined variables at test.yaml, but these are referenced from common data file.

```html
<h4>common data</h4>
<ul>
	<li>commonProp = ${commonProp}</li>
	<li>commonHash.prop1 = ${commonHash.prop1}</li>
</ul>
```

 common data file
The following two files. Defined `commonProp`, `commonHash` in these files.

* [tutorial/data/common/common_pc.yaml](https://github.com/CyberAgent/aeromock/blob/master/tutorial/data/common/common_pc.yaml)
* [tutorial/data/common/common_sp.yaml](https://github.com/CyberAgent/aeromock/blob/master/tutorial/data/common/common_sp.yaml)

In [tutorial/freemarker/data.groovy](https://github.com/CyberAgent/aeromock/blob/master/tutorial/freemarker/data.groovy), select either file by User-Agent string.

```Groovy
if (USER_AGENT =~ /(iPhone|Android)/ ) {
    return ["common/common_sp"]
} else {
    return ["common/common_pc"]
}
```

##### Custom Tag
In area of `use tag`, use custom tag as `@span`.

```html
<h4>use tag</h4>
<ul>
    <li><@span style="font-style: italic;">span test</@span></li>
</ul>
```
This implementation is [tutorial/freemarker/tag/span.groovy](https://github.com/CyberAgent/aeromock/blob/master/tutorial/freemarker/tag/span.groovy).

Control output of `@span`.

```Groovy
_writer.write("""<span""")

// add class attribute
_writer.write(""" class="special-span" """)

// write tag attributes
for (entry in _params) {
    _writer.write(""" ${entry.key}=\"${entry.value}\"""")
}

_writer.write(""">""")
if (_body != null) {
    // render inner body.
    _body.render(_writer)
}
_writer.write("""</span>""")
```

##### Custom Function
In area of `use function`, use custom function as `staticUrl`.

```html
<h4>use function</h4>
<div>
    <img src=${staticUrl("/img/sample.jpg")} />
</div>
```

This implementation is [tutorial/freemarker/function/staticUrl.groovy](https://github.com/CyberAgent/aeromock/blob/master/tutorial/freemarker/function/staticUrl.groovy).

Return url of target image from relative path.
```Groovy
if (arguments.isEmpty()) {
  throw new RuntimeException("Argument required")
}

return "http://$HOST${arguments[0]}"
```
Clone this wiki locally